Help for this page

Select Code to Download


  1. or download this
        # explicit
        my $str = 'Just another perl hacker';
    ...
        # implicit
        $_ = 'Just another perl hacker';
        print trim(undef, 4); # 'Just'
    
  2. or download this
        # $_ 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 $_
    
  3. or download this
        substr 'Just another substr', 0, 4; # 'Just'
    
  4. or download this
        # string based
        my $str = 'Just another index+substr';
    ...
        my $pattern = qr/o/;
        'Just another zero-width positive look-behind'
            =~ s/\K$pattern.*//; # 'Just an'
    
  5. or download this
        @info = dismantle $str;
    
  6. or download this
        my $length = shift @info;
        # now do something with $length
        # now do something with the remaining array of characters
    
  7. or download this
        my @elements = split //, 'Just another split on empty regex';
        my $length = scalar @elements; # or here, just @elements
    
  8. or download this
        use parent qw(Exporter);
        our @EXPORT_OK = qw(
            trim strim dismantle
        );
    
  9. or download this
        use String::Iota qw(trim strim dismantle);
        my $str = 'Just another perl hacker';
    ...
                strim($_, q{#}); # Get rid of comments
            }
        }
    
  10. or download this
        =head3 trim
    
        C<< trim(I<$string>, I<$num>) >>
    
        Trims a string to a certain length, specified by $num.