Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

heredoc and Carriage return

by gabrielsousa (Sexton)
on Oct 25, 2016 at 09:18 UTC ( #1174670=perlquestion: print w/replies, xml ) Need Help??

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

heredoc read the LF ( \n ) , but dont parse / stores the Carriage return ( \r )

how can i force heredoc to read and store carriers return ( \r ) ?

=====================added before======================


i have a pdf file / binary inside my perl script using heredoc
has 40 Carriage returns, i can search on VI, i see the ^M
but when i print to a file i lose all Carriage returns..
i'm assuming is a problem of heredoc

Replies are listed 'Best First'.
Re: heredoc and carriers return
by choroba (Archbishop) on Oct 25, 2016 at 09:35 UTC
    I can confirm the behaviour in 5.25.5 (built on September 16) on Linux.

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $nix = << '__NIX__'; line 1 line 2 __NIX__ my $win = << '__WIN__'; # ^M present at end of lines. line 1 line 2 __WIN__ # ^M present here, too: my $quoted = 'line1 line2 '; say /\r/ for $nix, $win, $quoted;

    I can't find it documented anywhere.

    Update: It doesn't work for normal quoted strings, either.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I suppose it's related to Perl being OS agnostic regarding \n when printing.

      Please check the hex code of the here doc to see what is really embedded.

      Edit: OK seems like the regex is supposed to do this.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      > I can't find it documented anywhere

      I'm to busy now, but you might wanna look into open which mentions a IO layer called :crlf or encoding or other docs mentioned in the footnotes ...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Neither open nor encoding describe how they influence Perl's parsing of multiline string literals.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: heredoc and carriers return
by shawnhcorey (Friar) on Oct 25, 2016 at 11:09 UTC

    Try:

    my $win = << "__WIN__"; line 1\r line 2\r __WIN__

    Using a double quote around the heredoc tag will cause the heredoc to be interpolated. That is, the "\r" will be interpolated as a CR (U+000D).

Re: heredoc and carriers return
by LanX (Sage) on Oct 25, 2016 at 13:47 UTC
    > heredoc read the LF ( \n ) , but dont parse / stores the carriers return ( \r )

    there is a misconception on your side and probably it's an XY Problem too.

    \n is the newline according to the OS you use and NOT (only) the LF from ASCII.

    When you print "some line break \n and more lines" then Perl will autotranslate the \n to whatever *nix, win or mac wants to see.

    Perl does it to assure maximum OS independency.

    You are probably bitten by the reverse effect, that some combinations of CR and LF are interpreted as \n.

    The answer to your problem is asking what for do you need it?

    If you just need linebreaks ignore the effect, if you need the \r otherwise, do like suggested and include them explicitly.

    You are free to put the text into an external file and read it in binmode , or even encode it as hex or base64 if you really need literally the same ascii code.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      i have a pdf file / binary inside my perl script using heredoc

      has 40 Carriage returns, i can search on VI, i see the ^M

      but when i print to a file i lose all Carriage returns..

      i'm assuming is a problem of heredoc
        Heredocs are not meant for binary data.

        If you don't want to read from an external file, then I can only see two options

        • try to put it after DATA and read from there (not tested)
        • encode and decode the stuff in an ASCII format, like base64

        HTH ! :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: heredoc and Carriage return
by choroba (Archbishop) on Oct 25, 2016 at 20:38 UTC
    Crossposted to reddit.

    Please, next time link to your crosspost yourself, to avoid duplicate effort by people not attending both sites.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      > to avoid duplicate effort by people not attending both sites

      Indeed! :/

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-09-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?