Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Why does File::Temp use sysopen?

by John M. Dlugosz (Monsignor)
on May 02, 2005 at 16:30 UTC ( [id://453318]=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I found an interesting issue when running under Windows where "normal" text files have CRLF line endings.

Basically, using a generated temporary file from File::Temp was not giving the normal endings, but were "binary" by default.

use File::Temp; use File::Spec; print "hello1\n"; # defaults to CRLF, as documented. open F, ">", "out2.txt"; # ditto. print F "hello2\n"; open F, "> :utf8", "out3.txt"; # still CRLF print F "hello3\n"; open F, ">", "out4.txt"; binmode F, ":utf8"; # still OK! print F "hello4\x{b6}\n"; $h= new File::Temp( TEMPLATE => 'helloXXXXX', SUFFIX => $suffix, UNLIN +K => 0, DIR => File::Spec->tmpdir()); $fname= $h->filename; print "sending out5 to $fname\n"; # binmode $h, ":utf8"; # no CR, shows 0x0A only. But :utf8 silences +warning. print $h "hello5\x{b6}\n";
Looking at the Perl code for File::Temp, I see that it uses sysopen rather than the normal open. I think that's why it behaves differently.

Why?

And why does sysopen not set up the platform defaults, though it still appears to use IO layers?

—John

Update: behavior on Perl 5.8.4 (ActiveState build 810), but is fixed in Perl 5.8.6 (AS build 811)

Replies are listed 'Best First'.
Re: Why does File::Temp use sysopen?
by revdiablo (Prior) on May 02, 2005 at 17:57 UTC
    Why does File::Temp use sysopen?

    Because sysopen lets you use some useful open flags. Afaik, File::Temp uses this to avoid race conditions that would be created trying to emulate the same behavior with a plain open.

    Update: Indeed, File::Temp uses the O_EXCL flag by default, which does the following:

    In many systems the O_EXCL flag is available for opening files in exclusive mode. This is not locking: exclusiveness means here that if the file already exists, sysopen() fails.

    That way it can keep retrying until it gets a file that didn't exist. Doing this with a -e check before using open would cause a race condition.

Re: Why does File::Temp use sysopen? (binmode)
by tye (Sage) on May 02, 2005 at 19:19 UTC

    You should test your thesis, because using sysopen on Win32 does not cause a file to be in binmode for me ("\n" gets written as "\r\n" when I print "\n" to a such a handle).

    - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-24 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found