in reply to print to clipboard

Here you go, this script reads itself into the clipboard then reads that back.....

use Win32::Clipboard; my $cb = Win32::Clipboard(); $cb->Empty(); open F, $0 or die $!; $cb->Set( do{ local $/; <F> } ); close F; $cb->WaitForChange(); printf "Clipboard contains:\n\n%s\n", $cb->Get();

cheers

tachyon

Replies are listed 'Best First'.
Re^2: print to clipboard
by tilly (Archbishop) on Aug 24, 2004 at 15:24 UTC
    If you'll be cutting and pasting stuff to and from the clipboard as well as reading and writing it, then you'll want to convert \n to \r\n on the way to the clipboard, and vice versa on the way back. Otherwise you'll see \r in what you read, and when you paste other programs won't know what to do with the plain \n.

    This conversion is automatically done for you when you read from/write to textfiles, but you have to do it yourself with Win32::Clipboard.

      I appreciate the point but the code above runs as expected vis CRLF. The thing with Win32 clipboard is that it kinda makes conversions unnecessary. You just don't paste from Win32 to Gnome. And if you remain on Win32 then the line endings simply remain as they should be.

      cheers

      tachyon

        I'm sure that the above program worked in your test. But try the following experiment:

        Open up Notepad, write something, and copy it to the clipboard. Read the clipboard with Win32::Clipboard. Look for whether you now see \r inside Perl.

        Save the file from Notepad to the filesystem. In Perl, read that file, and copy it to the clipboard. Paste the clipboard into Notepad, and see how it deals with the straight \n's that you see.

        When I tried this a long time ago (I no longer deal with Windows, so it might have changed), the results were that other programs put \r\n into the clipboard and expected to find \r\n when they copied from it. Therefore you need to handle the conversion with the Win32 clipboard if you want to be able to get smooth interactions between Perl and other Windows programs.