Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Passing by reference from within a sub

by RichardK (Parson)
on Jul 15, 2015 at 12:16 UTC ( [id://1134869]=note: print w/replies, xml ) Need Help??


in reply to Passing by reference from within a sub

Isn't this just an ordering issue ?

The compiler hasn't seen the definition for sub_c when it compiles sub_b, but it has by the time it gets to the stand alone call of sub_c, hence the differing behaviour. So if you swap the order of your functions it will do what you want.

use strict; use Data::Dumper; sub sub_c (\%) { my ($hash1c) = @_; print "sub_c arguments:" . Dumper (@_); } sub sub_b () { my %hash1b = (); $hash1b{"key1"} = "1"; print "calling sub_c from sub_b\n"; sub_c (%hash1b); } my %hash1a = (); $hash1a{"key1"} = "1"; print "calling sub_b\n"; sub_b (); print "\n"; print "calling sub_c directly\n"; sub_c (%hash1a);

Replies are listed 'Best First'.
Re^2: Passing by reference from within a sub
by arvid (Initiate) on Jul 15, 2015 at 12:25 UTC
    Thank you Richard for saving my sanity. It works like expected now.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found