/usr/share/perl5/Text/Clip.pm is in libtext-clip-perl 0.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | package Text::Clip;
BEGIN {
$Text::Clip::VERSION = '0.0014';
}
# ABSTRACT: Clip and extract text in clipboard-like way
use Any::Moose;
has data => qw/ reader data writer _data required 1 /;
has [qw/ start head tail mhead mtail /] => qw/ is rw required 1 isa Int default 0 /;
has _parent => qw/ is ro isa Maybe[Text::Clip] init_arg parent /;
has found => qw/ is ro required 1 isa Str /, default => '';
has content => qw/ is ro required 1 isa Str /, default => '';
has _matched => qw/ init_arg matched is ro isa ArrayRef /, default => sub { [] };
sub matched { return @{ $_[0]->matched } }
has matcher => qw/ is ro /, default => undef;
has default => qw/ is ro lazy_build 1 isa HashRef /;
sub _build_default { {
slurp => '[)',
} }
sub BUILD {
my $self = shift;
my $data = $self->data;
if ( ref $data ne 'SCALAR' ) {
chomp $data;
$data .= "\n" if length $data;
$self->_data( \$data );
}
}
sub _fhead ($$) {
my ( $data, $from ) = @_;
my $i0 = rindex $$data, "\n", $from;
return $i0 + 1 unless -1 == $i0;
return 0;
}
sub _ftail ($$) {
my ( $data, $from ) = @_;
my $i0 = index $$data, "\n", $from;
return $i0 unless -1 == $i0;
return -1 + length $$data;
}
sub parent {
my $self = shift;
if ( my $parent = $self->_parent ) { return $parent }
return $self; # We are the base (root) split
}
sub is_root {
my $self = shift;
return ! $self->_parent;
}
sub _strip_edness ($) {
my $slurp = $_[0];
$slurp->{chomp} = delete $slurp->{chomped} if
exists $slurp->{chomped} && not exists $slurp->{chomp};
$slurp->{trim} = delete $slurp->{trimmed} if
exists $slurp->{trimmed} && not exists $slurp->{trim};
}
sub _parse_slurp ($@) {
my $slurp = shift;
my %slurp = @_; # Can/will be overidden
_strip_edness \%slurp;
if ( ref $slurp eq 'HASH' ) {
$slurp = { %$slurp };
_strip_edness $slurp;
%slurp = ( %slurp, %$slurp );
}
else {
$slurp =~
m{^
([\@\$])?
([\(\[])
([\)\]])
(/)?
}x or die "Invalid slurp pattern ($slurp)";
$slurp{wantlist} = $1 eq '@' ? 1 : 0 if $1;
$slurp{slurpl} = $2 eq '[' ? 1 : 0;
$slurp{slurpr} = $3 eq ']' ? 1 : 0;
$slurp{chomp} = 1 if $4;
}
return %slurp;
}
sub find {
return shift->split( @_ );
}
sub split {
my $self = shift;
my $matcher;
$matcher = shift if @_ % 2; # Odd number of arguments
my %given = @_;
my $data = $self->data;
my $length = length $$data;
return unless $length; # Nothing to split
my $from = $self->_parent ? $self->tail + 1 : 0;
return if $length <= $from; # Was already at end of data
pos $data = $from;
return unless $$data =~ m/\G[[:ascii:]]*?($matcher)/mgc;
my @match = map { substr $$data, $-[$_], $+[$_] - $-[$_] } ( 0 .. -1 + scalar @- );
shift @match;
my $found = shift @match;
my ( $mhead, $mtail ) = ( $-[1], $+[1] - 1 );
my $head = _fhead $data, $mhead;
my $tail = _ftail $data, $mtail;
# TODO This is hacky
my @matched = @match;
my $content = substr $$data, $head, 1 + $tail - $head;
my $split = __PACKAGE__->new(
data => $data, parent => $self,
start => $from, mhead => $mhead, mtail => $mtail, head => $head, tail => $tail,
matcher => $matcher, found => $found, matched => \@matched,
content => $content,
default => $self->default,
);
return $split unless wantarray && ( my $slurp = delete $given{slurp} );
return ( $split, $split->slurp( $slurp, %given ) );
}
sub slurp {
my $self = shift;
my $slurp = 1;
$slurp = shift if @_ % 2; # Odd number of arguments
my %given = @_;
my $split = $self;
_strip_edness \%given;
my %slurp = _parse_slurp $self->default->{slurp};
exists $given{$_} and $slurp{$_} = $given{$_} for qw/ chomp trim /;
%slurp = _parse_slurp $slurp, %slurp unless $slurp eq 1;
my @content;
push @content, $self->parent->content if $slurp{slurpl};
push @content, $split->preceding;
push @content, $split->content if $slurp{slurpr};
my $content = join '', @content;
if ( $slurp{trim} ) {
s/^\s*//, s/\s*$//, for $content;
}
if ( wantarray && $slurp{wantlist} ) {
@content = grep { $_ ne "\n" } split m/(\n)/, $content;
@content = map { "$_\n" } @content unless $slurp{chomp};
return @content;
}
else {
return $content;
}
}
sub preceding {
my $self = shift;
my $data = $self->data;
my $length = $self->head - $self->start;
return '' unless $length;
return substr $$data, $self->start, $length;
}
sub pre { return shift->preceding( @_ ) }
sub remaining {
my $self = shift;
my $data = $self->data;
return $$data if $self->is_root;
my $from = $self->tail + 1;
my $length = length( $$data ) - $from + 1;
return '' unless $length;
return substr $$data, $from, $length;
}
sub re { return shift->remaining( @_ ) }
sub match {
my $self = shift;
my $ii = shift;
return $self->found if $ii == -1;
return $self->_matched->[$ii];
}
sub is {
my $self = shift;
my $ii = shift;
my $is = shift;
return unless defined ( my $match = $self->match( $ii ) );
if ( ref $is eq 'Regexp' ) { $match =~ $is }
else { return $match eq $is }
}
1;
__END__
=pod
=head1 NAME
Text::Clip - Clip and extract text in clipboard-like way
=head1 VERSION
version 0.0014
=head1 SYNOPSIS
$data = <<_END_
# Xyzzy
# --- START
qwerty
1 2 3 4 5 6
8 9 10 The end
# abcdefghi
jklmnop
_END_
$mark = Text::Clip->new( data => ... )->find( qr/#\s*--- START/ )
( $mark, $content ) = $mark->find( qr/ The end/, slurp => '[]' )
C<$content> =
# --- START
qwerty
1 2 3 4 5 6
8 9 10 The end
Alternatively, with
( $mark, $content ) = $mark->find( qr/ The end/, slurp => '()' )
C<$content> =
qwerty
1 2 3 4 5 6
=head1 DESCRIPTION
Text::Clip allows you to mark/slice up a piece of text. String matching (by regular expression, etc.) is used to place marks. The first mark lets you access the text preceding and following the mark. Subsequent marks allow you to slurp up the text "clipped" between the marks.
=head1 AUTHOR
Robert Krimen <robertkrimen@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2010 by Robert Krimen.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|