in reply to Re^2: Debugger and lexicals
in thread Debugger and lexicals

Some of what you say I can agree with. But "they only exist within a lexical scope"---and why's that? It's because lexical variables cannot access the symbol table. That's why I mentioned that there's a control "sandboxed" way from the debugger. Package variables can be accessed via the symbol table.

As for my $a getting optimized away, I disagree with that because I just don't see how it can be optimized away when it's uninitialized in the subroutine. Here's a little test that I ran:

#!/usr/bin/perl use strict; use warnings; print mt2(55), "\n"; my $a = 23; sub mt2 { my $b = $_[0] * 2; return $a + $b; }
Here's the corrected script:
#!/usr/bin/perl use strict; use warnings; print mt2(55), "\n"; sub mt2 { my $a = 23; my $b = $_[0] * 2; return $a + $b; }
This one works in the debugger. Thanks for the input.

Replies are listed 'Best First'.
Re^4: Debugger and lexicals
by Anonymous Monk on Aug 07, 2011 at 11:59 UTC

    and why's that? It's because lexical variables cannot access the symbol table.

    No, its because they're initialized at runtime and stored in a pad/Scratchpads, which are attached to subroutines ... http://search.cpan.org/dist/illguts/

    As for my $a getting optimized away, I disagree with that because I just don't see how it can be optimized away when it's uninitialized in the subroutine. Here's a little test that I ran:

    Your example is different.

    In your example, $a is initialized after the call to mt2.

    And you never remove a reference to $a, so it never gets optimized away.

    Here is what you intended :)

    $a is defined
    The "problem" of $a being undefined, is because it is optimized away -- really because the sub which we're stepping into (ASDF) doesn't refer to it

    Here, a different sub refers to $a, so when we step into ASDF, $a is still undefined -- it is not attached to ASDF's Scratchpad

    You should consider not answering or prefacing your guesses (see To Answer, Or Not To Answer....), because there won't always be someone there to answer when Duty Calls

      Stricly speaking $a is not optimised away; it just has a short life span, as as I pointed out earlier.
      $ cat /tmp/Foo.pm package Foo; sub DESTROY { print "DESTROY\n" } my $a = bless {}; print "executing Foo\n"; $ perl -I/tmp -e'use Foo; BEGIN { print "BEGIN\n" }' executing Foo DESTROY BEGIN

      Dave.

        Stricly speaking $a is not optimised away; it just has a short life span, as as I pointed out earlier.

        Yeah, because it isn't attached to any scratchpad