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


In reply to Re^4: Debugger and lexicals by Anonymous Monk
in thread Debugger and lexicals by lzipin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.