maard has asked for the wisdom of the Perl Monks concerning the following question:

Greetings.

While extending functionality of one of the CPAN modules I've run into something strange.

The code is:

foreach my $name (@$fields) { # ... my $namedisplay = $1 if($name =~ /AS\s+(\S+)/i); warn "namedisplay: $namedisplay"; # 1-st warn # that's the lines I added to the code: if ( !$namedisplay && exists($obj->{labels}->{$name}) && $obj->{labels}->{$name}) { $namedisplay = $obj->{labels}->{$name}; } warn "namedisplay: $namedisplay"; # 2-nd warn # ... }
I was amazed to see that on each iteration $namedisplay is being assigned the same value (the $obj->{labels}->{$fields->[0]} one).

After splitting the line where $namedisplay first occured into declaration and assignment parts everything became OK.

my $namedisplay; $namedisplay = $1 if($name =~ /AS\s+(\S+)/i);

But I still don't understand what was happening before fix. I understand that $namedisplay was declared regardles of regex match (I was even begining to think the entire declaration could be skipped if regex didn't match). But it somehow remained alive throghout all loop iterations (1-st warn showed the same value for the variable on all iterations exept first one).

Will wise monks bring some light to this problem?

Replies are listed 'Best First'.
Re: will you explain what's going on?
by fglock (Vicar) on Oct 26, 2004 at 18:06 UTC

    update: this explanation is wrong - see chromatic post below.

    The first time the if-condition is false, you are "autovivifying" a global variable "$namedisplay".

    Whenever the if-condition is false again, the global variable will be in scope and the old value will show up.

    You can catch this with "use strict;".

      No, that's incorrect:

      use strict; use warnings; for my $bool ( 0 .. 1 ) { my $name = 'bob' if $bool; print "($name)\n"; }

      The actual truth of the matter is that my has compile-time and run-time effects.

      At compile time, Perl creates an entry for $namedisplay in the appropriate lexical pad. Thus every lookup for that variable will find the lexical.

      At run time, Perl evaluates the condition to determine whether to assign a new value to the lexical variable. Perl doesn't (always) clear lexical variables with refcounts of zero at scope exit (it's an optimization). However, if you don't always reset your variable -- if the run-time behavior of my doesn't occur because of a false conditional -- you will (may) see the old values.

      Update: Actually, I can't recreate the memory or trip the warning about it now either, so I've edited my post somewhat.

        Perl doesn't (always) clear lexical variables with refcounts of zero at scope exit (it's an optimization).
        Under normal circumstances, Perl always clears the variable; the optimisation is that it never frees it. The bug is that the run-time effect of of my is to push an instruction onto the savestack to clear the variable on scope exit. If the my is skipped, the var doesn't get cleared on scope exit, and its old value is still there at the next entry to the block. (Note that on entry to a block, my doesn't clear the variable).

        Some people have used this as a way of getting static vars, but this is officially deprecated these days.

        Dave.

        I tried the following program under Perl 5.005_03, 5.6.1, and 5.8.4 and got the same results - no "memory effect":

        use strict; #use warnings; for my $s ( qw( nob bob cob bob dob ) ) { my $name = $1 if $s =~ /(bob)/; warn $name || 'undef'; }

        result:

        undef at - line 7. bob at - line 7. undef at - line 7. bob at - line 7. undef at - line 7.
        At compile time, Perl creates an entry for $namedisplay in the appropriate lexical pad. Thus every lookup for that variable will find the lexical.

        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.

        { # with some hack $a is accessible here { my $a; } }
        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.
        So this is really interesting: which of this two approaches is used in Perl?

        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:

        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.
        If $name wasn't cleared, as you say, 'c' would spread to the end of loop. Or maybe I'm just missing something obvious? :)