Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Scope and references

by jpl (Monk)
on Jun 19, 2011 at 18:31 UTC ( [id://910461]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Scope and references
in thread Scope and references

It might (or might not) be helpful to realize that my is an executable statement, not "just" a declaration.

update The following is more misleading than helpful. Please see the followup "puzzle" Re^4: Scope and references to see why. It produces a new instance of its argument(s), effectively unrelated (except by name) to previous instances created in that scope, and any previous instance has its reference count reduced by 1. If you have tucked a reference to a previous instance into an array, as you did in your first example, that keeps the reference count positive, so the instance does not get garbage collected. If there is no other reference to a previous instance, it ceases to exist as far as you are concerned.

This isn't simple stuff, most of us have been tripped up by something similar when we first started using lexical variables.

Replies are listed 'Best First'.
Re^4: Scope and references
by jpl (Monk) on Jun 20, 2011 at 10:51 UTC
    Here's a test of understanding. What does the following produce?
    use strict; use Data::Dumper; my @array; for (my $i = 0; $i < 5; ++$i) { LABEL: my @y; push(@y, $i); push(@array, \@y); if ($i & 1) { ++$i; goto LABEL; } } print(Dumper(\@array), "\n");
    Hint: I failed.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-16 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found