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

Passing named parameters

by jcwren (Prior)
on May 02, 2000 at 03:54 UTC ( #9866=perlquestion: print w/ replies, xml ) Need Help??
jcwren has asked for the wisdom of the Perl Monks concerning the following question:

I am off on a jobsite without my trusty Perl CD Bookshelf.

I need(want) to pass named parameters to a subroutine, ala CGI..

do_this_cool_function (-retire=>'early', -payscale=>-55.00, -pizza=>'cold', -beer=>none);

I'm using 'use strict' and '-w'. The compiler throws up on my shoes with
"Can't use string ("-spot") as a HASH ref while "strict refs" in use at t.pl line 13."

A) What do I need to do remove this error,
B) How do I access the named arguments in the subroutine?.

Comment on Passing named parameters
Re: Passing named parameters
by snowcrash (Friar) on May 02, 2000 at 10:45 UTC
    Access your argument in the subroutine like this:
    #!/usr/bin/perl -w use strict; sub do_this_cool_function { my %args = ( -retire => "never", -pizza => "cheese", -payscale => 0, -beer => "guiness", @_, # argument pair list goes here ); print "I wanna retire $args{-retire} if my pizza is $args{-pizza}. +\n"; } do_this_cool_function(-retire=>'early', -payscale=>-55.00, -pizza=>'co +ld', -beer=>'none');
    i don't get your error by the way. the only error your function call gives, is the following, because you didn't quote 'none':
    Bareword "none" not allowed while "strict subs" in use at ./test.pl li +ne 17. Unquoted string "none" may clash with future reserved word at ./test.p +l line 17. Execution of ./test.pl aborted due to compilation errors.
RE: Passing named parameters
by ChOas (Curate) on May 02, 2000 at 10:50 UTC
    Hi!

    Eeehm you might want to put ' thingies around your
    keys aswel that hash, that might help

    and for B:
    I'd think this'd work:
    sub do_this_cool_function(@)
    {
    my %Param=@_;
    # Now address your params like this:
    my $When=$Param{'-retire'};
    my $Wages=$Param('-payscale'};
    my $Favourite_temperature=$Param{'-pizza'};

    }
    That's how I'd do it.
      No, this is proper syntax: do_this_cool_function (-retire=>'early', -payscale=>-55.00, -pizza=>'cold', -beer=>none); This should work fine as in many modules like CGI.pm. The leading '-' in -retire will perl to interpret '-retire' as a string. Here is a snip from perlop
      Unary "-" performs arithmetic negation if the operand is numeric. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to "-bareword".
        Which is why -beer isn't a bareword. But the => is equivalent to a , (comma) thus ending the bareword, er string. So then
            -beer, none
        will result in none being a bareword. At least, that's how I understand it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-08-29 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls