The idiom as I understand for extracting function arguments into lexicals is: my $var = shift;

Sample code:

use strict; use warnings; sub quux { my $foo = shift; my $bar = shift; my $baz = shift; print "FOO:$foo BAR:$bar BAZ:$baz\n"; } quux(1, 2, 'three'); quux(one => 2, 3); # illustrate "fat comma"

Sample output:

FOO:1 BAR:2 BAZ:three FOO:one BAR:2 BAZ:3

Generally, you should use one of these idioms near the very beginning of each sub to bring your arguments into lexicals. There is usually little difference between assigning @_ to a list of variables and using shift, but sometimes it can matter, particularly if you have additional arguments to process. You can also use splice on @_ to extract a group of arguments into a list of variables or an array, but this is a more advanced feature. Directly accessing elements of @_ is best limited to very short subs, with a comment listing the arguments nearby.

The "fat comma" is another neat Perl feature. You can think of it as quoting the previous word (there are some fairly complex rules for what gets quoted, but in most contexts it Just Works) and acting exactly like a normal comma in every other way. You will see it often in keyword arguments in many Perl modules. See the "Comma Operator" section in perlop for details.


In reply to Re: $dbh could not be passed to function unless another variable is also passed by jcb
in thread $dbh could not be passed to function unless another variable is also passed by tukusejssirs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.