Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

More Scope related questions.

by curiousmonk (Beadle)
on Mar 29, 2013 at 04:22 UTC ( [id://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 ];

Replies are listed 'Best First'.
Re: More Scope related questions.
by BrowserUk (Patriarch) 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 LanX (Saint) 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 kcott (Archbishop) 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

Re: More Scope related questions.
by 2teez (Vicar) on Mar 29, 2013 at 04:57 UTC
Re: More Scope related questions.
by Anonymous Monk on Mar 29, 2013 at 05:10 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found