in reply to How to print Unix newlines in Strawberry Perl on Windows

evgen-l:

I defer to the correct answers below.

You can set the output record delimiter ($/ see perlvar for details) like so:

print "default line ending:\n"; { local $/="\n"; print "Unix line ending:\n"; } { local $/="\r\n"; print "Win/DOS line ending:\n"; } { local $/="\r"; print "Antique MAC line ending:\n"; }

Note: Untested, etc... Standard disclaimers apply.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: How to print Unix newlines in Strawberry Perl on Windows
by BrowserUk (Patriarch) on Feb 20, 2013 at 12:32 UTC

    In addition to Athanasius' caveats, realise that when you write "\n" to a text file, the OS will add the "\r" for you.

    Thus your advice to use local $/="\r\n"; will result in "\r\r\n" being written if the file is in text mode.

    In order to avoid that addition, you must binmode the file. Or remove the :crlf layer from the perlIO stack.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: How to print Unix newlines in Strawberry Perl on Windows
by Athanasius (Archbishop) on Feb 20, 2013 at 12:25 UTC

    $/ is the input record separator. The output record separator is $\. See The parable of the falling droplet.

    Also, the default value of $\ is undef (meaning that nothing will be automatically appended to each printed line). If you set $\ as shown, you should omit the trailing "\n":

    { local $\ = "\r\n"; print "Win/DOS line ending"; }

    — otherwise, you will get "\n\r\n" printed at the end of the string.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,