vkon has asked for the wisdom of the Perl Monks concerning the following question:
Following script: (use strict; is always on)
Last print statement is in a problem: even if f2.txt exists, I see the mentioned warning, but why wasn't failure at nearby open statement ??open my $fh, "<f1.txt" or do { print STDERR "can't open <f1.txt, but nevermind, I'll try f2.txt\n +"; open $fh, "<f2.txt" or die "still can't..."; }; print <$fh>; # this gives a warning "read on closed filehandle";
Things work different if I move declaration of $fh above:
How this strange scoping explained?my $fh; open $fh, "<f1.txt" or do { print STDERR "can't open <f1.txt, but nevermind, I'll try f2.txt\n +"; open $fh, "<f2.txt" or die "still can't..."; }; print <$fh>; # I see the content of f2.txt here ...
In my currently non-enlightened view, DWIMery is broken.
Can anyone restore DWIMery, please? :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: "my $filehandle" scoping strange problem
by Fletch (Bishop) on Apr 14, 2006 at 12:33 UTC | |
by vkon (Curate) on Apr 14, 2006 at 12:56 UTC | |
by Fletch (Bishop) on Apr 14, 2006 at 13:30 UTC | |
by vkon (Curate) on Apr 14, 2006 at 13:54 UTC |