Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: what is the scope of my $x=$x

by davorg (Chancellor)
on Jun 05, 2006 at 10:46 UTC ( [id://553565]=note: print w/replies, xml ) Need Help??


in reply to what is the scope of my $x=$x

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

Replies are listed 'Best First'.
Re^2: what is the scope of my $x=$x
by Hue-Bond (Priest) on Jun 25, 2006 at 01:55 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found