in reply to Why isn't a fatal error trappable?

It is trappable:

c:\test>perl -E"chop for 'fred'; print 'here' " Modification of a read-only value attempted at -e line 1. c:\test>perl -E"eval{ chop; } for 'fred'; print 'here' " here

The only circumstance I'm aware of that it isn't, is if you supply a constant directly to a mutator:

c:\test>perl -E"chop 'fred'; print 'here' " Can't modify constant item in chop at -e line 1, near "'fred';" Execution of -e aborted due to compilation errors. c:\test>perl -E"eval{ chop 'fred' }; print 'here' " Can't modify constant item in chop at -e line 1, near "'fred' }" Execution of -e aborted due to compilation errors.

And that, I've always assumed, is because it is detected at compile time rather than runtime.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Why isn't a fatal error trappable?
by ikegami (Patriarch) on Nov 16, 2010 at 18:22 UTC

    Interesting. Division by zero is intentionally deferred to compile time.

    $ perl -c -e'3/0' -e syntax OK $ perl -e'3/0' Illegal division by zero at -e line 1.

    Yet chop is constant folded even though that always results in an exception.

    Update: Ah! The exception is probably not from constant folding. It's probably a compile-time requirement for an lvalue.

    $ perl -e'chop "fred"' Can't modify constant item in chop at -e line 1, at EOF Execution of -e aborted due to compilation errors.

    runs afoul the same check as

    $ perl -e'"fred" = $_' Can't modify constant item in scalar assignment at -e line 1, at EOF Execution of -e aborted due to compilation errors.