Steven Haryanto > SHARYANTO-String-Util > SHARYANTO::String::Util



Annotate this POD


View/Report Bugs
Module Version: 0.28   Source  


SHARYANTO::String::Util - String utilities


This document describes version 0.28 of SHARYANTO::String::Util (from Perl distribution SHARYANTO-String-Util), released on 2014-07-25.


ltrim($str) => STR

Trim whitespaces (including newlines) at the beginning of string. Equivalent to:

 $str =~ s/\A\s+//s;

ltrim_lines($str) => STR

Trim whitespaces (not including newlines) at the beginning of each line of string. Equivalent to:

 $str =~ s/^\s+//mg;

rtrim($str) => STR

Trim whitespaces (including newlines) at the end of string. Equivalent to:

 $str =~ s/[ \t]+\z//s;

rtrim_lines($str) => STR

Trim whitespaces (not including newlines) at the end of each line of string. Equivalent to:

 $str =~ s/[ \t]+$//mg;

trim($str) => STR

ltrim + rtrim.

trim_lines($str) => STR

ltrim_lines + rtrim_lines.

trim_blank_lines($str) => STR

Trim blank lines at the beginning and the end. Won't trim blank lines in the middle. Blank lines include lines with only whitespaces in them.

ellipsis($str[, $maxlen, $ellipsis]) => STR

Return $str unmodified if $str's length is less than $maxlen (default 80). Otherwise cut $str to ($maxlen - length($ellipsis)) and append $ellipsis (default '...') at the end.

indent($indent, $str, \%opts) => STR

Indent every line in $str with $indent. Example:

 indent('  ', "one\ntwo\nthree") # "  one\n  two\n  three"

%opts is optional. Known options:

linenum($str, \%opts) => STR

Add line numbers. For example:


Known options:

pad($text, $width[, $which[, $padchar[, $truncate]]]) => STR

Return $text padded with $padchar to $width columns. $which is either "r" or "right" for padding on the right (the default if not specified), "l" or "left" for padding on the right, or "c" or "center" or "centre" for left+right padding to center the text.

$padchar is whitespace if not specified. It should be string having the width of 1 column.

double_quote($str) => STR

Quote or encode $str to the Perl double quote (") literal representation of the string. Example:

 say double_quote("a");        # => "a"     (with the quotes)
 say double_quote("a\n");      # => "a\n"
 say double_quote('"');        # => "\""
 say double_quote('$foo');     # => "\$foo"

This code is taken from quote() in Data::Dump. Maybe I didn't look more closely, but I couldn't a module that provides a function to do something like this. String::Escape, for example, provides qqbackslash but it does not escape $.

single_quote($str) => STR

Like double_quote but will produce a Perl single quote literal representation instead of the double quote ones. In single quotes, only literal backslash \ and single quote character ' are escaped, the rest are displayed as-is, so the result might span multiple lines or contain other non-printable characters.

 say single_quote("Mom's");    # => 'Mom\'s' (with the quotes)
 say single_quote("a\\");      # => 'a\\"
 say single_quote('"');        # => '"'
 say single_quote("\$foo");    # => '$foo'

common_prefix(@LIST) => STR

Given a list of strings, return common prefix.

common_suffix(@LIST) => STR

Given a list of strings, return common suffix.




Please visit the project's homepage at


Source repository is at


Please report any bugs or feature requests on the bugtracker website

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


Steven Haryanto <>


This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: