# explicit my $str = 'Just another perl hacker'; print trim($str, 4); # 'Just' # implicit $_ = 'Just another perl hacker'; print trim(undef, 4); # 'Just' #### # $_ is set somewhere else to a value, e.g. through readline(). # The user programs a bug and $foo is unexpectedly still undef. print trim($foo, 4); # is expected to operate on $foo, but # really returns the first 4 characters of $_ #### substr 'Just another substr', 0, 4; # 'Just' #### # string based my $str = 'Just another index+substr'; substr $str, 0, index $str, 'a'; # 'Just ' # regex based my $pattern = qr/o/; 'Just another zero-width positive look-behind' =~ s/\K$pattern.*//; # 'Just an' #### @info = dismantle $str; #### my $length = shift @info; # now do something with $length # now do something with the remaining array of characters #### my @elements = split //, 'Just another split on empty regex'; my $length = scalar @elements; # or here, just @elements #### use parent qw(Exporter); our @EXPORT_OK = qw( trim strim dismantle ); #### use String::Iota qw(trim strim dismantle); my $str = 'Just another perl hacker'; print trim($str, 4); # 'Just' print strim($str, 'p'); # 'Just another ' @info = dismantle($str); print join q{, }, @info; # '24, J, u, s, t, , a, n, o, t, h, e, r, , p, e, r, l, , h, a, c, k, e, r' { open my $fh, '<', 'test.txt'; while (<$fh>) { trim($_, 40); # Trim every line of test.txt to 40 characters strim($_, q{#}); # Get rid of comments } } #### =head3 trim C<< trim(I<$string>, I<$num>) >> Trims a string to a certain length, specified by $num.