Reputation: 2173
I have Perl 5.18.2 version installed in my Ubuntu 14.04 LTS.
Now I have created a custom command in geany which I am firing as below:
perl /home/ubuntu/geany_custom_cmds/get_current_directory_path.pl %f %d %e
And below is the code of get_current_directory_path.pl
file:
#!/usr/bin/perl -w
# print $ARGV[0];
use Clipboard;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
Clipboard->copy($ARGV[$argnum]);
print Clipboard->paste;
}
But I am not getting any argument's value in my Ubuntu clipboard which I can paste by Ctrl + V.
Any suggestions are welcome.
Upvotes: 0
Views: 2649
Reputation: 3023
On Weyland xclip will not paste to the clipboard, you need wl-clip. I have written a module that will "do the right thing", invoke as:
#!/usr/bin/perl
use lib "$ENV{HOME}/Perl-Modules"; # Set to wherever you put SJWclip.pm
use SJWclip;
use warnings;
use warnings FATAL => 'all';
use strict;
# X Windows requires xclip installed.
# Wayland requires wl-clip and/or xclip installed.
# 1) Returns three values:
# 1a) True if get/put secondary selection is available.
# Only available if xclip is installed. Secondary clipboard is not of much use.
# 1b) True if put to the clipboard is available.
# True on X Windows.
# Only true on Wayland if wl-clip is installed.
# 1c) True if cbclear is available.
# Only true on Wayland, and only if wl-clip is installed.
# Even when true, cbclear is not available for the secondary selection.
my ($secondary, $putcb, $clear) = cbavailable();
# 3) Prints the content of the clipboard.
# This is the ^C ^V clipboard.
print cbget(), "\n";
# 4) Sets the value of the clipboard. See note 1b.
cbput('hello world');
# 5) Prints the value of the primary selection.
# This is highlighted text with left mouse button, that can be pasted with the middle mouse button.
cbselect('primary'); # use 'primary', 'secondary', or 'clipboard'. Default is clipboard.
print cbget(), "\n";
# 6) Clears the clipboard. In this case the primary selection, but it could be the clipboard.
cbclear();
print ">", cbget(), "<\n"; # This will print ><
Here is the code:
package SJWclip;
require Exporter;
use warnings;
use warnings 'FATAL' => 'all';
use strict;
use Carp;
use constant
{
'FALSE' => 0,
'TRUE' => 1,
};
our @ISA = qw(Exporter);
our @EXPORT = qw(cbget cbput cbselect cbclear cbavailable);
my $wlclip;
my $xwindows;
my $noclip = "xclip is not installed.\nIt can be installed with: sudo apt update; sudo apt install xclip";
my $xclip = FALSE;
if (exists($ENV{'WAYLAND_DISPLAY'}))
{
$xwindows = FALSE;
if (system("type wl-copy 1>/dev/null 2>&1") == 0) {$wlclip = ''}
elsif (system("type wl-clip.copy 1>/dev/null 2>&1") == 0) {$wlclip = 'clip.'}
else {$noclip = "Neither wl-clip nor xclip is installed.\nwl-clip can be installed with: sudo snap install wl-clip"}
}
if (system("type xclip 1>/dev/null 2>&1") == 0) {$xclip = TRUE}
else {croak $noclip unless defined($wlclip)}
my $selection = 'clipboard';
my $wlselection = '';
return TRUE;
sub cbget ()
{
my $cmd = "xclip -selection $selection -o 2>/dev/null";
if (defined($wlclip))
{
$cmd = "wl-${wlclip}paste$wlselection -n 2>/dev/null" unless $selection eq 'secondary';
}
return `$cmd`;
}
sub cbput($)
{
my $cmd = "xclip -selection $selection -i";
my $nl = '';
if (defined($wlclip))
{
$cmd = "wl-${wlclip}copy$wlselection -n" unless $selection eq 'secondary';
$nl = "\n";
}
else
{
croak "Put to clipboard not available on Wayland without wl-clip.\nIt can be installed with: sudo snap install wl-clip" if $selection eq 'clipboard';
}
open(XCLIP, "| $cmd") or croak("Can't open pipe to:\n$cmd\$!");
print XCLIP $_[0], $nl;
close(XCLIP);
}
sub cbselect($)
{
croak "Invalid selection:\n$_[0]" if $_[0] !~ /^(primary|secondary|clipboard)$/;
croak "Secondary selection can only be used if xclip is installed.\nIt can be installed with: sudo apt update; sudo apt install xclip" if $xclip == FALSE && $_[0] eq 'secondary';
$selection = $_[0];
$wlselection = ($selection eq 'primary') ? ' -p' : '';
}
sub cbclear()
{
croak "Cannot clear the secondary selection." if $selection eq 'secondary';
croak "Clear is only available on Wayland." if $xwindows;
croak "Clear is only available if wl-clip is installed.\nIt can be installed with: sudo snap install wl-clip" unless defined($wlclip);
my $cmd = "wl-${wlclip}copy$wlselection -fc";
croak "system call failed for: $cmd" if system($cmd) != 0;
}
sub cbavailable()
{
return ($xclip, defined($wlclip) | $xwindows, $xwindows == FALSE && defined($wlclip))
}
Upvotes: 0
Reputation: 309
The issue is that X-windows has three different cut-and-paste concepts:
Reference: https://linux.die.net/man/1/xsel
Evidently the perl Clipboard package only knows about the primary selection. You probably want both "primary" and "clipboard", or at least "clipboard".
Working code to solve this problem under X-windows can be found at https://www.av8n.com/security/Xclip.pm
Invoke as:
use Xclip;
Xclip::copy2("some stuff");
Requires either xsel
or xclip
to be installed.
Upvotes: 0
Reputation: 126722
The Clipboard
module is written to perform equally well on Windows, Mac, and Linux systems. To achieve that it has three different driver modules which implement the functionality
The Linux driver uses the xclip
command-line tool, which isn't installed by default on Ubuntu. You must install it with
sudo apt install xclip
before the module will work for you
There really should be a comment to this effect in the module's documentation, as it is far from obvious
Upvotes: 1