Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: Scope and references

by jpl (Monk)
on Jun 19, 2011 at 18:31 UTC ( #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.


Comment on Re^3: Scope and references
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
Node Status?
node history
Node Type: note [id://910461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-10-23 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls