in reply to Re: 'used once' during perl -c
in thread 'used once' during perl -c

When I use warnings, why does the 'once' warning only show up during a -c check, not during a regular run? Is the meaning of "use warnings" (with no parameters) different in these two cases?

Replies are listed 'Best First'.
(bbfu) (warning only during -c) Re3: 'used once' during perl -c
by bbfu (Curate) on Nov 02, 2002 at 18:53 UTC

    I'm afriad I'm unable to duplicate that here. I get the warning both when I -c it, and when I run it which, as far I as I know, is the correct behavior. This is with both ActiveState perl, and Cygwin perl.

    Perhaps you can post the code that's causes that behavior? You're not on 5.8.0, are you? Both of my perl's are 5.6.1. *shrug*

    (Output from my test below)

    bbfu
    Black flowers blossum
    Fearless on my breath

      Hmm... a guess: my code is in a .pm file. Perhaps Perl automatically enters another "use" of import when doing a use of a module. Then the issue isn't -c or not, but main-program or not.

        From your /msg, I've been able to put together the following:

        [johnsca@CORY tmp]$ cat tst.pm #!/usr/bin/perl package tst; use warnings; use strict; *import = \&foo; sub foo { print "Import foo\n"; } 1; [johnsca@CORY tmp]$ cat tst.pl #!/usr/bin/perl use warnings; use strict; use lib '.'; use tst; [johnsca@CORY tmp]$ perl -c tst.pm Name "tst::import" used only once: possible typo at tst.pm line 9. tst.pm syntax OK [johnsca@CORY tmp]$ perl -c tst.pl Import foo tst syntax OK [johnsca@CORY tmp]$ perl tst.pm Name "tst::import" used only once: possible typo at tst.pm line 9. [johnsca@CORY tmp]$ perl tst.pl Import foo

        From this, I think makes sense. The warning when running perl -c tst.pm (and perl tst.pm) is appropriate, as import is only used once, when you consider only tst.pm. However, once you throw tst.pl into the mix, you get another (implicit) use of *tst::import via the use, as you said, which silences the warning. This makes sense as, according to the docs for use:

        It is exactly equivalent to
        
            BEGIN { require Module; import Module LIST; }
        
        except that Module must be a bareword.
        

        To prevent the warning when checking tst.pm, you can locally disable warnings, as I suggested before, or you can add another (no-op) reference to *import. In the code below, I use use vars qw/*import/; because our doesn't seem to like typeglobs. :) <Update>Though our $import; seems to get rid of the error just fine.</Update>

        [johnsca@CORY tmp]$ cat tst.pm #!/usr/bin/perl package tst; use warnings; use strict; use vars qw/*import/; # or: *import = *import; *import = \&foo; sub foo { print "Import foo\n"; } 1; [johnsca@CORY tmp]$ perl -c tst.pm tst.pm syntax OK

        bbfu
        Black flowers blossum
        Fearless on my breath