in reply to write to /dev/full does not fail

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

Replies are listed 'Best First'.
Re^2: write to /dev/full does not fail
by dave_the_m (Monsignor) on May 23, 2014 at 06:37 UTC
    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.

Re^2: write to /dev/full does not fail
by Discipulus (Canon) on May 23, 2014 at 07:44 UTC
    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.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        no, it seems to me it always, only dies on close:
        #this not dies perl -e 'open FH, ">", "/dev/full" or die $!; for (1..100) {print FH " +line\n" or die $!;} ' #this too not dies perl -e '$|++;open FH, ">", "/dev/full" or die $!; for (1..100) {prin +t FH "line\n" or die $!;} ' #only this dies, on close perl -e 'open FH, ">", "/dev/full" or die $!; for (1..100) {print FH +"line\n" or die $!;}; close FH or die $!' No space left on device at -e line 1.
        UPDATE: as suggested by choroba 100 are few (magic number seems to be 820):
        # this still does not die perl -e '$|++;open FH, ">", "/dev/full" or die $!; for (1..819) {print + FH "line\n" or die $!;} ' #but this dies! perl -e '$|++;open FH, ">", "/dev/full" or die $!; for (1..1000) {pri +nt FH "line\n" or die $!;} ' No space left on device at -e line 1.
        820 is to much? Perl becomes bored after 820 errors (not spotted)? is very patient: i'm hurted by second error.. ;=)
        # perl -e 'print "$_\n" for 1..10000' | perl -ne '$num = <STDIN>;print + "$num:";open FH, ">", "/dev/full" or die $!; for (1..$num) {print FH + "line\n" or die $!;};close FH;' .. :816 :818 :820 No space left on device at -e line 1, <STDIN> line 410.
        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.
      select(FH); before you increment $|. As-is, you just set autoflush on STDOUT, with no effect on FH.