in reply to Catching error in DateTime::Format

Actually using $@ as a boolean is a bug waiting to happen. It relies on the exception being a non-empty non-zero string, and strange interactions between object destructors can sometimes break that assumption.

A safer pattern is

unless (eval { ...; 1 }) { ... # handle the error in $@ }
or for simpler cases,
eval { ...; 1 } or return undef; return undef unless eval { ...; 1 };
Eval is guaranteed to return a false value on an exception, so you ensure your expression ends with a true value, and now you aren't at the mercy of the exception text/object.

Replies are listed 'Best First'.
Re^2: Catching error in DateTime::Format
by ysth (Canon) on Oct 29, 2025 at 19:06 UTC
      use 5.040; ... In 5.34 to 5.[3]8 ...

      Or just use Feature::Compat::Try and save any bother over which version of perl might be running it.


      🦛

      5.48 should be 5.38