Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?

by jira0004 (Monk)
on Dec 23, 2005 at 00:11 UTC ( #518664=perlquestion: print w/replies, xml ) Need Help??

jira0004 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

On Windows the line terminator is the two charcter sequence \x0d \x0a. On UNIX the line terminator is the single character \x0a.

If you are processing a file and you do not use binmode on the file handle, then Perl automatically uses and recognizes the native line terminator for your platform \x0d \x0a on Windows, \x0a on UNIX. Thus, $x = <FILE_IN>; will result in the last character sequence in $x being \x0d \x0a on a Windows platform and \x0 on a UNIX platform. Likewise, print FILE_OUT "Hello.\n"; will result in H e l l o . \x0d \x0a being written to the output file on Windows and H e l l o . \x0a on UNIX.

In my Perl script I allow the user to specify which line terminator to use on the command-line so that the user can get either a file with Windows line terminators or a file winth UNIX line terminators regardless of what platform the user is running on. I am doing this because we are generating data files and I need the right line terminator in the data file and the right line terminator in this specific case is not necessarily the platform native one.

I want to default to the platform native line terminator if the user does not specify a line terminator. However, I am using binmode on my file handle so on Windows Perl won't automatically translate \n to \x0d \x0a.

What I want to know is, does Perl store the native line terminator character in a special global variable? If so which one?

Can some one tell me how I can find out dynamically at run time what the native line terminator is for the platform on which my script is running?

Thanks, Regards

Peter Jirak

jira0004@yahoo.com
  • Comment on Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?

Replies are listed 'Best First'.
Re: Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?
by Corion (Pope) on Dec 23, 2005 at 00:13 UTC

    perldoc perlvar tells us that $/ contains whatever is used to mark the end of line. And chomp removes it from the end of a string.

      And $/ is (defaults to) "\n" on every platform. And "\n" is "\cJ" aka "\x0a" on every ASCII platform (it is "\cM"/"\x0d" on the near-ASCII platform of old Macs). So $/ doesn't have much to do with the original question.

      - tye        

Re: Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?
by ysth (Canon) on Dec 23, 2005 at 04:17 UTC
    With a suitably capable version of perl, you could:
    my $newline = "\n"; $_ eq "crlf" and $newline = "\r\n" for PerlIO::get_layers(*STDOUT);
    or, if they specify a terminator, use binmode and their selection, otherwise, don't use binmode and output "\n".
      my $newline = "\n"; $newline = "\r\n" if grep $_ eq "crlf", PerlIO::ge­t_layers(*­STDOUT);

      - tye        

Re: Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?
by jmcnamara (Monsignor) on Dec 23, 2005 at 01:34 UTC

    I don't think that there is any variable or core module that you can query to get this information. You could check $^O (see perlvar) or Config to get the OS name and work it out from there. However, it might not be easy to produce a foolproof solution with this method.

    As a workaround I'd suggest writing "\n" to a temp file, binmoding the file and reading back in the character(s) used as line endings.

    --
    John.

Re: Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?
by blazar (Canon) on Dec 23, 2005 at 11:56 UTC
    In my Perl script I allow the user to specify which line terminator to use on the command-line

    [SNIP]

    Can some one tell me how I can find out dynamically at run time what the native line terminator is for the platform on which my script is running?

    Others told you how to retrieve it from layers info, by means of PerlIO. Now, the next logical step which happens to be even simpler is that you can also use layers to set the output mode once you know what you want:

    my $end = $wantunix ? 'unix' : 'crlf'; open my $out, ">:$end", 'outputfile' or die $!;

    Incidentally, you're not referring to the "file delimiter", which rather suggests the idea of EOF marker (^D under *NIX and ^Z under DOS/Win* AFAIK), but to the "end-of-line" marker.

Re: Is there a variable in which Perl stores the native file delimiter for the platform on which your script is running?
by snowhare (Friar) on Dec 23, 2005 at 20:15 UTC
    While not precisely what you asked for, Text::FixEOL will probably do what you are trying to do.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-10-22 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (229 votes). Check out past polls.

    Notices?