NAME

Hash::Abbrev - Text::Abbrev with aliases

VERSION

version 0.01

SYNOPSIS

this module creates an abbreviation hash where each abbreviation of the key is read/write aliased to the same value.

    use Hash::Abbrev;

    my $hash = abbrev qw(file directory count);

    say $$hash{f};  # 'file'
    say $$hash{dir} # 'directory'

    $_ .= '!' for @$hash{qw/f d c/};

    say $$hash{file}; # 'file!'
    say $$hash{co};   # 'count!'

or as a dispatch table:

    @$hash{qw/file dir count/} = (\&load_file, \&read_dir, \&get_count);

    $$hash{f}(...)          # calls load_file(...)
    $$hash{directory}(...)  # calls read_dir(...)

EXPORT

this module exports the abbrev function by default.

SUBROUTINES

abbrev LIST

takes a list of strings and returns a hash reference where all of the non-ambiguous abbreviations are aliased together. the returned reference is to an ordinary hash, it is not tied or magic in any way.

the behavior could be written out this way if the := operator meant 'alias the lhs to the rhs':

    abbrev 'abc', 'xyz'  ~~  $h{abc} = 'abc'
                             $h{ab} := $h{abc}
                             $h{a}  := $h{abc}
                             $h{xyz} = 'xyz'
                             $h{xy} := $h{xyz}
                             $h{x}  := $h{xyz}

abbrev HASHREF LIST

the first argument to abbrev can be a hash reference. that hash will be modified in place with the existing keys and values and then will be returned. an additional list of keys to abbreviate can be provided after the hash reference.

    my $hash = abbrev {
        file      => sub {"file(@_)"},
        directory => sub {"directory(@_)"},
    };

    say $$hash{f}('abc.txt');  # 'file(abc.txt)'
    say $$hash{dir}('/');      # 'directory(/)'

since the modification is done in place, the following also works:

    my %hash = (
        file      => sub {"file(@_)"},
        directory => sub {"directory(@_)"},
    );

    abbrev \%hash;

    say $hash{f}('abc.txt');  # 'file(abc.txt)'
    say $hash{dir}('/');      # 'directory(/)'

AUTHOR

Eric Strom, <asg at cpan.org>

BUGS

please report any bugs or feature requests to bug-hash-abbrev at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-Abbrev. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Text::Abbrev for the abbreviation table.
Hash::Util for hv_store.

LICENSE AND COPYRIGHT

copyright 2011 Eric Strom.

this program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

see http://dev.perl.org/licenses/ for more information.