Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I'm confused because I thought when using "strict", variables only existed inside their blocks

Using the strict pragma doesn't actually change where variables "exist" (as you put it.) What it does do is force the program to error out when a global variable is used without being properly declared. Variables declared with my are lexically scoped (not global) and are only visible to their enclosing block, file, or eval.

When I pass the reference to %sub_hash back from &makehash, why isn't it a reference to something that no longer exists?

It is probably easiest and most correct to think of the reference as a new value. It is also best to think of your variable's value and its name being separate. Your my %sub_hash variable is indeed only visible within your subroutine. That is to say, the value it holds can only be accessed via the name %sub_hash while inside your sub. If you did not pass a reference to the value back to the main program, there would be no way to get at that value again (and perl would consider it ripe for garbage collecting.) Since you do create a reference to the value by using the backwhack ("\") operator, perl makes a note of the fact that you still want it to be accessible (by incrementing its reference count) and will not garbage collect it.

Does that help?

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: What's a reference? What's a variable? What's scope? by sauoq
in thread What's a reference? What's a variable? What's scope? by kurt_kober

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 pondering the Monastery: (3)
As of 2024-04-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found