Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

what is the scope of my $x=$x

by perladdict (Chaplain)
on Jun 05, 2006 at 10:36 UTC ( #553561=perlquestion: print w/replies, xml ) Need Help??

perladdict has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: what is the scope of my $x=$x
by davorg (Chancellor) on Jun 05, 2006 at 10:46 UTC

    What happened when you tried it out?

    There are two variables called $x there. The one on the left hand side of the assignment is a lexical variable and it will be in scope from this statement until the end of the innermost enclosing block. On the right hand side of the assignment is a existing package variable and its scope depends on where and how you created it. By default its scope will be the whole of the package that it was created in.

    Try experimenting with this code:

    $x = "foo"; print "$x / $main::x\n"; { my $x = $x; print "$x / $main::x\n"; $x = "bar"; print "$x / $main::x\n"; } print "$x / $main::x\n";
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      The one on the left hand side of the assignment is a lexical variable and it will be in scope from this statement until the end of the innermost enclosing block.

      Minor nit: s/from this statement/from the next statement/; Variables declared with my begin to be visible at the next statement, as documented in perlsub and demonstrated here:

      my $c=4; if (my $c and !defined $c) { print "in\n"; }

      This prints nothing, since defined is accessing the outer $c (that is defined) even when it's after the my declaration in the same statement.

      --
      David Serrano

Re: what is the scope of my $x=$x
by liverpole (Monsignor) on Jun 05, 2006 at 10:51 UTC
    Hi perladdict,

    Why don't you just try it and see?! ...

    #!/usr/bin/perl -w + # Strict use strict; use warnings; + + my $x = 15; # This is the outer $x + { my $x = $x; # This is the inner $x print "Location 1, x = $x\n"; $x += 7; print "Location 2, x = $x\n"; } + print "Location 3, x = $x\n";
    The results are:
    Location 1, x = 15 Location 2, x = 22 Location 3, x = 15
    Clearly, the line my $x = $x; is creating a lexical variable $x (the first one) which starts out with the value of the "outer" $x (the second one).  It's really just like saying my $b = $a, where $a is the "outer" value, and $b is the "inner" one.

    Of course, whether or not it's a good programming practice to use the same name for multiple variables is an exercise left to the reader.  ;-)


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: what is the scope of my $x=$x
by Joost (Canon) on Jun 05, 2006 at 10:45 UTC

      Probably, but it could also be

      sub routine{ my $x = 123; print $x; ## 123 if( 1 ) { my $x = $x; ## Snippet here $x /= 2; print $x; ## 61.5 do { my $x = $x; ## Or here $x *= 10; print $x; ## 615 }; print $x; ## 61.5 } print $x; ## 123 }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: what is the scope of my $x=$x
by duff (Parson) on Jun 05, 2006 at 14:47 UTC

    In perl5, you have two different $x. In perl6, both $x are the same $x. To get at a variable named $x in an outer scope you need to use the fully qualified name of the variable which would include information about whether it's a global variable (GLOBAL::<$x>, perl6 has true globals BTW), a variable in the symbol table of the current package (OUR::<$x>), or a lexical variable in the surrounding lexical scope (OUTER::<$x>).

Re: what is the scope of my $x=$x
by girarde (Hermit) on Jun 05, 2006 at 14:02 UTC
    An interesting question, but I wonder why the answer matters? Needing a my copy of a global is not surprising. Needing it to have the same name as the global, though? How does that arise? Why wouldn't you just make a local?
Re: what is the scope of my $x=$x
by schwern (Scribe) on Jun 05, 2006 at 19:01 UTC

    The real answer is "don't do that".

    "my $x" and "$x" here are two different variables, there is nothing magical about them having the same name. "my $x" contains a copy of "$x". "my $x" exists only for the life of its scope and masks "$x". It should be clear now why you do not want to use the same variable name for an inner scope, the overlapping names make it really hard to remember what's going on or to even talk about it.

    (local $x = $x is somewhat different)

    Use a different name. Then it will become clear what the scope of each variable is and the issue goes away. I can't give you a good name without any context, and $x is an awful name to begin with, but even "my $copy_x = $x" is better.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-28 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (100 votes). Check out past polls.

    Notices?