Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

More Scope related questions.

by curiousmonk (Sexton)
on Mar 29, 2013 at 04:22 UTC ( #1026077=perlquestion: print w/ replies, xml ) Need Help??
curiousmonk has asked for the wisdom of the Perl Monks concerning the following question:

In the examples below, the variable the declaration of $b without my, local or our works like a global variable. I know this issue will be solved if strictures and warnings are enabled(One would be forced to declare a scope specifier) but without that, the expected behaviour should be that unless you explicitly declare it as 'our' the scope of the variable should end immediately after the curly brace

Shouldn't the default scope be local?

case 1:

$ perl -e 'use Data::Dumper;{my @a=(0..10);$b=\@a} print Dumper $b' $VAR1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

case 2:

$ perl -e 'use Data::Dumper;{my @a=(0..10);my $b=\@a} print Dumper $b' $VAR1 = undef;

case 3:

$ perl -e 'use Data::Dumper;{my @a=(0..10);local $b=\@a} print Dumper +$b' $VAR1 = undef;

case 4:

$ perl -e 'use Data::Dumper;{my @a=(0..10);our $b=\@a} print Dumper $b +' $VAR1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

Comment on More Scope related questions.
Select or Download Code
Re: More Scope related questions.
by LanX (Canon) on Mar 29, 2013 at 04:44 UTC
    > Shouldn't the default scope be local?

    No, the default isn't scoped at all¹ and the variables are globally accessible package vars.

    our is similar, but the (compile time) declaration is scoped.

    our was introduced comparatively late. Before that you needed to use vars to be strict.

    Try searching the archives, it's full of explanations and comparative examples.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) some languages do, but Perl follows the mainstream tradition.

Re: More Scope related questions.
by BrowserUk (Pope) on Mar 29, 2013 at 04:47 UTC
    Shouldn't the default scope be local?

    Should or should not doesn't enter into it. It isn't.

    For why, read 25 years of history.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: More Scope related questions.
by 2teez (Priest) on Mar 29, 2013 at 04:57 UTC
Re: More Scope related questions.
by Anonymous Monk on Mar 29, 2013 at 05:10 UTC
Re: More Scope related questions.
by kcott (Abbot) on Mar 29, 2013 at 06:40 UTC

    G'day curiousmonk,

    Your question appears to have been answered in the responses you've already received. I might just point out that $a and $b were poor choices for your examples as they're special variables that don't require declaration. Here's an excerpt from perlvar:

    • $a
    • $b
      Special package variables ... Because of this specialness $a and $b don't need to be declared (using use vars, or our()) even when using the strict 'vars' pragma. ...

    -- Ken

      Thanks, this was one thing I didn't know about. It just all boils down to reading documentation properly

      Just as you said

      $ perl -e 'use strict;use Data::Dumper;{my @c=(0..10);$d=\@c} print Du +mper $d' Global symbol "$d" requires explicit package name at -e line 1. Global symbol "$d" requires explicit package name at -e line 1.

      Doesn't work, but

      $ perl -e 'use strict;use Data::Dumper;{my @a=(0..10);$b=\@a} print Du +mper $b' $VAR1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

      Does

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-12-26 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (167 votes), past polls