When it comes to receiving your arguments, I wouldn't focus on speed as much as I would on appearance. If you are passing one or two arguments, feel free to shift them off. Myself, I generally shift for only one or two args, any more I use the @_ method, not for speed, but for shorter, cleaner, uncluttered code. Just to exagerate my point, which snippet looks nicer?

# Example 1 sub my_sub { my $name = shift; my $mail = shift; my $city = shift; print "$name, from $city, has e-mail address $mail."; } my_sub('Coruscate', 'Red Spot', 'Jupiter'); # Example 2 sub my_sub { my ($name, $mail, $city) = @_; print "$name, from $city, has e-mail address $mail."; } my_sub('Coruscate', 'Red Spot', 'Jupiter'); # Example 3 # Once I hit 4+ arguments, I hit named arguments! # (Yes, I know the example only has 3 args...) sub my_sub { my %q = @_; print "$q{name}, from $q{city}, has e-mail address $q{mail}."; } my_sub( name => 'Coruscate', mail => 'Red Spot', city => 'Jupiter' );

Update: Another reason I really like named arguments is code that looks like the following:

sub my_sub { my %q = ( title => 'Untitled', author => 'Anonymous', values => [1,1,1,1,1], values2 => { key1 => 'value1', key2 => 'value2' }, @_ ); print "$q{title} (by $q{author}):\n", "\t- ", join(', ', @{$q{values}}), "\n", "\t- ", join(', ', values %{$q{values2}}), "\n"; } my_sub( title => 'My Title', values2 => { key1 => 'yippee!', key2 => 'booooo!' } );


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, reply to this node or /msg me to tell me what is wrong with the post, so that I may update the node to the best of my ability. If you do not inform me as to why the post deserved a downvote, your vote does not have any significance and will be disregarded.


In reply to Re: Silly question about function args by Coruscate
in thread Silly question about function args by Anonymous Monk

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.