in reply to "my $filehandle" scoping strange problem

B::Deparse is your friend in strange syntax corners.

$ perl -MO=Deparse,-p,-q # . . . your first example here ^D unless (open(my $fh, '<f1.txt')) { do { print(STDERR "can't open <f1.txt, but nevermind, I'll try f2.t +xt\n"); (open($fh, '<f2.txt') or die(q[still can't...])); }; } print(<$fh>);

So you can see now that $fh is scoped to the unless block that's been implicitly created.

Update: Not that that's not a little wierd, mind you. After looking at it a second time it's slightly surprising.

Replies are listed 'Best First'.
Re^2: "my $filehandle" scoping strange problem
by vkon (Curate) on Apr 14, 2006 at 12:56 UTC
    Given, as you said, $fh is scoped to the unless block, why the almighty DWIMery allows to use $fh afterwards within a scope of use strict;??

      It doesn't. Add a -Mstrict or use strict; and it complains.

      $ perl -MO=Deparse,-p,-q use strict; 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>; ^D Global symbol "$fh" requires explicit package name at - line 5. - had compilation errors. use strict 'refs'; unless (open(my $fh, '<f1.txt')) { do { print(STDERR "can't open <f1.txt, but nevermind, I'll try f2.t +xt\n"); (open(${'fh'}, '<f2.txt') or die(q[still can't...])); }; } print(<$fh>);
        Thanks.

        Actually my problem was due to yet another $fh in outer block.
        My larger script, which uses strict (let strict's days will be countless:) contained another declaration of $fh, so within open my $fh ...... outer $fh come into play .....

        Problem is solved...