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

Is this a bug, undefined behaviour or just a general lack of wakefulness on my behalf
use strict; use warnings; use Devel::Peek; my $i = 0; { my $x = "foo"; sub bbtest { $x .= " bar"; print "subroutine: ", $x, $/; } } { my $x = "foo"; SUB_BBTEST: { Dump($x); $x .= " bar"; print "bareblock : ", $x, $/; } } goto SUB_BBTEST while $i++ < 3; bbtest() while $i++ < 7;
__output__ SV = PV(0x80fbe38) at 0x8108048 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x8101be0 "foo"\0 CUR = 3 LEN = 4 bareblock : foo bar SV = PV(0x80fbe38) at 0x8108048 REFCNT = 1 FLAGS = (PADBUSY,PADMY) PV = 0x8101be0 "foo bar"\0 CUR = 7 LEN = 8 bareblock : bar SV = PV(0x80fbe38) at 0x8108048 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x8101be0 " bar"\0 CUR = 4 LEN = 8 bareblock : bar bar SV = PV(0x80fbe38) at 0x8108048 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x8101be0 " bar bar"\0 CUR = 8 LEN = 9 bareblock : bar bar bar subroutine: foo bar subroutine: foo bar bar subroutine: foo bar bar bar
So the strange thing there is that when the bare block scope is re-entered the POK and pPOK flags have been removed (which indicate the validity of a variable and the validity of the variable in regards to the magic system, respectively) but the original value remains. Whereas the subroutine doesn't have any problems (as would be expected). Anyone have an idea as to why this is happening?
HTH

_________
broquaint

512 posts later and I'm still asking questions, ain't perl (and of course Perlmonks!) great?

Replies are listed 'Best First'.
Re: Bare blocks forgetting lexical values
by sauoq (Abbot) on Dec 05, 2002 at 18:33 UTC
    Is this a bug, undefined behaviour or just a general lack of wakefulness on my behalf.

    I vote for "undefined behaviour." As $x is lexical, once you leave the block it is completely reasonable that its storage be reallocated, right? In your example, you are jumping right back into the block but what if there was a fair amount of complexity between exit and reentry?

    If I had two votes, I might call it a bug too. I think that $x should cease to exist on exit and on reentry you should be modifying the package's $x.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Bare blocks forgetting lexical values
by BrowserUk (Patriarch) on Dec 05, 2002 at 19:36 UTC

    My vote is that in the same way that it is illegal to goto a label within a for loop (see perltrap, section: "Discontinuance, Deprecation, and BugFix traps" ), it should probably be illegal to goto the middle of a block.

    Apart from the fact I can't see any real use for the construct, one way to deal with the anomoly would be to only permit goto's from code at the same lexical scope or deeper. That is to say, make the labels themselves have lexical scope just like a my'd variable. That would allow most of the useful uses of goto to continue to work, but would prevent the anomoly you describe.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Bare blocks forgetting lexical values
by shotgunefx (Parson) on Dec 05, 2002 at 20:53 UTC
    I would assume it's because the my $x is optimized to a pad access at compile time. You're not initilizing it so it has it's old value. This sounds like the "my $x if 0 bug". This statement by Elian might shed some light on it as well.

    -Lee

    "To be civilized is to deny one's nature."
      Yep, that's likely it. Whether this is a bug or a feature is up in the air--personally I don't think you're supposed to be able to jump into the middle of a block, but...
        Personally, I think of it as a feature. It does what it says it does, just not what some would expect. I've never used it as I've always had the feeling it would be fixed and making an outer scope is simple and clearer.

        -Lee

        "To be civilized is to deny one's nature."