Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Variable declaration

by nagalenoj (Friar)
on Oct 14, 2011 at 05:24 UTC ( #931434=perlquestion: print w/ replies, xml ) Need Help??
nagalenoj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
use strict; use warnings; package new; $new::variable = 1; $another_variable = 2; 1;

When I right a code like this, I get error for not declaring $another_variable and there is no error for not declaring $variable. Why is so?

I thought, it will raise error for both cases. Someone please explain, how both are different.


Thanks.

Comment on Variable declaration
Download Code
Re: Variable declaration
by fisher (Priest) on Oct 14, 2011 at 06:10 UTC
    You 'use strict' and do not declare the scope of your variable; just add before actual usage a declaration like this:
    my $another_variable;
    And variable $new::variable already declared in package 'new'.
Re: Variable declaration
by Anonymous Monk on Oct 14, 2011 at 06:11 UTC
Re: Variable declaration
by GrandFather (Cardinal) on Oct 14, 2011 at 06:33 UTC

    $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
      $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).

Re: Variable declaration
by Ovid (Cardinal) on Oct 17, 2011 at 15:14 UTC

    In addition to the other comments, I recommend new programmers also use diagnostics. Not only does that give you more information about the error, it also gives you suggestions on how to fix it. Here's the output I get when I include that:

    Global symbol "$another_variable" requires explicit package name at di +ag.pl line 6. Execution of diag.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 "$another_variable" requires explicit package na +me at diag.pl line 6. Execution of diag.pl aborted due to compilation errors. at diag.pl line 6

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-09-30 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls