How do Perl named arguments work?





If you have questions or comments about this code or related issues, write to me at the address below. I'll expand the article as necessary. BTW I'm seeking employment. For a resume and background information, click here.

oldcoder@yahoo.com


# This example discusses normal (non-OO) subroutines.  OO-Perl is
# beyond the scope of the discussion.

# Presently,  this is  just a set of  code fragments.  If there's
# sufficient interest, it'll be expanded.

# If a  Perl subroutine takes  a short and  simple argument list,
# the standard @_ conventions are fine. For example, this subrou-
# tine takes two integers, adds them, and returns the result:

    use strict;                # These 3 statements are your fri-
    use Carp;                  # ends
    use warnings;

    sub addem
    {
        my ($a, $b) = @_;      # Argument list
        $a + $b;               # Result
    }
                               # Sample call
    print "1 + 2 is " . &addem (1, 2) . "\n";

# However,  for more complicated cases, named arguments should be
# used. Under the named-argument approach, you can pass arguments
# in any order and  there's no  confusion about which argument is
# which.  The tradeoff is that extra code is needed  both to pass
# arguments and to extract them on the receiving side.

# Here's a version of the previous subroutine modified to use the
# named-argument approach:

    sub addem2
    {
        my (%args) = @_;       # Argument table (this part is al-
                               # ways the same)

        my $a = $args {-a};    # Extract argument "a"
        my $b = $args {-b};    # Extract argument "b"
        $a + $b;               # Result
    }
                               # Sample call
    print "1 + 2 is " . &addem2 (-a => 1, -b => 2) . "\n";

                               # For this approach, argument or-
                               # der doesn't matter
    print "1 + 2 is " . &addem2 (-b => 2, -a => 1) . "\n";

# Note that  we've  started argument names  (in calls and in the
# args table)  with  dashes. This  is a  convention.  The dashes
# aren't  strictly required, but they should be used consistent-
# ly if they're used at all.

# Calls to  named-argument subroutines will often be multi-line.
# This  is O.K.  and in fact  recommended.  Multi-line  argument
# lists improve readability and allow for per-argument comments.
# Here's a sample call of this type  (we've omitted the  defini-
# tion of the associated subroutine, so this code won't actually
# run):

    my $NewWord = &MapAlias
    (                          # Map alias to canonical
                               # Flag: Map must succeed
        -Strict      => 0      ,
                               # This is a debugging string
        -DebugString => "1024" ,
                               # Word to make canonical
        -Word        => "applesauce"
    );





Hosting provided by Zymic.

For acknowledgments related to CSS and other code used, click here.

Linked or embedded works (software, books, articles, etc.) are presented under their own licenses.

Other unique content on this page, excluding screenshots, is distributed under the following license: C.C. Attribution NonCommercial ShareAlike 3.0. In some cases, screenshots are distributed under the same license. In other cases, the associated program's license applies.

image Valid XHTML 1.0