Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Why does File::Temp use sysopen?

by John M. Dlugosz (Monsignor)
on May 02, 2005 at 16:30 UTC ( #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.


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


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://453318]
Approved by kvale
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-01-21 00:35 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.