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

I'm sure I'm missing something truly obvious.

After running this script, the result pastes into Word and Wordpad on two lines as expected, but as one line in Notepad and other text boxes.

use Win32::Clipboard;

$CLIP = Win32::Clipboard();

$a = "Line1\nLine2";

$CLIP->Set($a);

Replies are listed 'Best First'.
Re: newlines in Clipboard
by MZSanford (Curate) on Oct 25, 2001 at 18:41 UTC
    This is because Word/Wordpad accept \n (Unix line ending) as well as \r\n (Windows Line Ending). Notepad does not. Change to this :
    $a = "Line1\r\nLine2";

    i had a memory leak once, and it ruined my favorite shirt.
      New here: is it proper to say thank you? Thank you.
Re (tilly) 1: newlines in Clipboard
by tilly (Archbishop) on Oct 25, 2001 at 23:18 UTC
    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.)
      Thank you.