smitz has asked for the wisdom of the Perl Monks concerning the following question:

Why oh why does this not barf:
use strict; open (FOO, "whatever.file"); print BAR "Where is this going???"; close FOO;

I thought use strict prevented just such mishaps?

V. confused SMiTZ

Replies are listed 'Best First'.
Re: strict filehandles?
by danichka (Hermit) on Apr 11, 2002 at 14:59 UTC
    I would look over here on this one. Other people have already answered this much better than I can.


    use Your::Head;
Re: strict filehandles?
by Juerd (Abbot) on Apr 11, 2002 at 15:44 UTC

    danichka has already pointed out the thread I was about to link to, so I won't to that.

    To summarize: strict does not protect filehandles, but you can use undefined scalars, that will be made references to anonymous globs.

    use strict; open my $foo, 'whatever.file' or die $!; print $bar "Where is this going???"; close $foo;
    That will barf. By the way, the "Where is this going???" isn't going anywhere in your example. Had you used warnings, the "print() on unopened filehandle" warning would have been emitted. And because print returns true on success and false on failure, you could have tried:
    print BAR "Where is this going???" or die $!;
    Which would die "Bad file descriptor".

    Yes, I reinvent wheels.
    

Re: strict filehandles?
by mrbbking (Hermit) on Apr 11, 2002 at 17:07 UTC
    Why oh why does this not barf
    Because you did not use -w.
    [localhost:~/Desktop] bbking% perl ./yourprog.pl [localhost:~/Desktop] bbking% perl -w ./yourprog.pl Name "main::BAR" used only once: possible typo at ./temp.pl line 5. Filehandle main::BAR never opened at ./temp.pl line 5. [localhost:~/Desktop] bbking%
    HTH --
    s!!password!;y?sordid?binger?; y.paw.mrk.;;print chr 0x5b ;;; print;print chr(0x5b+0x2);;;;;