chayashida has asked for the wisdom of the Perl Monks concerning the following question:
I need help with my and scope. Management asked I audit someone else's code, and I came across what I thought was a problem with a variable going out of scope.
I wrote the following as a test, and to my surprise, it ran without errors! Now I'm really confused.
use strict; use warnings; sub scope_test() { my @test_array = (1, 2, 3, 4, 5, 6); return \@test_array; } print join ("\n", @{ scope_test() } );
I thought that @test_array was local to the function, so it would need to copy the data before returning a value. I thought that returning a reference to that variable would cause a problem, but it doesn't.
What's going on here? Am I crazy? Am I biased because C/C++ behaves differently with scope? Or am I complete wrong about scope in the first place, and it works this way in both Perl and C/C++? There's no style guide here, and sometimes I have a problem differentiating between "different from how I'd do it" and "wrong."
Thanks in advance for any help.
Edit: Updated the title for better search results later
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: my and scope
by LanX (Saint) on Mar 28, 2013 at 20:59 UTC | |
by chayashida (Acolyte) on Mar 28, 2013 at 21:21 UTC | |
by davido (Cardinal) on Mar 28, 2013 at 22:06 UTC | |
by LanX (Saint) on Mar 28, 2013 at 22:14 UTC | |
by davido (Cardinal) on Mar 28, 2013 at 22:43 UTC | |
|
Re: my and scope
by McA (Priest) on Mar 28, 2013 at 21:05 UTC | |
by chayashida (Acolyte) on Mar 28, 2013 at 21:25 UTC | |
|
Re: my and scope
by chromatic (Archbishop) on Mar 28, 2013 at 22:15 UTC | |
|
Re: my and scope
by Anonymous Monk on Mar 28, 2013 at 23:49 UTC | |
|
Re: my and scope
by Anonymous Monk on Mar 28, 2013 at 20:59 UTC |