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

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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910461]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-21 10:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.