in reply to Re^2: When to use eval
in thread When to use eval

I have never used autodie, but my understanding that it just makes functions that normally return false on failure die instead. So if you have this code:

open my $fh, '<', 'filename.txt';

...and you have autodie in use, the code will behave as if it were written like this:

open my $fh, '<', 'filename.txt' or die $!;

Myself, I prefer to be able to see within the code if something is designed to die explicitly or not so I would never use such a contraption as autodie.

Replies are listed 'Best First'.
Re^4: When to use eval
by haj (Vicar) on Feb 01, 2024 at 19:30 UTC

    Actually, autodie is way better than or die $!. It would rather be like this:

    open my $fh, '<', $filename or die qq(Can't open $filename for $mode: '$!')

    It takes care to inform about the filename (in quotes, so that it stands out against the constant text), the open mode, and also wraps $! in quotes. That is exactly why I often use autodie, especially in short and quick programs when I don't want to spend time on crafting error messages.

    Which error message would you prefer:

    Permission denied at demo1.pl line 2.
    Can't open 'filename.txt' for reading: 'Permission denied' at demo2.pl line 2

      I should have provided a better example. What autodie outputs is near identical to the diagnostic info I do manually. Nonetheless, it does do it automatically :)