dynamic scoped variables in the debugger

Discussion in 'Perl Misc' started by Nathan, Jul 27, 2008.

  1. Nathan

    Nathan Guest

    Hi,

    I'm running Perl v5.8.8 compiled for linux. I was reading about the perl
    debugger and ran the following script with the command "perl -d":

    1 #!/ usr/bin/perl
    2 $_ = "foo";
    3 /foo/;
    4
    5 print "$&";
    6 print "\n";

    As expected it prints "foo" before the newline, and if I check the value
    of $& in the debugger it does show "foo". However, if I omit line 5 of
    the program then the debugger says $& is undefined when it reaches the
    last print statement. I don't understand how this can be, is there some
    subtlety to the way the debugger treats dynamically scoped variables?
    Any insight anyone can offer would be great, thanks.

    -Nathan
    Nathan, Jul 27, 2008
    #1
    1. Advertising

  2. Nathan

    Guest

    Nathan <> wrote:
    > Hi,
    >
    > I'm running Perl v5.8.8 compiled for linux. I was reading about the perl
    > debugger and ran the following script with the command "perl -d":
    >
    > 1 #!/ usr/bin/perl
    > 2 $_ = "foo";
    > 3 /foo/;
    > 4
    > 5 print "$&";
    > 6 print "\n";
    >
    > As expected it prints "foo" before the newline, and if I check the value
    > of $& in the debugger it does show "foo". However, if I omit line 5 of
    > the program then the debugger says $& is undefined when it reaches the
    > last print statement.


    When the program is compiled, if the compiler sees $& being used,
    then it tuns on the (expensive) code which causes $& to be set during
    regex operations. If $& is not seen, then this code is not activated
    and $& does not get populated.

    > I don't understand how this can be, is there some
    > subtlety to the way the debugger treats dynamically scoped variables?


    This is not related to the debugger itself. The debugger is just giving
    you a way arrange things so that you can inspect $& without perl knowing
    you are going to do so. You can get the same behaviour using string eval:

    perl -le '$_="foo"; /foo/; eval q{print "$&"}'

    Also, $& is not just a dynamic variable (like $foo::bar), but a special
    variable. It is the implementation details of this specialness that is
    tripping you up.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Jul 28, 2008
    #2
    1. Advertising

  3. Nathan

    Nathan Guest

    wrote:
    > Nathan <> wrote:
    >> Hi,
    >>
    >> I'm running Perl v5.8.8 compiled for linux. I was reading about the perl
    >> debugger and ran the following script with the command "perl -d":
    >>
    >> 1 #!/ usr/bin/perl
    >> 2 $_ = "foo";
    >> 3 /foo/;
    >> 4
    >> 5 print "$&";
    >> 6 print "\n";
    >>
    >> As expected it prints "foo" before the newline, and if I check the value
    >> of $& in the debugger it does show "foo". However, if I omit line 5 of
    >> the program then the debugger says $& is undefined when it reaches the
    >> last print statement.

    >
    > When the program is compiled, if the compiler sees $& being used,
    > then it tuns on the (expensive) code which causes $& to be set during
    > regex operations. If $& is not seen, then this code is not activated
    > and $& does not get populated.
    >
    >> I don't understand how this can be, is there some
    >> subtlety to the way the debugger treats dynamically scoped variables?

    >
    > This is not related to the debugger itself. The debugger is just giving
    > you a way arrange things so that you can inspect $& without perl knowing
    > you are going to do so. You can get the same behaviour using string eval:
    >
    > perl -le '$_="foo"; /foo/; eval q{print "$&"}'
    >
    > Also, $& is not just a dynamic variable (like $foo::bar), but a special
    > variable. It is the implementation details of this specialness that is
    > tripping you up.
    >
    > Xho
    >


    Thanks Xho, I vaguely suspected something along those lines, but I guess
    I underestimated how "special" the special variables are. Perl gives you
    everything you need except consistency. :)

    -Nathan
    Nathan, Jul 28, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    4
    Views:
    7,931
    mike_treseler
    Nov 9, 2004
  2. Andy Fish

    block scoped variables

    Andy Fish, Aug 15, 2003, in forum: Java
    Replies:
    3
    Views:
    407
    Chris Uppal
    Aug 16, 2003
  3. Madhav
    Replies:
    4
    Views:
    299
    Kenny McCormack
    Jan 8, 2006
  4. Madhav
    Replies:
    27
    Views:
    548
    Dave Thompson
    Jan 16, 2006
  5. Eric Sunshine

    Lexically scoped variables

    Eric Sunshine, Dec 19, 2003, in forum: Ruby
    Replies:
    6
    Views:
    130
    Robert Klemme
    Dec 26, 2003
Loading...

Share This Page