Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Argument Passing, preference or performance ?

by Anonymous Monk
on Nov 11, 2002 at 22:44 UTC ( [id://212129]=note: print w/replies, xml ) Need Help??


in reply to Argument Passing, preference or performance ?

Logically they are the same, and even the gracefulness are the same. However there is one situation, I would suggest to use @_ instead of shift, to provide the elegance. That's the time you pass named parms, for example:
sub func { %parms = @_; foreach (keys %parms) { print "parms[$_] = $parms{$_}\n"; } } func("a" => 1, "b" => 2, "c" => 3);
Well, you can use shift, but in this case, a little bit tasteless, is it?

Replies are listed 'Best First'.
(tye)Re: Named Argument Passing
by tye (Sage) on Nov 11, 2002 at 23:16 UTC

    I disagree. I prefer passing named arguments as a hash reference:

    sub Open { my( $file, $opts )= @_; foreach $key ( %$opts ) { # ... } } Open( "file", { ReadOnly=>1, Retries=>2 } );
    for the following reasons:
    • It leaves lots of room for flexability in the API. For example, if my routine requires a 'file' parameter, then I don't see much point in requiring you to type File => when I can do things as above. You can even have optional parameters:
      sub Open { my $opts= UNIVERSAL::isa( $_[-1], "HASH" ) ? pop(@_) : {}; my( $file, $mode, $mask )= @_; # ... } Open( "file" ); Open( "file", { Exclusive=> 1 } ); Open( "file", "w", 0666, { Locked=>1 } );
    • The "Odd number of elements in hash assignment" warning automatically gets thrown at the calling code where it belongs
    • It provides a visual cue that named arguments are being used
    • It stops you from getting tricky and doing:     Create( Name=>"This", Users=>'fred','bob','joe' ); which you will regret later. Instead you are forced to:     Create( Name=>"This", Users=>['fred','bob','joe'] );
    But you need to be careful to not have your function modify the hash. (:

            - tye
Re: Re: Argument Passing, preference or performance ?
by kshay (Beadle) on Nov 12, 2002 at 00:09 UTC
    I'm not sure I agree that the gracefulness is the same. One situation that comes to mind is when you want to populate the elements of a hash with the passed arguments:
    sub add_person { my %person = (); @person{qw(firstname lastname addr1 addr2 city state zip)} = @_; ... }

    vs.

    sub add_person { my %person = (); $person{firstname} = shift; $person{lastname} = shift; $person{addr1} = shift; $person{addr2} = shift; $person{city} = shift; $person{state} = shift; $person{zip} = shift; ... }

    The first one just seems a lot cleaner to me. (Of course, in practice you might have the list of field names in a predefined array that you could loop through, but you get the idea.)

    --Kevin

Re: Re: Argument Passing, preference or performance ?
by seattlejohn (Deacon) on Nov 12, 2002 at 03:58 UTC
    Logically they are the same

    Unfortunately not. routine1 makes the assignment without modifying @_, whereas routine2 consumes elements of @_. Add something like return $_[0]; to each of the above and in general you will get different results...

            $perlmonks{seattlejohn} = 'John Clyman';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found