File Coverage

blib/lib/String/Snip.pm
Criterion Covered Total %
statement 29 30 96.6
branch 5 6 83.3
condition 9 10 90.0
subroutine 6 6 100.0
pod 1 1 100.0
total 50 53 94.3


line stmt bran cond sub pod time code
1             package String::Snip;
2             $String::Snip::VERSION = '0.002';
3 2     2   50232 use strict;
  2         5  
  2         44  
4 2     2   9 use warnings;
  2         3  
  2         51  
5              
6 2     2   1361 use String::Truncate;
  2         737478  
  2         12  
7              
8             {
9             our $the_closure;
10              
11             sub _hook_closure{
12 68     68   86 return &{$the_closure}(@_);
  68         132  
13             }
14              
15             sub snip{
16 9     9 1 32 my ( $string, $opts ) = @_;
17 9   50     24 $string ||= '';
18 9   100     32 $opts ||= {};
19 9   100     35 my $max_length = $opts->{max_length} || 2000;
20 9   100     37 my $short_length = $opts->{short_length} || 100;
21 9   100     36 my $substr_regex = $opts->{substr_regex} || '\\S+';
22 9 100       24 if( $max_length < 100 ){ $max_length = 100; }
  1         3  
23 9 50       20 if( $short_length < 50 ){ $short_length = 50 };
  0         0  
24              
25             local $the_closure = sub{
26 68     68   126 my ($str) = @_;
27 68 100       146 if( length($str) < $max_length ){
28 60         222 return $str;
29             }
30 8         11 my $chardiff = length($str);
31 8         53 return String::Truncate::elide($str, $short_length,
32             { truncate => 'middle',
33             marker => ' ..[SNIP (was '.$chardiff.'chars)].. '
34             });
35 9         39 };
36 9         91 $string =~ s/($substr_regex)/_hook_closure($1)/egs;
  68         245  
37 9         323 return $string;
38             }
39             }
40              
41             1;
42              
43             __END__