String::Truncate - a module for when strings are too long to be displayed in...
version 0.102
$Id: /my/cs/projects/String-Truncate/trunk/lib/String/Truncate.pm 32171 2007-07-23T21:45:08.267438Z rjbs $
This module handles the simple but common problem of long strings and finite terminal width. It can convert:
"this is your brain" -> "this is your ..." or "...is your brain" or "this is... brain" or "... is your b..."
It's simple:
use String::Truncate qw(elide); my $brain = "this is your brain"; elide($brain, 16); # first option elide($brain, 16, { truncate => 'left' }); # second option elide($brain, 16, { truncate => 'middle' }); # third option elide($brain, 16, { truncate => 'ends' }); # fourth option String::Trunc::trunc($brain, 16); # => "this is your bra"
elide($string, $length, \%arg)
This function returns the string, if it is less than or equal to $length characters long. If it is longer, it truncates the string and marks the elision.
$length
Valid arguments are:
elide - elide at left, right, middle, or ends? (default: right) marker - how to mark the elision (default: ...) at_space - if true, strings will be broken at whitespace if possible
trunc($string, $length, \%arg)
This acts just like elide, but assumes an empty marker, so it actually truncates the string normally.
elide
String::Truncate exports both elide and trunc, and also supports the Exporter-style ":all" tag.
trunc
use String::Truncate (); # export nothing use String::Truncate qw(elide); # export just elide() use String::Truncate qw(:all); # export both elide() and trunc() use String::Truncate qw(-all); # export both elide() and trunc()
When exporting, you may also supply default values:
use String::Truncate -all => defaults => { length => 10, marker => '--' }; # or use String::Truncate -all => { length => 10, marker => '--' };
These values affect only the imported version of the functions. You may pass arguments as usual to override them, and you may call the subroutine by its fully-qualified name to get the standard behavior.
The imported builds and installs lexical closures (code references) that merge in given values to the defaults. You can build your own closures without importing them into your namespace. To do this, use the elide_with_defaults and trunc_with_defaults routines.
elide_with_defaults
trunc_with_defaults
my $elider = String::Truncate::elide_with_defaults(\%arg);
This routine, never exported, builds a coderef which behaves like elide, but uses default values when needed. All the valud arguments to elide are valid here, as well as length.
length
This routine behaves exactly like elide_with_defaults, with one obvious exception: it retuns code that works like trunc rather than elide. If a marker argument is passed, it is ignored.
marker
Text::Truncate does a very similar thing. So does Text::Elide.
Ricardo SIGNES, <rjbs at cpan.org>
<rjbs at cpan.org>
Please report any bugs or feature requests through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Truncate. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Ian Langworth gave me some good advice about naming things. (Also some bad jokes. Nobody wants String::ETOOLONG, Ian.) Hans Dieter Pearcey suggested allowing defaults just in time for a long bus ride, and I was rescued from boredom by that suggestion
Copyright 2005-2007 Ricardo SIGNES.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::Truncate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::Truncate
CPAN shell
perl -MCPAN -e shell install String::Truncate
For more information on module installation, please visit the detailed CPAN module installation guide.