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

i have a odd code. code is here.
use strict; # you must do declare vars before you use it. local $var; $var = "perl is"; print "$var";
and error is:
Global symbol "$var" requires explicit package name at - line 3. Global symbol "$var" requires explicit package name at - line 4. Global symbol "$var" requires explicit package name at - line 5. Execution of - aborted due to compilation errors.
what happen?

Replies are listed 'Best First'.
Re: Is local a declaration?
by rob_au (Abbot) on Apr 10, 2002 at 13:37 UTC
Re: Is local a declaration?
by broquaint (Abbot) on Apr 10, 2002 at 14:31 UTC
    The local() function will only work on existing package variables. The reason being is that it localises the given variable(s) to the current scope e.g
    $foo = "main scope"; { local $foo = "temp scope"; print "\$foo is $foo\n"; } print "\$foo now $foo\n"; __output__ $foo is temp scope $foo now main scope
    This is handy for some of perl's cruftier globals such as $/ and $".
    HTH

    broquaint

Re: Is local a declaration?
by dsb (Chaplain) on Apr 10, 2002 at 15:58 UTC
    When you are using the strict pragma, Perl is, in a sense, enforcing the use of fully qualified variable names. All variables belong to a package, if even only the main package. So when you use strict and initialize a variable with <text>my</text>, essentially Perl is identifying that variable as part of the current package(like $package::var - NOTE - WHen no package is explicitly declared, the package is 'main').

    So:

    #!/usr/bin/perl package Test; use strict; my $var;
    UPDATE: I originally posted that my $var was equivalent to $Test::var. That is incorrect however, I've since learned that lexical variables are not package variables at all. I offer as proof:
    package Amel; use strict; $Amel::var = "Dan"; my $var = "Balaban"; print $Amel::var, " $var\n";
    'local' on the other hand does not actually initialize variables, and certainly does nothing to fully qualify the variable name with a package.

    As far as the error goes:

    Global symbol "$var" requires explicit package name at - line 3.

    When Perl says 'explicit package name', technically you could fix your problem my preceding all of your variables with the name of the current package instead of using 'my', but you'd have to do that every time you use the variable...blech!

    But just so you understand:

    #!/usr/bin/perl use strict; $main::var = "string"; print $main::var, "\n";

    Amel - f.k.a. - kel

Re: Is local a declaration?
by talexb (Chancellor) on Apr 10, 2002 at 18:05 UTC
    Is local a declaration?

    Nope, local is a more like a redeclaration for a variable that already exists.

    --t. alex

    "Here's the chocolates, and here's the flowers. Now how 'bout it, widder hen, will ya marry me?" --Foghorn Leghorn

      I'd probably just add to that to say "... a redeclaration of more narrow scope for a variable that already exists

      :-)

       

Re: Is local a declaration?
by helmex (Initiate) on Apr 10, 2002 at 14:22 UTC
    I dido the other commet. Local is not the same as 'my'.
    The way I understand it, 'my' is more for initializing, while local is more of a scope property.
    When using strict you must declare all variables with 'my'.
    If there are certain variables you do not want to initialize you can use var (I sometimes do this with a hash). Just put
    use var(all,vars,I,donot,want,initialized,with,my); above your use strict; cheers %^+^%