Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

mysterious error of read function in solaris

by david2008 (Scribe)
on Jan 19, 2012 at 13:30 UTC ( #948778=perlquestion: print w/replies, xml ) Need Help??
david2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have the following script
use strict; use warnings; my ($output_file) = 'a.exe'; open INPUT, $output_file or die "Cannot read the installer fil +e $output_file: $!"; binmode INPUT; my $content = ""; my $buffer; while ( read (INPUT, $buffer, 65536) # read in (up to) 64k +chunks, write ) { if ($!){die $!} $content.=$buffer; }; die "Cannot read the installer file $output_file: $!" if $! && + ($! ne 'Bad file descriptor'); close INPUT; return $content;
When i run it on linux it works, but on solaris i get an error message in the read command.
Bad file number at line 13.<br>
What can be the reason? Thanks, David

Replies are listed 'Best First'.
Re: mysterious error of read function in solaris
by Anonymous Monk on Jan 19, 2012 at 13:55 UTC

    Only check $! right after a system call

    open ... or die $!;
    close ... or die $!;
    print ... or die $!;
    my $read = read ...;
    die $! unless defined $read;
Re: mysterious error of read function in solaris
by JavaFan (Canon) on Jan 19, 2012 at 14:27 UTC
    You're dying whenever $! is a non-false value. But $! having a non-false value is not an indication anything is wrong. Check the system call, only if that is false, $! will have a useful value. In all other cases, you may not rely on $! having a sensible value.

    In this, my bet is that Solaris sets errno to 13 during one of the system calls that are being made. And an OS is free to do so, the 13 only needs to signal something if the call fails (which it won't, because if the read fails, you aren't testing $!).

      Which variable should i check if read fails?
        Whatever variable you put the return value of read in. From perldoc -f read
        Returns the number of characters actually read, 0 at end of file, or undef if there was an error (in the latter case $! is also set).
        So, you'd do something like:
        my $r; while ($r = read ...) { ... do stuff here ... } die "read failed: $!" unless defined $r;
        I guess since 5.10, you could also write:
        while (read(...) // die "read failed: $!") { ... do stuff here ... }
        but I don't think I would want to recommend that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://948778]
Approved by marto
[Corion]: ... an aggregator site that also listed (ideally) all of these links once. And maybe also have an exclude list to blacklist some of the most spammy links that the site may have never mentioned at all
[marto]: I gave up reading HN at all, and feel much better for it :P
[Corion]: Now, how to best automate that Google search ... :-)
[Corion]: marto: Yeah, I'm also short of not reading it at all. I already stopped reading it in the morning because it gave me a foul mood.
[Corion]: The good posts on HN are the non-computing posts. Most of the other stuff is maybe relevant to you if you are 20 and live in Silicon Valley...
[Corion]: Two attributes that don't describe me.
[Corion]: Maybe I should also write a curator for HN or simply not read it anymore, like you do.

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2017-07-24 13:26 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (354 votes). Check out past polls.