in reply to SOLVED: Eval not trapping fatal error

Perl cannot compile your code because it is invalid. You cannot assign to a constant or function (unless that function is already known as lvalue subroutine).

If you want to convert a "syntax"/semantic error to runtime, you will need to use the string form of eval.

Replies are listed 'Best First'.
Re^2: Eval not trapping fatal error
by Ea (Chaplain) on Dec 07, 2015 at 12:26 UTC
    Of course, you're right.

    So, while other constant modules may die and be trapped with eval BLOCK, the constant pragma makes assignment a syntax error and needs to be trapped with eval EXPRESSION. Now I understand the line in the book

    the code in the BLOCK has to be valid Perl code to make it past the compilation phase
    Thanks

    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    http://act.yapc.eu/lpw2015/ It's that time of year again!

      I don't think that you can trap other constant modules with a block eval either. The issue lies with Perl, which doesn't allow a function/subroutine on the left-hand side of an assignment, unless that function is already known as assignable through the :lvalue attribute:

      > perl -wle "sub foo {}; eval { foo()='baz' }" Can't modify non-lvalue subroutine call in scalar assignment at -e lin +e 1, near "'baz' }" Execution of -e aborted due to compilation errors.

      Marking the subroutine with :lvalue lets Perl know that we intend to let the function appear on the left-hand side of an assignment:

      > perl -wle "sub foo:lvalue {}; eval { foo()='baz' }"

      All "constant" modules are syntactic sugar for defining functions that take no parameters, because that's how sigil-less constants work in Perl.