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

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
#!/usr/bin/perl -- use strict; use warnings; print ASDF(19), "\n"; BEGIN { my $a = 22; sub ASDF { my $b = $_[0] + 1; $a + $b; } }
perl.exe -d pm.919064.1.pl Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(pm.919064.1.pl:4): print ASDF(19), "\n"; main::(pm.919064.1.pl:5): BEGIN { DB<1> s main::ASDF(pm.919064.1.pl:8): my $b = $_[0] + 1; DB<1> x $a 0 22 DB<2> s main::ASDF(pm.919064.1.pl:9): $a + $b; DB<2> s 42 Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<2> q
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
#!/usr/bin/perl -- use strict; use warnings; print ASDF(19), "\n"; BEGIN { my $a = 22; sub ASDF { my $b = $_[0] + 1; } }
perl.exe -d pm.919064.2.pl Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(pm.919064.2.pl:4): print ASDF(19), "\n"; main::(pm.919064.2.pl:5): BEGIN { DB<1> s main::ASDF(pm.919064.2.pl:8): my $b = $_[0] + 1; DB<1> x $a 0 undef DB<2> s 20 Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<2> q

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
#!/usr/bin/perl -- use strict; use warnings; print ASDF(19), "\n"; BEGIN { my $a = 22; $a += 0; sub ASDF { my $b = $_[0] + 1; } sub DADA { $a } }
perl.exe -d pm.919064.4.pl Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(pm.919064.4.pl:4): print ASDF(19), "\n"; main::(pm.919064.4.pl:5): BEGIN { DB<1> s main::ASDF(pm.919064.4.pl:9): my $b = $_[0] + 1; DB<1> x $a 0 undef DB<2> s 20 Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<2> q

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

Replies are listed 'Best First'.
Re^5: Debugger and lexicals
by dave_the_m (Monsignor) on Aug 07, 2011 at 12:57 UTC
    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

        Its attached to the scratchpad of the CV that is created by compiling Foo. After Foo is executed, that CV and its scratchpad are freed, and $a gets freed at that point since there aren't any other references to it.

        Dave.