in reply to Re: Eval not trapping fatal error
in thread SOLVED: Eval not trapping fatal error

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!

Replies are listed 'Best First'.
Re^3: Eval not trapping fatal error
by Corion (Patriarch) on Dec 07, 2015 at 12:34 UTC

    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.