Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
why this is not a closure though ?
use warnings; use strict; { my $a; } $a=1 print $a;
in my eyes my $a is visible outside the {} many thanks

First off, that's a bad example, because $a is a pre-defined global in perl, used by the built-in sort function. Secondly, when you declare a variable with "my", you are setting the scope of that variable to be the smallest block that encloses the declaration; or if it's not inside any curly-bracketed block, then the scope is the remainder of the given script file:

use strict; use warnings; $x = 0; # syntax error: $x has not been declared my $y = 1; # ok, $y is "in scope" for remainder of file { my $y = 2; # a different $y, scope limited to this block print $y, $/; # you cannot "see" the "outer" instance of $y sub foo { $y--; print "the inner \$y is now $y\n"; } foo(); } print $y, $/; # this is the "outer" $y my $x = 3; # ok: $x is in scope for remainder of file my $y = 4; # warning: "my" declaration masks earlier declaration in +same scope print $y,$/; foo();
As-is, that snippet will not run, but it will generate the error and warning messages shown in comments. Delete or comment-out the "$x=0" line, and it will run, but the "my $y=4" will still cause the warning about "masks earlier declaration in same scope".

When it runs, you'll see that the sub "foo", which is callable from anywhere, will always use the "inner" instance of $y, because that's the only one it could "see".

(updated to remove a stray ";", and to make the "foo" sub a little more interesting)


In reply to Re: Closures clarification by graff
in thread Closures clarification by props

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found