Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Variable declaration

by GrandFather (Cardinal)
on Oct 14, 2011 at 06:33 UTC ( #931439=note: print w/ replies, xml ) Need Help??


in reply to Variable declaration

$new::variable is a package variable belonging to the 'new' package (bad name btw). $another_variable is a lexical variable that you need to declare using 'my' because you are using strictures (use strict; use warnings; - highly recommended btw). The fix is just:

use strict; use warnings; use MyPackage; $MyPackage::variable = 1; my $another_variable = 2;

Note that if package MyPackage uses strict it will need to declare $variable using our:

use MyPackage; use strict; use warnings; our $variable = 1;
True laziness is hard work


Comment on Re: Variable declaration
Select or Download Code
Re^2: Variable declaration
by Eliya (Vicar) on Oct 14, 2011 at 07:14 UTC
    $another_variable is a lexical variable...

    Strictly speaking, $another_variable (as the OP has it) isn't a lexical variable.  my declares a lexical variable — no my, no lexical variable... (maybe you meant to say it should be a lexical variable?).

    In the OP's case, both variables are package variables, it's just that strictures do not complain if you use the fully-qualified form, even if the variable is otherwise undeclared (the idea being that you know what you're doing if you spell it this way).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-07-28 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (193 votes), past polls