Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Why does Perl allow you to return array references to variables in local scope?

by MrSnrub (Sexton)
on Oct 16, 2013 at 11:12 UTC ( #1058423=perlquestion: print w/ replies, xml ) Need Help??
MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:

sub DoSomething { my @arr; push @arr, "foo"; return \@arr; }
How am I able to access "foo" after calling DoSomething() in main if I am only returning a reference? Shouldn't the values in the array be out of scope at that point?

Comment on Why does Perl allow you to return array references to variables in local scope?
Download Code
Re: Why does Perl allow you to return array references to variables in local scope?
by Corion (Pope) on Oct 16, 2013 at 11:14 UTC

    No. Perl is not C. The values are still in scope. This is a feature of Perl, enabled by using reference counting for values. Perl manages your memory for you so you don't need to allocate and free the memory manually.

    The thing you're looking for is "references". If you come from a C background, "pointers" are a concept mostly similar, except where they differ. See References Quick Reference on how to work with references.

Re: Why does Perl allow you to return array references to variables in local scope?
by parv (Priest) on Oct 16, 2013 at 11:16 UTC
Re: Why does Perl allow you to return array references to variables in local scope?
by moritz (Cardinal) on Oct 16, 2013 at 12:20 UTC
Re: Why does Perl allow you to return array references to variables in local scope?
by talexb (Canon) on Oct 16, 2013 at 12:26 UTC

    You've hit on one of the improvements that Perl makes over C (and I've developed in both languages for some time).

    In C, that code is meaningless, because the allocated address is on the stack, and that address disappears as soon as you return from the routine. But, as wise brother moritz has pointed out, in Perl, this allocation comes from the heap and not the stack, thus it's still available when the routine returns.

    And this is a very useful feature. In C, you'd have to do something convoluted like assign some memory from the heap, pass that address into the routine, and have the routine write to the address that's been passed in, trusting that there's no buffer overrun. Ugh.

    Perl just does it better! :)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Why does Perl allow you to return array references to variables in local scope?
by BrowserUk (Pope) on Oct 16, 2013 at 12:34 UTC

    I think the easiest way to think of it is that: names have scope; data lives as long as it is referenced.

    So whilst the name @arr has gone out of scope; the data that it labeled whilst in-scope, continues to persist, because a reference to it was returned to the caller.

    Hence, if you call DoSomething() in a void context - ie. you don't assign the return reference to a name in the calling scope -- then the returned reference will be discarded, and the data it references -- formerly known as @arr -- will no longer be referenced and so will be returned to the process memory pool for reallocation.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Why does Perl allow you to return array references to variables in local scope?
by Anonymous Monk on Oct 16, 2013 at 13:38 UTC

    A good way to think of it is that, in Perl, everything is "a reference-counted Thing," allocated from the heap not from a stack. Anything with a reference-count of zero is eligible for cleanup, although it doesn't necessarily happen right away. When any variable anywhere refers to that Thing, it adds one to the count. When the value changes, the counts are adjusted.

    These "things" can be of many intrinsic types .. string, float, integer, file .. or a truly-interesting type: "a reference to something else." In other words, indirection, but without the silly punctuation-marks that a language like C demands. The presence of a reference to any Thing also adds one to the count.

    The only thing that you've got to watch-out for in that system is circular references, which of course would keep the counts from ever becoming zero hence the memory would never be freed. Perl has a "weakened reference" feature (Scalar::Util) to deal with that.

    It is fast, it is efficient, and it works great. You really get used to it very quickly.

Re: Why does Perl allow you to return array references to variables in local scope?
by toolic (Chancellor) on Oct 16, 2013 at 13:38 UTC
    How am I able to access "foo" after calling DoSomething() in main
    use warnings; use strict; my $aref = DoSomething(); print "$aref->[0]\n"; sub DoSomething { my @arr; push @arr, "foo"; return \@arr; } __END__ foo
Re: Why does Perl allow you to return array references to variables in local scope?
by Anonymous Monk on Oct 16, 2013 at 13:45 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1058423]
Approved by hdb
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-10-31 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls