in reply to Re^2: will you explain what's going on?
in thread will you explain what's going on?
Does this mean that space for all variables in all nested blocks is pre-allocated at compile time? If so that means that even lexicals in blocks of deeper level than current one are accessibe from current block (if I understood you right), i.e.
But I used to think (no, I don't know perl internals) that on entering each block that block's 'my' variables are created(allocated) in some 'lexicals stack', probably replacing lexicals with the same name from upper blocks.{ # with some hack $a is accessible here { my $a; } }
Perl doesn't (always) clear lexical variables with refcounts of zero at scope exit (it's an optimization).
Reasonable. But what about initialization of variable on each loop pass? If I just write my $var;, the variable is being assigned undef (or empty list for array/hash). But my $var='foo' if $bool effect is still unclear to me. Consider this:
If $name wasn't cleared, as you say, 'c' would spread to the end of loop. Or maybe I'm just missing something obvious? :)use strict; my %hash = ( a => 'A', b => 'B', c => 'C', e => 'E', d => 'D', ); for my $s ( qw( a b c d e ) ) { my $name = $1 if $s =~ /(c)/; $name = $hash{$s} unless defined $name; warn $name || 'undef'; } Output: A at 7.pl line 19. A at 7.pl line 19. c at 7.pl line 19. D at 7.pl line 19. D at 7.pl line 19.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: will you explain what's going on?
by ysth (Canon) on Oct 27, 2004 at 11:58 UTC |