Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

writing to a file using print

by Brad.Walker (Sexton)
on Feb 13, 2007 at 04:31 UTC ( [id://599633]=perlquestion: print w/replies, xml ) Need Help??

Brad.Walker has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to write to a file use the print keyword. I'm doing the following:
print $test_file $random_data
$random_data is a 1MB string and $test_file is the file handle that was opened up using the 3 argument version of open.

The problem that I'm encountering is the Perl interpreter is breaking this large write into 4K writes at the system call level. I don't really want this because I'm trying to do some NFS performance measurements and the breakup of writes is causing me problems. I know that I could use syswrite, but was hoping to stay away from this.

Thanks.
-brad w.

Replies are listed 'Best First'.
Re: writing to a file using print
by dorko (Prior) on Feb 13, 2007 at 04:40 UTC
        This whole discussion gives me nasty flashbacks to a time when I couldn't have been more wrong. At that point I switched from $| = 1; to $|++;.

        I've mostly gotten over it... :)

        Cheers,

        Brent

        -- Yeah, I'm a Delt.
Re: writing to a file using print
by ikegami (Patriarch) on Feb 13, 2007 at 04:53 UTC

    Do you get the same problem with syswrite? It doesn't use the buffering system used by print. It might still not work, though, because syswrite is not guaranteed to write everything it is asked to write (in a single call).

    my $written = syswrite($test_file, $buf, length($buf)); die("Unable to write to file: $!\n") if not defined $written; die("Unable to write whole string in one call\n") if $written != length($buf);

    For comparison, syswrite is normally used as follows:

    my $offset = 0; my $to_write = length($buf); while ($to_write > 0) { my $written = syswrite($test_file, $buf, $to_write, $offset); die("Unable to write to file: $!\n") if not defined $written; $to_write -= $written; $offset += $written; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found