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

I'm just starting to use Fatal but it seems to cause "used only once" warnings.

The use of a glob name as a parameter to a Fatal overriden function doesn't count for some reason. In the code below, "open A, ..." isn't counted.
What is going on?

[11:34 bsb]$ perl -wl -e 'open A, "date|"; print scalar <A>' Tue Mar 25 11:34:27 EST 2003 [11:34 bsb]$ perl -wl -MFatal=open -e 'open A, "date|"; print scalar < +A>' Name "main::A" used only once: possible typo at -e line 1. Tue Mar 25 11:34:36 EST 2003 [11:34 bsb]$ perl -wl -MFatal=open -e 'open A, "date|"; print readline + A' Tue Mar 25 11:34:45 EST 2003

Replies are listed 'Best First'.
Re: Fatal + <FILE> = only once warning (deparsed)
by Aristotle (Chancellor) on Mar 25, 2003 at 01:37 UTC
    Let's feed the code to the deparser and find out what Perl sees..
    $ perl -MO=Deparse -wl -MFatal=open -e 'open A, "date|"; print scalar +<A>' Name "main::A" used only once: possible typo at -e line 1. # .. # lots of imported stuff from Fatal # .. &open('A', 'date|'); print scalar <A>; -e syntax OK

    As you can see the A in the open call has become a string. I guess the builtin's prototype cannot be exactly duplicated by Fatal, leading to the filehandle being passed it by symbolic reference rather than globname. Of course perl can't know that that string is a symref to the handle at compile time.

    The simplest fix is also a good habit: use lexical variables to hold your filehandles.

    $ perl -wl -MFatal=open -e 'open my $pipe, "date|"; print scalar <$pip +e>' Tue Mar 25 02:37:32 CET 2003
    This also makes Perl automatically clean up filehandles that go out of scope behind you, and you don't have to be careful not to step on anyone's toes with the chosen filehandle name either.

    Makeshifts last the longest.

Re: Fatal + <FILE> = only once warning
by pg (Canon) on Mar 25, 2003 at 02:09 UTC
    That's quite alright, because your call to open() has been replaced by the following code, and that symbol "A" is no longer there. So, yes, that "A" in open() call is not counted.

    sub (*;$@) { local($", $!) = (', ', 0); if (@_ == 1) { return CORE::open($_[0]) || croak "Can't open(@_): $!"; } elsif (@_ == 2) { return CORE::open($_[0], $_[1]) || croak "Can't open(@_): $!"; } elsif (@_ == 3) { return CORE::open($_[0], $_[1], @_[2..$#_]) || croak "Can't op +en(@_): $!"; } die "open(@_): Do not expect to get ", scalar @_, " arguments"; }