Author image David Muir Sharnoff
and 1 contributors


 Tie::Function::Examples - tie functions to the the read side of hashes


 use Tie::Function::Examples;
 use Tie::Function::Examples qw(%thoucomma %nothoucomma %addcents %q_shell %round %sprintf);

 tie %array, 'Tie::Function::Examples', \&function;


        use Tie::Function::Examples;

        tie %double, 'Tie::Function::Examples', 
                sub {
                        my ($key) = @_;
                        return $key * 2 if $key != 0;
                        return $key.$key;

        print "2 * 2 is $double{2}\n";

        use Tie::Function::Examples qw(%thoucomma %addcents);

        tie %mymoney, 'Tie::Function::Examples',
                sub {
                        my ($key, $underlying_array) = @_;
                        return "\$$thoucomma{$addcents{$underlying_array->{$key}}";

        $mymoney{joe} = 7000;
        print "$mymoney{joe}\n" # prints $7,000.00


Tie::Function::Examples provides a simple method to tie a function to a hash.

The function is passed two arguments: the key used to access the array and a reference to a hash that is used for all non-read accesses to the array.


The following hashes are bound and can be imported from Tie::Function::Examples.


Adds commas to numbers. "7000.32" becomes "7,000.32"


Removes commas from numbers. "7,000.32" becomes "7000.32"


Make sure that numbers end two places to the right of the decimal. "7000" becomes "7000.00" and "7000.149" becomes "7000.15".


Quote strings for use in perl eval.


Quotes file names quoted for use on a command line with the bash shell. This will sometimes put 'single quotes' around the file name and other times it will leave it bare.


This will round a number to the nearest integer. If you want a different rounding-point, use a pseudo-two dimensional lookup to provide a scale. Use "0.01" to round to the nearest penny and "1000" to round to the nearest thousand. For example: $round{38.7, 10} will round up to 40.


Use a comma to do a pseudo-multi-dimensional lookup to specify both a format and arguments. Obviously, none of the arguments can have the ASCII character that is equal to the perl $; variable. Example: $sprintf{"%07d", 82} will interpolate to "0000082".


Add line numbers to a block of text.


Copyright (C) 2008-2007,2008-2010 David Sharnoff. Copyright (C) 2007-2008 SearchMe Inc. Copyright (C) 2011 Google Inc. This package may be used and redistributed under the terms of either the Artistic 2.0 or LGPL 2.1 license.