orthanc has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: What Is Going On Here ?????
by autark (Friar) on Nov 21, 2000 at 22:03 UTC | |
Re: What Is Going On Here ?????
by Jonathan (Curate) on Nov 21, 2000 at 15:42 UTC | |
by orthanc (Monk) on Nov 21, 2000 at 15:47 UTC | |
by repson (Chaplain) on Nov 21, 2000 at 16:12 UTC | |
by extremely (Priest) on Nov 22, 2000 at 05:02 UTC | |
by Jonathan (Curate) on Nov 21, 2000 at 17:21 UTC | |
by Fastolfe (Vicar) on Nov 21, 2000 at 19:17 UTC | |
Re: What Is Going On Here ?????
by Dominus (Parson) on Nov 21, 2000 at 22:23 UTC | |
by tye (Sage) on Nov 21, 2000 at 22:35 UTC | |
Re: What Is Going On Here ?????
by dws (Chancellor) on Nov 21, 2000 at 22:57 UTC | |
by orthanc (Monk) on Nov 22, 2000 at 14:25 UTC | |
Re: What Is Going On Here ?????
by Anonymous Monk on Nov 23, 2000 at 09:30 UTC |