Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I get a reference to a subroutine return value?

by Anonymous Monk
on Mar 02, 2001 at 23:56 UTC ( [id://61865]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to make a subroutine like:

myfunc(return => query->param("return"), authors => query->param("authors"));

(with many more parameters)

But I want those arrays from query->param() to be passed to the function as array references instead, naturally. Is there any way to do so without a bunch of temporary variables?

  • Comment on How do I get a reference to a subroutine return value?

Replies are listed 'Best First'.
Re: How do I get a reference to a subroutine return value?
by japhy (Canon) on Mar 02, 2001 at 23:58 UTC
    Do you mean:
    myfunc( return => [ $query->param('return') ], authors => [ $query->param('authors') ], # ... );
Re: How do I get a reference to a subroutine return value? - or is it "how do I get cgi parameters into a subroutine"
by bjelli (Pilgrim) on Mar 03, 2001 at 03:22 UTC

    I'm not too sure I understood the question, I assume you want to get all the CGI-Paramers into your subroutine like so: myfunc(return => query->param("return"), authors => query->param("authors"));

    The short answer: myfunc(  map { $_ => [$query->param($_)] } $query->param()  );

    the long answer:

    Get a list of all the parameters with $query->param(). This will be fed into map.

    Map can be used to apply some commands to every element in a list, and construct a new list from the result. In this case for every element you feed into the map, you get back two elements: you get back the key, and an anonymouse array with the values.

    that's it -- Brigitte 'I never met a chocolate I didnt like' Jellinek http://www.horus.com/~bjelli/ http://perlwelt.horus.at

(tye)Re: How do I get a reference to a subroutine return value?
by tye (Sage) on Mar 03, 2001 at 11:48 UTC

    If $query is a CGI.pm object, then the best way is probably:

    myfunc( return => $query->param_fetch("return"), authors => $query->param_fetch("authors") );

    (with many more parameters)

    Oh, well, then you probably want this instead:

    myfunc( map { $_ => $query->param_fetch($_) } qw( return authors to their stables immediately ) ) # or myfunc( map { $_ => $query->param_fetch($_) } $query->param() )

    For the more general problem of "How do I get a reference to a subroutine return value?" (which I thought was an interesting question so I did some investigating), the answer is "you can't". You can get a reference to copies of subroutine return values via:

    $sv= \sub_returning_scalar(); $av= [sub_returning_list()]; $hv= {sub_returning_hash()};

    So if you follow the advice of those who answered before me but part of your unstated goal was to allow the subroutine to modify the parameters such that $query would notice the modifications, then you will fail because those array references will only allow you to modify copies of those values.

    The methods I suggested will allow myfunc to modify the CGI parameters directly. This can certainly be a disadvantage if you wanted myfunc to be able to futz with those arrays without affecting the CGI $query object.

            - tye (but my friends call me "Tye")
Re: How do I get a reference to a subroutine return value?
by steveAZ98 (Monk) on Mar 03, 2001 at 09:12 UTC
    I'm not sure I understand the question either, but if you would like those params in your sub, why don't you pass the query object to the sub?
    myfunc($query); sub myfunc { my $query = shift; }
    If you need the param values in an array ref to pass to the sub then you can use this
    my @values = map { $q->param($_) } $q->param; myfunc(\@values);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://61865]
Approved by root
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: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found