Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Passing named parameters

by jcwren (Prior)
on May 02, 2000 at 03:54 UTC ( [id://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 line 13."

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

Replies are listed 'Best First'.
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 ./ li +ne 17. Unquoted string "none" may clash with future reserved word at ./test.p +l line 17. Execution of ./ aborted due to compilation errors.
RE: Passing named parameters
by ChOas (Curate) on May 02, 2000 at 10:50 UTC

    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 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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-05-25 15:32 GMT
Find Nodes?
    Voting Booth?

    No recent polls found