A fuller explanation.

Windows text files store returns as a combination of two characters, character 13 then 10. (Which is conveniently represented as "\r\n" on that platform.) Perl is from Unix, which is used to using a single character, character 10 (ie "\n") for a line ending. For the sake of portability, Perl does this conversion by default when reading or writing files (you can use binmode to turn this conversion off). However the Win32::Clipboard module does not do this conversion.

Therefore returns in Windows come in through the clipboard as "\r\n". And many programs will want to see them come back as "\r\n" as well. Some can work around it, but others cannot. If doing this escaping yourself sounds like too much of a bother, you can do this:

# Like Get but handles incoming returns sub Win32::Clipboard::GetEsc { my $self = shift; my $text = shift; $text =~ s/\r\n?/\n/g; $self->Get($text); } # Like Set but escapes outgoing returns. sub Win32::Clipboard::SetEsc { my $self = shift; my $text = shift; $text =~ s/\r\n?|\n/\r\n/g; $self->Set($text); }
After putting that that bit of code in your program you can GetEsc and SetEsc and things will Work As Expected. (Feel free to edit your own copy of Win32::Clipboard to add these functions. If you do that, then you don't need to include Win::Clipboard:: in the names. See the package command to learn why not.)

In reply to Re (tilly) 1: newlines in Clipboard by tilly
in thread newlines in Clipboard by mstout

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.