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

Hi,

I'm testing an eval block. I want to imitate a situation in debug mode and for that I want to change $@ in debug mode to a different value. But the value in $@ does not seem to be changing.

Take a look at the following debug session:

> perl -d -e ' eval { 1/0}; print $@; $@ = "nikhil"; print $@' Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): eval { 1/0}; print $@; $@ = "nikhil"; print $@ DB<1> DB<1> n main::(-e:1): eval { 1/0}; print $@; $@ = "nikhil"; print $@ DB<1> n main::(-e:1): eval { 1/0}; print $@; $@ = "nikhil"; print $@ DB<1> n Illegal division by zero at -e line 1. main::(-e:1): eval { 1/0}; print $@; $@ = "nikhil"; print $@ DB<1> x $@ 0 'Illegal division by zero at -e line 1. ' DB<2> $@ = "a" DB<3> x $@ 0 'Illegal division by zero at -e line 1. ' DB<4> n main::(-e:1): eval { 1/0}; print $@; $@ = "nikhil"; print $@ DB<4> x $@ 0 'nikhil' DB<5>

I'm able to set $@ programmatically from the script but not in the debug shell. Is this a known perl feature/issue?

Please enlighten me.

Thanks,

Nikhil

Replies are listed 'Best First'.
Re: Changing $@ in debug mode
by Corion (Patriarch) on Dec 02, 2010 at 12:53 UTC

    Have you tried your script outside of the debugger?

    eval {1/0} is a compile time error untrappable by block-eval. You will need to use string-eval to postpone that error, or make the constant expression a variable expression that cannot be optimized:

    # string-eval eval "1/0"; my $div = 0; eval { 1 / $div };

      As shown in the debug session output, it's compiling fine. 1/0 is a runtime divide by 0 error and that was what I expected. But that is not my question here. This is just some sample code that I put in here to explain my doubt.

Re: Changing $@ in debug mode
by locked_user sundialsvc4 (Abbot) on Dec 02, 2010 at 13:44 UTC

    I frankly would not expect that variable to be subject to manipulation in that way, nor for its value to remain stable.   “It does not belong to you...”   You need to figure out another way to do what you want.

      That's what I did ultimately...
      But isn't there any way I can change it in debug mode? I mean it allows me to modify that variable in the script itself. But not while debugging.... Why should it be like that?
Re: Changing $@ in debug mode
by pemungkah (Priest) on Dec 03, 2010 at 00:16 UTC
    Take a look at the debugger source code - specifically, bring up perl5db.pl in your editor and search for "Begin lexical danger zone".

    This is DB::eval(), the subroutine that the debugger uses to protect itself from you. Anything that you run here can see all of the lexical variables that are currently in scope - and it saves and restores $@ so it can show it to you should the eval of the code you type in sets it. This means that manipulating $@ in the debugger won't work.

    DB::eval is also the reason that my $x = "foo" "doesn't work" under the debugger - the creation of the lexical variable happens inside DB::eval, so it comes into existence and disappears again as soon as DB::eval exits.