Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Is local a declaration?

by dsb (Chaplain)
on Apr 10, 2002 at 15:58 UTC ( [id://158050]=note: print w/replies, xml ) Need Help??


in reply to Is local a declaration?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://158050]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found