Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: looking for feedback on my first script

by Anonymous Monk
on Jan 09, 2021 at 20:01 UTC ( [id://11126673]=note: print w/replies, xml ) Need Help??


in reply to Re^4: looking for feedback on my first script
in thread looking for feedback on my first script

In Perl, if printf() fails, a fatal error will be thrown long before it returns to the caller.

wrong

I've never checked the return value of sprintf.

then stop talking and read sprintf

Replies are listed 'Best First'.
Re^6: looking for feedback on my first script
by Marshall (Canon) on Jan 09, 2021 at 22:21 UTC
    I disagree about printf() errors.
    With normal file operations, what I said is true. It is almost impossible for a print or printf to "fail" once a local Hard Disk file handle is open. One way would be "File system full", but in that case, a fatal exception is thrown - the calling program won't see that.

    I did make a mistake in interpreting the exact situation. I would have written: return sprintf "..." because I favor explicit return statements.

      In Perl, if printf() fails, a fatal error will be thrown long before it returns to the caller.
      I disagree about printf() errors. With normal file operations, what I said is true. It is almost impossible for a print or printf to "fail" once a local Hard Disk file handle is open. One way would be "File system full", but in that case, a fatal exception is thrown - the calling program won't see that.

      Test before posting. Confidently making incorrect statements doesn't help anyone.

      #!/usr/bin/env perl use warnings; use strict; my $file = $^O eq 'MSWin32' ? "P:\\foo" : '/tmp/foo/bar'; open my $fh, '>', $file or die "$file: $!"; my $data = "\0" x 2_000_000; print "Writing ", length $data, " bytes to $file\n"; printf $fh "%s", $data or warn "printf: $!"; close $fh or warn "close: $!"; print "Still not fatal\n"; __END__ Windows: Install https://www.ltr-data.se/opencode.html/#ImDisk As Administrator: > imdisk -a -t vm -m P: -s 1M -p "/fs:fat /q /y" > perl write.pl Writing 2000000 bytes to P:\foo printf: No space left on device at write.pl line 9. close: No space left on device at write.pl line 10. Still not fatal > imdisk -D -m P: Linux: $ mkdir -vp /tmp/foo $ sudo mount -t tmpfs -o size=1M,uid=`id -u` tmpfs /tmp/foo $ perl write.pl Writing 2000000 bytes to /tmp/foo/bar printf: No space left on device at write.pl line 9. close: No space left on device at write.pl line 10. Still not fatal $ sudo umount /tmp/foo
      I disagree about printf() errors.

      It was all about sprintf, not printf. Different cases and conditions (memory exhausted vs. disk full etc, NFS conditions and such).

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        I think this was about return values from a subroutine.

        I prefer explicit return statements except in the case of Perl sort sub routines.
        There are Monks who will disagree with this style.
        So be it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11126673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found