Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Are we seeing syntax inconsistency?

by Animator (Hermit)
on Nov 11, 2005 at 16:37 UTC ( [id://507779]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Are we seeing syntax inconsistency?
in thread Are we seeing syntax inconsistency?

Actually that's not entirely correct.

foreach does not create a variable. It is my that creates the variable. And my will use some free memory. My guess is that the references counter is increased by using the variable in an anonymous, subroutine (/closure). Which means that at the end of the foreach-block the reference counter is not 0 (and therfor the memory is not freed). Then that same part of the memory is not free at the start of the next iteration, so my will use some other memory.

Some code:
for my $x (0 .. 3) { print \$x }; ==> this code should print the same references for all 4 iterations.
for my $x (0 .. 3) { print \$x; push @z, sub { $x; }; } ==>This code will print 4 different references.

Replies are listed 'Best First'.
Re^4: Are we seeing syntax inconsistency?
by Aristotle (Chancellor) on Nov 11, 2005 at 17:11 UTC

    Which means Ovid’s assertion is correct. A new variable gets allocated through each iteration – only that most of the time, the space for the iterator variable from the last iteration happens to get recycled.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found