Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: User-Defined Sub, Passing a Single Argument?

by kennethk (Abbot)
on Oct 15, 2014 at 17:34 UTC ( [id://1103941]=note: print w/replies, xml ) Need Help??


in reply to User-Defined Sub, Passing a Single Argument?

Note that Method #2 is buggy. You will not grab the argument, but the length of the argument array. You mean
sub myFunction { my ($arg) = @_; #blah blah blah, do some stuff..... } myFunction($myArg);
so that the assignment is in list context. See Context in perldata.

For my own work, I usually use

sub myFunction { my ($arg1, $arg2, ..., $argN) = @_; #blah blah blah, do some stuff..... } myFunction($myArg);
unless I'm writing an object method, in which case I use shift to get the reference:
sub myFunction { my $self = shift; my ($arg1, $arg2, ..., $argN) = @_; #blah blah blah, do some stuff..... } myFunction($myArg);
though, if it's just a quick and dirty iterator over a list, sometimes I skip stripping the argument array entirely:
sub myFunction { for (@_) { ... # lvalue; Note this impacts the passed values!! } } myFunction($myArg);
See also Best Practices(TM) reference card, noting that it doesn't weigh in.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1103941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-29 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found