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

Re: Perl's warts

by Petruchio (Vicar)
on Mar 21, 2001 at 22:30 UTC ( [id://66088]=note: print w/replies, xml ) Need Help??


in reply to Perl's warts

Second, what if you wanted to pass an arbitrary or unordered list of parameters to a subroutine? Perhaps you sometimes want to supply a particular parameter on some subroutine calls, but not others. Ideally, you should be able to pass key value pairs to a subroutine, so that you can pass arbitrary value lists and assign their variable names at the same time.

This is when you say:

my %parameters = @_;

which allows you to call your subroutine "ideally":

foo( color => 'blue', size => 'really big' );

Apparently the author is not familiar with this idiom, though it's pretty common.

Update

The author does indeed say,

Sure, one could pass a reference to a hash with these key value pairs

But that is not what I've done. I've passed an ordinary list, not a reference to an anonymous hash. That would be:

foo ( {color => 'blue', size => 'really big'} );

which would require the subrountine to say something like:

my %parameters = %{+shift};

Replies are listed 'Best First'.
Re: Re: Perl's warts
by grinder (Bishop) on Mar 21, 2001 at 23:19 UTC
    Apparently the author is not familiar with this idiom, though it's pretty common.

    He does say

    Sure, one could pass a reference to a hash with these key value pairs, and then decode the hash inside the subroutine. Again, this is asking the programmer to do what the language should intrinsically understand: in essence, to code around the language.

    I missed this the first time I read the article. I'm kind of ambivalent about this way of thinking. I accept the argument as valid, but on the other hand it's not a whole lot of overhead (read: not much syntactic sugar required). The cookbook shows a number of compact techniques for doing this. Although when writing OO Perl, it really is a drag to have to write my $self = shift every single time.

    What I have noticed with Perl is that I figure out very few tricks myself from first principles. Just about all of my mastery has come from reading books and other people's code. The only other language that I have observed this is C++. If you haven't read the Gang of Four book, and studied how the STL works, you'll only ever skim the surface.

    --
    g r i n d e r

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found