blib/lib/Clipboard/Xsel.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 91 CC 2 SCAR 9.8
CC
2
Cov
0%
paste
6.0
copy
2.0
copy_to_all_selections
2.0
CRAP 2.7
1 package Clipboard::Xsel;
2 $Clipboard::Xsel::VERSION = '0.32';
3 use strict;
4 use warnings;
5  
6 use File::Spec ();
7  
8 sub copy {
9 my $self = shift;
10 my ($input) = @_;
11 return $self->copy_to_selection($self->favorite_selection, $input);
12 }
13  
14 sub copy_to_all_selections {
15 my $self = shift;
16 my ($input) = @_;
17 foreach my $sel ($self->all_selections) {
18 $self->copy_to_selection($sel, $input);
19 }
20 return;
21 }
22  
23 sub copy_to_selection {
24 my $self = shift;
25 my ($selection, $input) = @_;
26 my $cmd = '|xsel -i --'. $selection;
27 my $r = open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
28 binmode $exe, ':encoding(UTF-8)';
29 print {$exe} $input;
30 close $exe or die "Error closing `$cmd`: $!";
31  
32 return;
33 }
34 sub paste {
35 my $self = shift;
36 for ($self->all_selections) {
37 my $data = $self->paste_from_selection($_);
38 return $data if length $data;
39 }
40 return undef;
41 }
42 sub paste_from_selection {
43 my $self = shift;
44 my ($selection) = @_;
45 my $cmd = "xsel -o --$selection|";
46 open my $exe, $cmd or die "Couldn't run `$cmd`: $!\n";
47 my $result = do { local $/; <$exe> };
48 close $exe or die "Error closing `$cmd`: $!";
49 return $result;
50 }
51 # This ordering isn't officially verified, but so far seems to work the best:
52 sub all_selections { qw(primary buffer clipboard secondary) }
53 sub favorite_selection { my $self = shift; ($self->all_selections)[0] }
54  
55 sub xsel_available {
56 # close STDERR
57 open my $olderr, '>&', \*STDERR;
58 close STDERR;
59 open STDERR, '>', File::Spec->devnull;
60  
61 my $open_retval = open my $just_checking, 'xsel -o|';
62  
63 # restore STDERR
64 close STDERR;
65 open STDERR, '>&', $olderr;
66 close $olderr;
67  
68 return $open_retval;
69 }
70  
71 {
72 xsel_available() or warn <<'EPIGRAPH';
73  
74 Can't find the 'xsel' program. Clipboard.pm's X support depends on it.
75  
76 Here's the project homepage: https://vergenet.net/~conrad/software/xsel/
77  
78 EPIGRAPH
79 }
80  
81 1;
82  
83 __END__