Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

multiple passes by reference

by smgfc (Monk)
on Feb 17, 2002 at 04:31 UTC ( [id://145953]=perlquestion: print w/replies, xml ) Need Help??

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

I am not sure if this should go in Q&A or here, but... anyway...if i have one subroutine that calls another subroutine, should i pass a value to sub one by reference, and then to sub two by reference again, or should i pass a value to sub one by reference and then to sub two straight? right now it works passing by reference twice, but it is kinda ugly...
%hash = ( "j" = "jack", "q" = "queen", "k" = "king", "a" = "ace" ); one( \%hash); sub one { my ($hash) = @_; if ( <STDIN> eq "hello\n" ) { two( \%$hash); # should this be two( $hash ); ? } } sub two { my ($hash) = @_; foreach ( keys %$hash ) { print "$_\n"; } }

Replies are listed 'Best First'.
Re: multiple passes by reference
by dws (Chancellor) on Feb 17, 2002 at 04:38 UTC
    if i have one subroutine that calls another subroutine, should i pass a value to sub one by reference, and then to sub two by reference again, or should i pass a value to sub one by reference and then to sub two straight?

    Once you have a reference, it stays a reference until you dereference it. This is a bit clearer if you use a more accurate name for the function arguments.

    Here's trimmed down version of your example, with names changed:

    one(\%hash); sub one { my($hashref) = @_; two($hashref); } sub two { my($hashref) = @_; # it's still a hashref foreach ( keys %$hashref ) { print "$_\n"; } }
Re: multiple passes by reference
by dvergin (Monsignor) on Feb 17, 2002 at 04:35 UTC
    You are right to suspect there is a simple way of saying this.

    $hash gives you the same thing as \%$hash.

    In convoluted English: the hashref will give you effectively the same thing as a reference to the hash pointed to by the hashref.

    Update: Attend to dws, I tried to stick with your var name, but there is a world of grief to be avoided by naming your vars so you really know what you have. $hashref is much more helpful than $hash when you come back to the code a month later.
     

Re: multiple passes by reference
by mojotoad (Monsignor) on Feb 17, 2002 at 05:37 UTC
    dws says most of what needs to be said. The rest of the answer to your question hinges on what you want to do with the data passed from one sub to the next. Is the intent to alter the original data? Does it matter if the data is altered? Is the data guaranteed not to be altered? These questions do not change with the number and scope of cascaded subs passing data to one another. Assuming you have control over the fate of the data then it is generally more efficient to pass refs.

    Based on the phrasing of your original query it sounds like passing the same ref is the way to go since you don't seem to need to work on copies.

    Matt

    (updated for grammatical clarity)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found