usv has asked for the wisdom of the Perl Monks concerning the following question:

perl -mstrict -we 'open F,">/dev/full";print F 1;close F'
Why no errors?
perl -V

Replies are listed 'Best First'.
Re: write to /dev/full does not fail
by davido (Cardinal) on May 23, 2014 at 05:25 UTC

    It does fail, but you need to check for failure.

    open, print, and close return a true value upon success, and a false value upon failure. They don't inherently throw an exception. If you want an exception to be thrown, you must test the return values and throw it.

    In the case of your code, it's the "close" that returns a failure status. The standard idiom is this:

    open my $fh, '>', '/dev/full' or die $!; print $fh "Hello world!\n"; close $fh or die $!;

    In this case, the error would be, "No space left on device at mytest.pl line 8.", where line 8 in my code is actually the "close" line.

    The autodie pragma would handle this automatically for you. However, in my testing it isn't effective when evoked like this:

    perl -mstrict -mautodie -e '..........'

    For some reason (which I haven't investigated), one must invoke it like this:

    perl -mstrict -e 'use autodie; ........... '

    Dave

      You need
      perl -Mautodie ...

      i.e. a captial M. Otherwise you're doing the equivalent of

      use autodie ();
      and thus enabling autodie only for a list of zero builtins.

      Dave.

      mmh.. intersting: why Perl does not dies on print? even if autoflush is enabled?
      # this not dies perl -e '$|++; open FH, ">", "/dev/full" or die $!; print FH "line\n" + or die $!; ' # while this dies on close perl -e 'open FH, ">", "/dev/full"; print FH "line\n" ;close FH or di +e $! ' No space left on device at -e line 1. # interestingly (?) sysopen die on write, may it knows about /dev/full +? perl -e 'sysopen (FH, "/dev/full", O_WRONLY) ; print FH "line\n" or d +ie $!;close FH ' Bad file descriptor at -e line 1.
      I expect Perl die when the print fails, at least when autoflush is on.

      why this behaviour?

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        Interestingly, if you print a lot, print itself will die:
        perl -wE 'open my $F, ">", "/dev/full" or die "open:$!"; for (1..10000) { print $F "line\n" or die "print:$!"; }; close $F or die "close:$!"; '
        print:No space left on device at -e line 3.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        select(FH); before you increment $|. As-is, you just set autoflush on STDOUT, with no effect on FH.