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
In reply to my, scope, and references by chayashida
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |