Cloaked eval errors - bug or feature?

Discussion in 'Perl Misc' started by Bas van Sisseren, Aug 9, 2005.

  1. Hi,

    I recently found out i was using a 'local $@' and a 'die' in the same code
    block by mistake. The result was an eval error which was cloaked by the
    'local $@'. It seems to me this is not the behaviour someone would expect
    of this combination.

    I'm using perl v5.8.7 from debian sid.

    Some examples:
    =====[ Example #1 ]=====
    #!/usr/bin/perl -w

    $@='';

    eval {
    print "1\n";
    eval {
    print "2\n";
    {
    local $@;
    die "something";
    }
    print "3\n";
    };
    die $@ if $@;
    print "4\n";
    };

    if ($@) {
    print "Exception caught: $@";
    } else {
    print "OK\n";
    }
    =======[ Result ]=======
    1
    2
    4
    OK
    ========================

    The exception isn't caught in the 'die $@ if $@', therefore it isn't
    possible anymore to rethrow this exception. It seems like the inner eval
    didn't have any problems.

    =====[ Example #2 ]=====
    #!/usr/bin/perl -w

    $@='';

    eval {
    print "1\n";
    eval {
    $@="hi there";
    print "2\n";
    {
    local $@;
    # die "something";
    }
    print "3\n";
    };
    die $@ if $@;
    print "4\n";
    };

    if ($@) {
    print "Exception caught: $@";
    } else {
    print "OK\n";
    }
    =======[ Result ]=======
    1
    2
    3
    4
    OK
    ========================
    Normal, expected, behaviour. At the end of the outer eval, the $@ variable
    is cleared.

    =====[ Example #3 ]=====
    #!/usr/bin/perl -w

    $@='';

    eval {
    print "1\n";
    eval {
    $@="hi there";
    print "2\n";
    {
    local $@;
    die "something";
    }
    print "3\n";
    };
    die $@ if $@;
    print "4\n";
    };

    if ($@) {
    print "Exception caught: $@";
    } else {
    print "OK\n";
    }
    =======[ Result ]=======
    1
    2
    Exception caught: hi there at ./eval_test.pl line 16.
    ========================
    Now, the previous $@ is inserted back into the $@ variable. We now have
    caught an exception in the outer eval, which was never thrown.


    Solution?

    I think the $@ should not be set in the code-block of the die itself, but at
    the point where code execution resumes, at the end of the eval{} block.

    --
    Bas van Sisseren
    Bas van Sisseren, Aug 9, 2005
    #1
    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 Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    942
    Mark Goldin
    Jan 17, 2004
  2. Martin Maat
    Replies:
    0
    Views:
    905
    Martin Maat
    May 30, 2004
  3. Eric Newton
    Replies:
    3
    Views:
    9,393
    Brock Allen
    Apr 4, 2005
  4. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
  5. robic0
    Replies:
    7
    Views:
    103
    Samwyse
    Jan 24, 2006
Loading...

Share This Page