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

Perl Monks,

I understand that local $/ saves a copy of $/ and it sets it to undef. But, what I wasn't expecting is when I run my script with the perl debugger and type x \$/ after local $/, it still shows that the value is \n. But, when I print $/ after performing local $/, it outputs the empty string (as expected). I'm not sure I understand why x \$/ in the perl debugger still shows it as a newline.

Thank you for your help,

lupey

#!/usr/bin/perl print "RS is:<$/>\n"; # typing 'x \$/' in debugger gives this (as expected) #0 SCALAR(0x10141238) # -> ' #' changeRS(); sub changeRS { local $/; print "RS is:<$/>\n"; # typing 'x \$/' in debugger gives this (*NOT* as expected) #0 SCALAR(0x103b8f68) # -> ' #' } print "RS is:<$/>\n"; # typing 'x \$/' in debugger gives this as expected #0 SCALAR(0x10141238) # -> ' #'

Replies are listed 'Best First'.
Re: confused about local $/ in perl debugger
by fishbot_v2 (Chaplain) on May 24, 2005 at 13:43 UTC

    I'm not a regular user of the perldebugger, but my understanding was that x worked from the package scope, not the lexical scope. If you wanted to dive down into the lexical scope, you needed y (and the PadWalker module). The debugger isn't terribly smart... hooked around the whole application, and thus outside the lexical scope in question. It's non-trivial to probe a lexical/local from outside their scope.

    This appears true for my lexicals and locals. Try:

    my $foo = 10; $bar = 10; sub somelocals { my $foo = 42; local $bar = 42; # "x $foo", "x $bar" are 10 here } somelocals();

    Update:

    This seems version dependent - my results and yours are consistent with v5.8.0. On v5.8.6, I see the opposite behaviour with x. (ie. lexicals are exposed as expected.)

    re-Update:

    Ignore. My testing was flawed.

      Your reply further confuses me. When I run your script in the debugger, $foo and $bar inside the subroutine are 42. The address of $foo and $bar changes appropriately.

      In my OP, notice that the address of $/ changes appropriately but the value does not.

      I'm runnning Perl 5.8.6 on Cygwin if this makes a difference.

      lupey

        Yeah, and now I am confused too. I never should have offered debugger advice. I am seeing different results too. (on v5.8.0):

        # inside - main::changeRS DB<1> x $/ 0 undef DB<2> x \$/ 0 SCALAR(0xfab54) -> ' '

        I am seeing that the value changes if I evaluate at that point, but if I 'x' the reference I see a new location, but the old value.

        Also, I can now no longer reproduce my lexical oddness from before. I think that my one trial silently fell out of the inner scope. Even my local $bar example is behaving as both x \$bar and x $bar.

        Loath as I am to say this, (especially with my track record in this thread) I think that this is a bug in the debugger. I am going to test with other special vars...

Re: confused about local $/ in perl debugger
by tlm (Prior) on May 24, 2005 at 13:42 UTC

    I'm not sure I understand your question 100%, but I think that what you are observing is a consequence of the fact that each line you type interactively in the debugger is in its own block/scope. Therefore, by using local you basically limit the change to $/ to that line. Watch:

    DB<1> print "<$/>" < > DB<2> local $/ DB<3> print "<$/>" < > DB<4> local $/; print "<$/>" <>

    Update: see also this node.

    the lowliest monk