Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

User-Defined Sub, Passing a Single Argument?

by mmartin (Monk)
on Oct 15, 2014 at 17:27 UTC ( [id://1103938]=perlquestion: print w/replies, xml ) Need Help??

mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I was in the middle of writing a sub/function in my script and I was curious about the following circumstance, and was
wondering if one of the methods below is preferred or recommended over the other. The situation is when you pass ONLY
a single argument to a sub-routine. So there will only ever be one argument passed to my sub/function...

Is one of these methods preferred/recommended over the other:

METHOD #1
sub myFunction { my $arg = shift; #blah blah blah, do some stuff..... } myFunction($myArg);

METHOD #2
sub myFunction { my $arg = @_; #blah blah blah, do some stuff..... } myFunction($myArg);

METHOD #3
sub myFunction { my $arg = $_[0]; #blah blah blah, do some stuff..... } myFunction($myArg);

I'm not sure if there are other ways to grab the argument besides those, but are any of those the preferred/recommended
method over the other?


Thanks in Advance,
Matt

Replies are listed 'Best First'.
Re: User-Defined Sub, Passing a Single Argument?
by kennethk (Abbot) on Oct 15, 2014 at 17:34 UTC
    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.

Re: User-Defined Sub, Passing a Single Argument?
by duelafn (Parson) on Oct 15, 2014 at 17:33 UTC

    Method 2 should have parens: my ($arg) = @_;

    As for which style to use, it really doesn't matter, it is all a matter of personal taste and I have used all three (though typically one of the first two).

    Update: Better phrasing, thanks kennethk

    Good Day,
        Dean

      You may want to update your phrasing; the choice of method doesn't really matter, but your comment sounds like the parentheses don't matter. The missing parentheses are a bug.


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

Re: User-Defined Sub, Passing a Single Argument?
by tobyink (Canon) on Oct 15, 2014 at 18:04 UTC

    I tend to use list assignment for most arguments:

    my ($foo, $bar, @baz) = @_;

    But if there are one or two arguments that are conceptually special, I'll use shift for them. (Or pop if it's the final argument, though that's less often the case.) For example, in OO code, the invocant:

    sub some_method { my $self = shift; my ($foo, $bar, @baz) = @_; ...; }

    Or for an around modifier, then the original coderef and the invocant would both be special:

    around some_method => sub { my ($orig, $self) = (shift, shift); my ($foo, $bar, @baz) = @_; ...; };

    Or if I was writing my own implementation of grep, then the coderef would be special enough:

    sub mygrep { my $code = shift; my (@items) = @_; ...; }

    My reason for this is that it's often nice to have all the "normal" arguments remaining in @_ in case you need to pass them all through to another function that you're calling.

    For single argument subroutines, I apply the same logic. If it's conceptually something like an invocant, I'll use shift; otherwise I'll use list assignment. After all, a single argument subroutine might end up taking more arguments as the project grows in complexity.

      Hey Toby, thanks for the reply...

      Gotcha, thanks for some more example, much appreciated!

      Yea, normally when I have more then one argument passed to a Sub I'll use the the 1st method you
      showed in your reply.

      sub myFunction { my ($first, $second, $third) = @_; .....code..... }

      I know its a slightly different topic, but when I pass more then one Argument to the script itself,
      like from the CLI, I like to use the Getopt::Long Module, it works great and does most of the work for you...
      use Getopt::Long; my $HELP; my $HOSTNAME; if (@ARGV > 0) { GetOptions('help|?' => \$HELP, 'host|hostname|H=s' => \$HOSTNAME ); }

      Thanks again for the examples!


      Thanks,
      Matt

Re: User-Defined Sub, Passing a Single Argument?
by mmartin (Monk) on Oct 15, 2014 at 17:55 UTC
    Hey Kenneth and duelafn, thanks for the replies!

    Yea, sorry about that I did have parenthesis around that one example, I just forgot to include them when I wrote out
    the example. And I did accidentally use it without them when I was testing and got a '1', since I only passed 1 Arg...

    Ok, well its good to know that it's more of a personal taste sort of thing, so thanks for the info. It is much
    appreciated... Thanks!

    I kind of like the 'shift' method myself... Easy to remember.

    Thanks Again,
    Matt
Re: User-Defined Sub, Passing a Single Argument?
by aitap (Curate) on Oct 18, 2014 at 21:59 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found