Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can I safely return a reference from a subroutine?

by btrott (Parson)
on Jun 21, 2000 at 02:13 UTC ( [id://19122]=note: print w/replies, xml ) Need Help??


in reply to Can I safely return a reference from a subroutine?

No, that's not correct. If you pass back a reference, the original data structure won't be destroyed, even though you've left the subroutine.

This is because of the way Perl's garbage collection is implemented: it uses reference counting, which means that as long as there's a reference to a particular piece of data, that data won't be destroyed. Once the reference count drops to 0, the original data will be garbage collected.

When you return a reference, the original reference to the data (the one created in the sub) goes away, but you grab a new reference to the data. So the reference count doesn't fall to 0, so the data isn't destroyed until your new reference goes away.

Does this make sense? Look in perlref for more details on references.

  • Comment on Re: Can I safely return a reference from a subroutine?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found