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

Consider this simple perl program:

#!/usr/bin/perl -w sub handle_error { if (open LOG, ">/dev/null") { carpout(LOG); } } print "hello world.\n";

The intent is to display a very small program to illustrate a problem with a much larger one (whose purpose and design is irrelevant at this point). The problem is, running the program yields a warning from perl:

Name "main::LOG" used only once: possible typo at bar.pl line 4.

The context of LOG's use seems clear that it's a parameter to another function, so it's not "used once." ...at least, not in a way that should warn the user. Now, if perl is going to follow that function to see what its doing with that parameter and seeing that nothing is done, fine. But it isn't, so isn't the warning unnecessary? (If you introduce an actual compile-time error, the warning goes away entirely.)

Replies are listed 'Best First'.
Re: unnecessary warning message?
by Animator (Hermit) on May 06, 2005 at 20:04 UTC

    Take a look at perlfaq7 . It has a question about it... (how can I pass/return a ...)

    To pass a filehandle you should use *FH or \*FH, not FH

    Also, if you would have 'use strict': then you would have gotten an error: 'Bareword "LOG" not allowed while "strict subs" in use'...

      Also, if you would have 'use strict': then you would have gotten an error: 'Bareword "LOG" not allowed while "strict subs" in use'...
      Just wanted to note that prototyping carpout to expect a filehandle name fixes the bareword error, but not the "used only once" warning. IMO this could be construed as a bug.
      $ cat carpout.pl; perl carpout.pl #!/usr/bin/perl -w use strict; sub carpout(*); sub handle_error { if (open LOG, ">/dev/null") { carpout(LOG); } } print "hello world.\n"; Name "main::LOG" used only once: possible typo at carpout.pl line 6. hello world.
Re: unnecessary warning message?
by thundergnat (Deacon) on May 07, 2005 at 14:12 UTC

    Using lexical filehandles will avoid those warnings.

    #!/usr/bin/perl -w sub handle_error { if (open my $log, ">/dev/null") { carpout($log); } } print "hello world.\n";