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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |