Hi Monks
Can anyone explain why the code below destroys the contents of @test_array?
All I can see is that we are simply reading from @test_array and never editing it, if the while statement in the subroutine is changed to a
foreach $line (<IN>)
@test_array remains unaffected.
I'm baffled, should the $_ variable being used for the file not be in a different scope to @test_array?
The size of @test_array remains unaffected but each element is empty, thus the warnings for uninitialised variables.
please say its not something blatantly obvious!!!
Orthanc
#!/usr/local/bin/perl -w my @test_array=( 'Element1', 'Element2', 'Element3', 'Element4', 'Element5', ); foreach (@test_array) { print "\"$_\"\n"; do_something(); } print "\n"; foreach (@test_array) { print "\"$_\"\n"; } sub do_something { open(IN, "$0") || die("No file"); while (<IN>) { # Do nothing, in the real software we would do something # with the content. } close(IN); }
In reply to What Is Going On Here ????? by orthanc
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |