use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( stevenmay => <<'__EOI__', my $string = $input; if ( $string and length $string > 100 ){ $string = substr( $string, 0, 100); my ($tmp) = $string =~ /(.+)\s.*?$/s; # last space if possible $tmp or ($tmp) = $string =~ /(.+)\W.*?$/s; # bust on last non-word $tmp and $string = $tmp; } __EOI__ ikegami1 => <<'__EOI__', my ($string) = $input =~ /^(.{0,100})(?!\S)/s; __EOI__ ikegami2 => <<'__EOI__', my $string = $input; if (length($string) > 100) { $string =~ s/^(.{0,100})(?!\S)\K.*//s } __EOI__ ); $_ = 'use strict; use warnings; our $input; '.$_.' 1' for values(%tests); { local our $input = ('x' x 90).' '.('x' x 10); cmpthese(-1, \%tests); } { local our $input = ('x' x 10).' '.('x' x 90); cmpthese(-1, \%tests); } { local our $input = ('x' x 99); cmpthese(-1, \%tests); }