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

print to clipboard

by tomazos (Deacon)
on Aug 24, 2004 at 06:26 UTC ( #385339=perlquestion: print w/replies, xml ) Need Help??
tomazos has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. I'm using activestate's win32 port of perl and I want to put stuff into the clipboard. What would be cool is a filehandle I could print to and then be able to paste the output. Any ideas?

Answer Found:

use Win32::Clipboard; $CLIP = Win32::Clipboard(); print "Clipboard contains: ", $CLIP->Get(), "\n"; $CLIP->Set("some text to copy into the clipboard"); $CLIP->Empty(); $CLIP->WaitForChange(); print "Clipboard has changed!\n";

Andrew Tomazos  |  |

Replies are listed 'Best First'.
Re: print to clipboard
by Chady (Priest) on Aug 24, 2004 at 08:23 UTC

    You can tie a filehandle:

    #!perl -w use Win32::Clipboard; my $CLIP = Win32::Clipboard(); print "This gets printed to STDOUT\n"; tie *STDOUT, 'clippy', \$CLIP; print "This will end up into the clipboard."; untie *STDOUT; package clippy; use Tie::Handle; use vars qw(@ISA); @ISA = qw(Tie::Handle); sub TIEHANDLE { my $class = shift; my $clip = shift; return bless({clip => $clip}, $class); } sub PRINT { my $self = shift; ${$self->{clip}}->Set(@_); }

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady |
Re: print to clipboard
by tachyon (Chancellor) on Aug 24, 2004 at 06:44 UTC

    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();



      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.



Re: print to clipboard
by bart (Canon) on Nov 12, 2006 at 15:54 UTC
    That's the code directly copied from the synopsis of Win32::Clipboard. However, it does far too much and it doesn't do the right thing. Actually that sample makes no sense as a whole. Because $CLIP->Empty() clears the clipboard, you end up with nothing.

    Shorter, working code is

    use Win32::Clipboard; $CLIP = Win32::Clipboard(); $CLIP->Set("some text to copy into the clipboard");
    Or, shorter still:
    use Win32::Clipboard; Win32::Clipboard("some different text to copy into the clipboard");

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://385339]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2018-05-20 18:24 GMT
Find Nodes?
    Voting Booth?