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

Hi monks,
when i execute script with 'use strict' and 'use warnings' I get compilation error stating
'Some Global variables require explicit package name at ... linenumber.'

But if i dont comment strict and warnings the script executes. Can anyone explain me as to what does compilation error states and why doesnt it throw the same errors when i comment use strict n warinings.

Replies are listed 'Best First'.
Re: Error - Global variable ....
by Corion (Patriarch) on May 16, 2006 at 08:24 UTC

    You don't show us a script, but this is what use strict; does - it forces you to predeclare all the variable names you intend to use. This is a good thing because it finds typos like the following:

    use strict; my $variable_with_a_very_long_name = 1; print "The value is >>$varaible_with_a_very_long_name<<";

    As your script seems to work with use strict; commented out, likely your script contains no typos and all you need is to predeclare your global variables:

    # for global variables: use vars qw(@users $account);

    If a variable isn't used in global scope, you want to declare it lexically within the block it is used in via the my keyword:

    for $user (@users) { $message = "Hello $user!"; ... }; # should become for my $user (@users) { my $message = "Hello $user!"; };
Re: Error - Global variable ....
by polettix (Vicar) on May 16, 2006 at 08:31 UTC
    Using strict you enable some compile-time sanity checks that make the compilation fail in various cases. One of these cases is when you use variables without either declaring them with my or via the vars pragma; this lets you easily spot typos in variables names and avoid hours of head-banging on the keyboard (and saves your keyboard as bonus).

    warnings are somewhat less vexating, but produces some notification run-time messages when a weird condition arises. For example, when you print or otherwise "use" an undef variable, it warns you that this is happening, and that it's probably something that you should avoid.

    For a somewhat frustrated dissertation about the whole issue, you can read On Commenting Out 'use strict;'.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Error - Global variable ....
by tinita (Parson) on May 16, 2006 at 13:27 UTC
    Some Global variables require explicit package name
    i don't know which perl version you are using, but the versions i have used in the past have all told me the exact name of the variable in question.

    that said - please copy & paste the exact error message so people can help you better.

Re: Error - Global variable ....
by swampyankee (Parson) on May 16, 2006 at 14:32 UTC

    How to solve your problem:

    • Read the list of errors emitted by Perl
    • Locate the variables which are listed. Perl helps here, as it gives line numbers.
    • Determine the minimum scope required for each variable. As an example, the scope for a loop index need not be more than the loop where it's used, and most variables in functions should have their scope limited to the function in which they're used.
    • Declare each variable with my, our, or local, as appropriate.

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele
Re: Error - Global variable ....
by jesuashok (Curate) on May 16, 2006 at 08:22 UTC
    hi

    could you post your code? so that it is for us to explain the things better.

    "Keep pouring your ideas"