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.001';
3 2     2   30214 use strict;
  2         3  
  2         89  
4 2     2   8 use warnings;
  2         2  
  2         57  
5              
6 2     2   949 use String::Truncate;
  2         28585  
  2         12  
7              
8             {
9             our $the_closure;
10              
11             sub _hook_closure{
12 68     68   49 return &{$the_closure}(@_);
  68         82  
13             }
14              
15             sub snip{
16 9     9 1 26 my ( $string, $opts ) = @_;
17 9   50     18 $string ||= '';
18 9   100     27 $opts ||= {};
19 9   100     25 my $max_length = $opts->{max_length} || 2000;
20 9   100     28 my $short_length = $opts->{short_length} || 100;
21 9   100     52 my $substr_regex = $opts->{substr_regex} || '\\S+';
22 9 100       21 if( $max_length < 100 ){ $max_length = 100; }
  1         2  
23 9 50       12 if( $short_length < 50 ){ $short_length = 50 };
  0         0  
24              
25             local $the_closure = sub{
26 68     68   82 my ($str) = @_;
27 68 100       102 if( length($str) < $max_length ){
28 60         143 return $str;
29             }
30 8         8 my $chardiff = length($str);
31 8         38 return String::Truncate::elide($str, $short_length,
32             { truncate => 'middle',
33             marker => ' ..[SNIP (was '.$chardiff.'chars)].. '
34             });
35 9         39 };
36 9         97 $string =~ s/($substr_regex)/_hook_closure($1)/egs;
  68         183  
37 9         252 return $string;
38             }
39             }
40              
41             1;
42              
43             __END__