in reply to will you explain what's going on?

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;".

Replies are listed 'Best First'.
Re^2: will you explain what's going on?
by chromatic (Archbishop) on Oct 26, 2004 at 19:26 UTC

    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.

        Oh, thank you very much!++
        Now everything is on it's place.
        Final question: how can one find out the answer to the question like this one? Looking through perl source code? Because I'd like to find the answer myself but sometimes stuck into not knowing even where to look into...

      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? :)
        Most of this is implementation details that you don't need to worry about (and aren't guaranteed). The exception that comes to mind off the top of my head is that lexicals are indeed created for the first time before their containing block is entered, and you can set them via a BEGIN, INIT, or CHECK block:
        { my $q; BEGIN { # runs at compile time when the outer block hasn't been entered $q = 42; } print $q; # prints 42 }
        However, changing it like so:
        for (0..1) { my $q; BEGIN { # runs at compile time when the outer block hasn't been entered $q = 42; } print $q || "undef"; # prints 42undef }
        shows that it does get restored to undef at the end of the block. Getting back to the original question in this thread, modifying it:
        our $q = 54; # never actually used for (0..1) { my $q if 0; BEGIN { # runs at compile time when the outer block hasn't been entered $q = 42; } print $q || "undef"; # prints 4242 }
        shows that keeping my from executing at runtime suppresses this clearing at the end of the block.