我的 Ubuntu 14.04 LTS 中安装了 Perl 5.18.2 版本。
现在我在 geany 中创建了一个自定义命令,如下所示:
perl /home/ubuntu/geany_custom_cmds/get_current_directory_path.pl %f %d %e
下面是
get_current_directory_path.pl
文件的代码:
#!/usr/bin/perl -w
# print $ARGV[0];
use Clipboard;
foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
Clipboard->copy($ARGV[$argnum]);
print Clipboard->paste;
}
但是我在 Ubuntu 剪贴板中没有获得任何参数值,我可以通过 Ctrl + V 进行粘贴。
欢迎任何建议。
Clipboard
模块的编写在 Windows、Mac 和 Linux 系统上同样具有良好的性能。为了实现这一目标,它具有三个不同的驱动程序模块来实现该功能
Linux 驱动程序使用
xclip
命令行工具,Ubuntu 上默认情况下不安装该工具。您必须安装它
sudo apt install xclip
在模块为您工作之前
模块的文档中确实应该有对此效果的注释,因为它远非显而易见
问题在于 X-windows 具有三种不同的剪切和粘贴概念:
参考:https://linux.die.net/man/1/xsel
显然 perl Clipboard 包只知道主要选择。 您可能需要“主要”和“剪贴板”,或者至少需要“剪贴板”。
在 X-windows 下解决此问题的工作代码可以在 https://www.av8n.com/security/Xclip.pm
找到调用为:
use Xclip;
Xclip::copy2("some stuff");
需要安装
xsel
或 xclip
。
在 Weyland 上,xclip 不会粘贴到剪贴板,您需要 wl-clip。我编写了一个将“做正确的事情”的模块,调用为:
#!/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 ><
这是代码:
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))
}