Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Quick and portable way to determine line-ending string?

by John M. Dlugosz (Monsignor)
on Aug 09, 2001 at 02:58 UTC ( #103272=note: print w/ replies, xml ) Need Help??


in reply to Quick and portable way to determine line-ending string?

Open a file and write "\n" to it. Re-open the file and use binmode, then read it back in. The result is the desired string.

Something like this...

open $foo, $tempfilename; print $foo "\n"; close $foo; # probably redundant, but why not. open $foo, $tempfilename; binmode $foo; read $foo, $result, 999; close $foo;
If you don't have a writable file (why not a valid temp directory??!) use a filehandle tied to a text buffer. Say, IO::Scalar.

Hmm, I tried that and it didn't work, as I half-suspected. The binmode thing is done in the C Standard library functions, and Perl might be relying on that and have no real knowledge of what it means on a given platform.

Are you sure you can't come up with a writable file, or a fake file that operates on the FD level rather than Perl's tie level?

—John


Comment on Re: Quick and portable way to determine line-ending string?
Download Code
Re: Re: Quick and portable way to determine line-ending string?
by bikeNomad (Priest) on Aug 09, 2001 at 03:03 UTC
    I mentioned doing what you suggest in my original post.

    I had thought about IO::Scalar, but it no-op's binmode(). So you can't use that.

    I believe the translation only happens on real physical disk files (I don't know about pipes, but there's no guarantee that a given platform will have pipes available).

    I'd prefer to stay away from having to write to files, because many of A::Z's users are using it in web servers; writing to a file requires knowing where a temp file can be made, and might slow things down.

      I'm pretty sure it will have effect any time the underlying system's file IO is used, whether it's a real file or whatever.

      As for slowing things down, does the line ending ever change? Figure it out once, and remember it. You could even make that part of installation.

      Here is another idea. You can have a known file containing the line ending candidate and just read it. See which one was transformed into a simple "\n".

      So, open (without binmode) lineend_mac and read it; ditto for lineend_pc and see which was a "\n". Neither really contained just a "\n".

      —John

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-08-27 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (238 votes), past polls