Bristle Software Perl Tips

This page is offered as a service of Bristle Software, Inc.  New tips are sent to an associated mailing list when they are posted here.  Please send comments, corrections, any tips you'd like to contribute, or requests to be added to the mailing list, to tips@bristle.com.

Table of Contents:

  1. Passing parameters to subroutines
  2. Beware ampersand on call without parameters
  3. Accessing a database via DBI

Details of Tips:

  1. Passing parameters to subroutines

    Last Updated: 10/6/1999
    Applies to:  Perl 5

    Perl subroutine calls with parameters look like any of the following:

    	sub1 1, "abc", 2;
    	&sub1 1, "abc", 2;
    	sub1 (1, "abc", 2);
    	&sub1 (1, "abc", 2);

    The ampersand ("&") and the parentheses are optional in most cases.  The called subroutine can then refer to the parameters as elements of the array @_ as:

    	print "Parameter 1 is: $_[0]";
    	print "Parameter 2 is: $_[1]";
    	print "Parameter 3 is: $_[2]";

    Or it can copy the array into a list of local variables as:

    	my ($param1, $param2, $param3) = @_;
    
    	print "Parameter 1 is: $param1";
    	print "Parameter 2 is: $param2";
    	print "Parameter 3 is: $param3";

    Or it can shift the parameters into local variables as:

    	my $param1 = shift;
    	my $param2 = shift;
    	my $param3 = shift;
    	print "Parameter 1 is: $param1";
    	print "Parameter 2 is: $param2";
    	print "Parameter 3 is: $param3";

    To supply default arguments for the parameters, in case the caller omitted some or all of them:

    	my $param1 = ($_ = shift) ? $_ : 123;
    	my $param2 = ($_ = shift) ? $_ : "abcdef";
    	my $param3 = ($_ = shift) ? $_ : 456;
    	print "Parameter 1 is: $param1";
    	print "Parameter 2 is: $param2";
    	print "Parameter 3 is: $param3";

    --Fred

  2. Beware ampersand on call without parameters

    Last Updated: 10/10/1999
    Applies to:  Perl 5

    Perl subroutine calls without parameters look like any of the following:

    	sub1;
    	&sub1;
    	sub1();
    	&sub1();

    However, one of these is not like the others.   The 2nd call -- the one with an ampersand ("&") and no parentheses -- has the interesting behavior of passing along the parameter list that was passed to the caller, minus any "shift"ed parameters.  For example:

    	sub a
    	{
    	    my $arg1 = shift;
    	    print "arg1 of a = \"$arg1\"\n";
    	}
    	sub b
    	{
    	    my $arg1 = shift;
    	    print "arg1 of b = \"$arg1\"\n";
    	    &a;
    	}
    	print "Calling b...\n";
    	b 1,2;

    Here b is called with 2 parameters.  It shifts one of them into its $arg1 variable, and then calls a, apparently passing no parameters.   Inside a however, the first parameter is the value passed as the second parameter to b

    This is not usually what you want, but it is not a bug.  It is the documented intended behavior of Perl.  To prevent b's parameters from being implicitly passed to a, either remove the ampersand from the call to a or add parentheses to the call.

    --Fred

  3. Accessing a database via DBI

    Last Updated: 10/17/1999
    Applies to:  Perl 5

    Database access from Perl is easy using the DBI package.  For example, to read data from an Oracle database:

    use DBI;
    my $drh = DBI->install_driver('Oracle');
    if (! $drh) { print("Error: $DBI::errstr"); }
    my $dbh = $drh->connect("mydatabase", "myuserid", "mypassword");
    if (! $dbh) { print("Error: $DBI::errstr"); }
    my $sth = $dbh->prepare(
    		"select name, SSN, phone_number from person");
    if (! $sth) { print("Error: $DBI::errstr"); }
    my $rv = $sth->execute;
    if (! $rv) { print("Error: $DBI::errstr"); }
    while(($Name, $SSN, $Phone) = $sth->fetchrow)
    {
        DoSomethingWith($Name, $SSN, $Phone);
    }
    my $rv = $sth->finish;
    if (! $rv) { print("Error: $DBI::errstr"); }
    $rv = $dbh->disconnect;
    if (! $rv) { print("Error: $DBI::errstr"); }

    For more info on the DBI package, see:

                http://www.symbolstone.org/technology/perl/DBI/index.html

    --Fred

©Copyright 1999-2014, Bristle Software, Inc.  All rights reserved.