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

Are there good modules to help me copy and paste things in perl? Specifically, a way to put something into the paste buffer (or whatever it's called). I.E. Rather than output my results to stdout, highlight with mouse, copy and then paste, Id like to run the script and have the results ready to paste immediately.

I'm thinking of writing a password-safe like app in perl. I don't want to do gui interface, but I do want to paste passwords without having to first copy them from the screen.

I work in Linux and OpenBSD. So my first choice would be some wonderful portable "it just works" module that can handle X windows and console mousy stuff (curses maybe?).


Replies are listed 'Best First'.
Re: Copy and Paste?
by zentara (Archbishop) on Jun 11, 2008 at 17:54 UTC
    Here are a couple of ideas.

    This one will let you put something into the Paste selection menu of many apps, but you need X windows running.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Clipboard; my $mw = tkinit; $mw->withdraw; #use Tk without showing a window my $content = 'foobar'.time; print "$content\n"; $mw->clipboardClear; $mw->clipboardAppend($content); MainLoop;

    or this one will let you copy into the mouse paste buffer. Unfortunately, you need to keep the Tk program going untill you make the mouse Paste. The programs' Paste buffer gets cleared when Tk exits.

    #!/usr/bin/perl use warnings; use strict; use Tk; # a "phantom newline"-free copy'n'paste to # the mouse clipboard my $mw = tkinit; my $textbox = $mw->Text(); #never packed $mw->Button(-text =>'CLipAppend', -command=>sub{ $textbox->clipboardClear; $textbox->delete('1.0','end'); $textbox->insert('end','foobarz'); $textbox->selectAll; #this next line must come after the selectAll $textbox->delete('end - 1 chars','end'); $textbox->clipboardColumnCopy; print chr(07); #beep })->pack(); MainLoop;

    Perl Gtk2 has some similar things, google for "Gtk2::Clipboard"

    I'm not really a human, but I play one on earth CandyGram for Mongo
Re: Copy and Paste?
by Thelonius (Priest) on Jun 11, 2008 at 18:03 UTC
    The CPAN module Clipboard says it does what you want. I haven't tried it.

      Maybe it's worth mentioning that Clipboard is just using xclip under the hood (on unix-ish systems). In other words, xclip would need to be installed for it to work... — which unfortunately often isn't the case, as xclip doesn't belong to the base tools that come with X11.

      In some cases, you might find xsel installed instead, which is another similar utility.


      BTW, seems like a good occasion to advertise a little trick: you may use xclip to conveniently copy files between machines when you already have a working X connection (e.g. ssh login with X forwarding enabled), and other means like scp or s/ftp would require you to re-authenticate.

      For example, when I log into client sites, I often have to go through a somewhat cumbersome procedure, requiring me to enter various passphrases, SecureID tokens and whatnot, until I finally have my SSH/X connection... (yes I know there's ssh-agent, but it doesn't always help). In these cases, xclip comes to rescue. I.e. with the following two aliases in my ~/.bashrc (though it of course also works without aliases):

      alias Xcopy='xclip -selection secondary -loops 1 -i <' alias Xpaste='xclip -selection secondary -o >'

      I can just type

      $ Xcopy myapp-latest-patches.tar.gz

      on the local machine. And then, on the machine I'm logged in remotely:

      $ Xpaste myapp-latest-patches.tar.gz

      to have the tarball transferred without needing to retype my credentials every time.

      (But mind you, however neat this seems, it isn't the right tool for copying Gigabyte-sized volumes... :)


Re: Copy and Paste?
by ambrus (Abbot) on Jun 11, 2008 at 18:21 UTC

    To put something in the paste buffer, you can try to install the simple application called xclip that can read stuff from stdin and put it into the X clipboard. There's a problem with terminology though because there are two or three completely different clipboards X programs use and I don't really understand how all that works.