perl

How can I get perl's strict warnings to recognize switches?


I have this perl script that uses switches and strict warnings.

#! /usr/bin/perl -s
use strict;
$some = $some . " more";
print "got $some\n";

When I run it at the command line I get these errors.

$ ./strictTest.pl -some=SOME
Variable "$some" is not imported at ./strictTest.pl line 3.
Variable "$some" is not imported at ./strictTest.pl line 3.
Variable "$some" is not imported at ./strictTest.pl line 4.
Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at ./strictTest.pl line 3.
Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at ./strictTest.pl line 3.
Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at ./strictTest.pl line 4.
Execution of ./strictTest.pl aborted due to compilation errors.

What can I do to get perl to recognize that $some is defined because it's passed as a switch?


Solution

  • No matter the source of the variables, you need to satisfy strict in the same way. Perl needs you to tell it that you intend to use the variable:

    If the error messages don't make much sense to you, substitute diagnostics for warnings to get longer explanations:

    Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 4.
    Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 4.
    Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 5.
    Execution of /Users/brian/Desktop/test.pl aborted due to compilation errors (#1)
        (F) You've said "use strict" or "use strict vars", which indicates
        that all variables must either be lexically scoped (using "my" or "state"),
        declared beforehand using "our", or explicitly qualified to say
        which package the global variable is in (using "::").
    
    Uncaught exception from user code:
        Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 4.
        Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 4.
        Global symbol "$some" requires explicit package name (did you forget to declare "my $some"?) at /Users/brian/Desktop/test.pl line 5.
        Execution of /Users/brian/Desktop/test.pl aborted due to compilation errors.