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

Hello all... I'm trying to bang out a simple script to create directories. I'm getting a warning message that I have never seen, and have had little success when searching Google and other sources.

The warning I get is "Name main::INPUT used only once: possible typo." This occurs on the line with the open statement below:

use strict; my $filename; print "Enter a filename(with full path) to search.\n"; chomp($filename=<STDIN>); open(INPUT, '$filename') or die ("Can't open '$filename' for reading: $!\n");
I curious as to what may be causing this warning, and how I might eliminate it? Any help would be greatly appreciated.
  • Comment on Strange warning message regarding filehandle in an "open" statement
  • Download Code

Replies are listed 'Best First'.
Re: Strange warning message regarding filehandle in an "open" statement
by seaver (Pilgrim) on Sep 24, 2004 at 21:09 UTC
    The error message you're getting is just a warning

    I can't tell if your code is complete, or if you're just posting the beginning of it. But the warning is simply saying you're only using 'INPUT' once, meaning you've created it, but are not accessing it.

    If you are trying to access INPUT later on in the code, then there must be a typo, just as the warning states, because it's not being accessed after all

    Cheers
    Sam

Re: Strange warning message regarding filehandle in an "open" statement
by punkish (Priest) on Sep 24, 2004 at 21:10 UTC
    instead of open(INPUT, '$filename') try open(INPUT, "$filename") The double-quotes coax perl to figure out the value of the variable inside the quotes instead of treating it as a literal text string.

    Read more on variable interpolation.

      gah, while open(INPUT, "$filename") is "correct", just use open(INPUT, $filename).

      Better yet, use open(INPUT, '<', $filename) to safely handle inputs of the form >bla or rm -rf / |.

        ikegami's advice is correct. jtgault, Just do away with the quotes.

        Other monks have also advised correctly. The error is precisely because jtgault has declared INPUT via the open command but not used INPUT anywhere further in your program. Do something with that INPUT and that warning will go away.

        And, finally, my advise is also correct (albeit a bit tangential) -- if, jtgault, you use single quotes, you will not be able to open your file, however, if you use double quotes, or as ikegami is suggesting, then you will be successful.

        Update: added "jtgault" so it doesn't seem like I am correcting/advising ikegami when actually I advising jtgault. ;-)

Re: Strange warning message regarding filehandle in an "open" statement
by TrekNoid (Pilgrim) on Sep 24, 2004 at 21:13 UTC
    perl is warning you that you've created this file handle, INPUT, but you're not using it anywhere else in the program.

    Usually, that's because you've mistyped the handle, but it's more likely happening for you because you haven't written any code to read the file you just opened the handle to yet.

    Trek

      Thanks to all who replied, I'll give your suggestions a try.

      jt

Re: Strange warning message regarding filehandle in an "open" statement
by ysth (Canon) on Sep 26, 2004 at 07:19 UTC
    If you ever wonder what a perl error or warning message means, just add "use diagnostics;" (or manually look in perldoc perldiag). For this one, it shows:
    (W once) Typographical errors often show up as unique variable nam +es. If you had a good reason for having a unique name, then just menti +on it again somehow to suppress the message. The our declaration is provided for this purpose. NOTE: This warning detects symbols that have been used only once s +o $c, @c, %c, *c, &c, sub c{}, c(), and c (the filehandle or format) are con +sidered the same; if a program uses $c only once but also uses any of the +others it will not trigger this warning.
    The (W once) means its a warning, and no warnings 'once'; will turn it off (and any other warnings in the once category) for a given scope.

    Unfortunately our won't work with filehandles; if it really isn't mistyped, you can also say use vars "*INPUT";

Re: Strange warning message regarding filehandle in an "open" statement
by ambrus (Abbot) on Sep 26, 2004 at 11:33 UTC

    You eliminate it my using a lexical filehandle (if you're using perl>=5.6):

    open (my $INPUT, $filename) or die ("Can't open '$filename' for reading: $!\n");