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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Okay, so without the extra braces, each my $var gets tripped up by the previous definition.

But with the bare blocks, why does the aaa section associate and print a value for $var while in the bbb section this fails? The code appears identical and independent to me.

Update: That is, by putting braces around the section in package bbb that newly defines(?) a new lexical variable that also happens to be called $var, why doesn't this get set up independently from the $var of package aaa?

package bbb; { use strict; use warnings 'all'; my $var = 'second time for var'; sub do_b { print "In 'bbb' var is set to '$var'\n"; } }
Further update: I suppose it's bad form to do this via an Update, but the following notes don't stand out and they contain the critical pieces (at least for me.)

chromatic writes: It does declare a new lexical $var over which do_b closes. However, you call the subroutine before the assignment occurs.

Errto writes: The issue is that the assignment of $var within the block under bbb never happens before you call do_b. Don't think of a line like my $x = somevalue; as a declaration of a variable to be initialized before the program begins. Think of it as a statement that runs when the program gets to it.

Certainly these "side effects" would not happen if I structured the components of the example more logically, i.e. saved that "main" section until the end. But it's exactly that jumble that leads to the strange (for me) behavior that now lets me grasp the concept. ++ to you and all who have contributed to this thread.


In reply to Re^2: Scope, package, and 'my' variables by ff
in thread Scope, package, and 'my' variables by ff

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 admiring the Monastery: (4)
As of 2024-03-29 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found