Lingua::Abbreviate::Hierarchy - Shorten verbose namespaces
This document describes Lingua::Abbreviate::Hierarchy version 0.04
use Lingua::Abbreviate::Hierarchy; my $abr = Lingua::Abbreviate::Hierarchy->new( keep => 1 ); $abr->add_namespace(qw( comp.lang.perl.misc comp.lang.perl.advocacy )); # gets 'c.l.p.misc' my $clpm = $abr->ab('comp.lang.perl.misc'); # abbreviate an array my @ab = $abr->ab(qw( comp.lang.perl.misc comp.lang.perl.advocacy ));
It's a common practice to abbreviate the elements of namespaces like this:
comp.lang.perl.misc -> c.l.p.misc comp.lang.perl.advocacy -> c.l.p.advocacy
This module performs such abbreviation. It guarantees that generated abbreviations are long enough to be unique within the current namespace.
To abbreviate names within a namespace use the module:
use Lingua::Ab::H; # use abbreviated name
Create a new abbreviator:
my $abr = Lingua::Ab::H->new( keep => 1 );
Set up the namespace:
$abr->add_namespace(qw( comp.lang.perl.misc comp.lang.perl.advocacy ));
Get your abbreviations:
# gets 'c.l.p.misc' my $clpm = $abr->ab('comp.lang.perl.misc'); # abbreviate an array my @ab = $abr->ab(qw( comp.lang.perl.misc comp.lang.perl.advocacy ));
Often the namespace will be larger; for example if you wanted to generate abbreviations that would be unique within the entire comp.lang.* hierarchy you would add all the terms in that space to the abbreviator.
Create a new abbreviator. Options may be passed as key, value pairs:
my $abr = Lingua::Ab::H->new( keep => 1, sep => '::' );
The following options are recognised:
The string that separates components in the namespace. For example '.' for domain names or '::' for Perl package names;
Abbreviate only the initial N elements in the name.
Leave N elements at the end of the name unabbreviated.
Abbreviate from the left until the generated abbreviation contains N or fewer characters. If
only is specified then at least that many elements will be abbreviated. If
keep is specified that many trailing elements will be unabbreviated.
May return more than N characters if the fully abbreviated name is still too long.
A truncation string (which may be empty). When
trunc is supplied the generated abbreviation will always be <=
max characters and will be prefixed by the truncation string.
Normally we consider the namespace to be rooted at the left (like a filename or package name). Set
flip to true to process right-rooted namespaces (like domain names).
ns =>array ref
Supply a reference to an array containing namespace terms. See
add_namespace for more details.
Add terms to the abbreviator's namespace:
$abr->add_namespace( 'foo.com', 'bar.com' );
When abbreviating a term only those elements of the term that fall within the namespace will be abbreviated. Elements outside the namespace will be untouched.
Abbreviate one or more terms:
my $short = $abr->ab( 'this.is.a.long.name' );
Or with an array:
my @short = $abr->ab( @long );
Expand an abbreviation created by calling
ab. When applied to abbreviations created in the current namespace
ex will reliably expand arbitrary abbreviated terms. It will also pass through non-abbreviated terms unmolested.
If the namespace for expansion is not identical to the namespace for abbreviation then the results are unpredictable.
my @ab = $abr->ab( @terms ); # Abbreviate terms... my @ex = $abr->ex( @ab ); # ...and get them back
Please report any bugs or feature requests to
email@example.com, or through the web interface at http://rt.cpan.org.
Copyright (c) 2009, Andy Armstrong
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.