August 1999 Return to the Perl Hardware Store Slide #23

Interpolation.pm

        sub FETCH {
          $_[1];
        }

the the hash key is evaluated (by Perl) and returned unchanged (by us).

        sub FETCH {
          my $key = sprintf("%.2f", abs($_[1]));
          my $positive = $_[1] > 0;     
          1 while $key =~ s/^([-+]?\d+)(\d{3})/$1,$2/;     # FAQ
          $positive ? " \$$key " : "(\$$key)";
        }
        use Eval '$';
        $EARNINGS = 47387.12;
        $EXPENSES = 55337.48;
        print "This year we earned $${$EARNINGS} and expended $${EXPENSES}.\n"
        print "Net profits were $${$EARNINGS-$EXPENSES}.\n";

prints

        This year we earned  $47,387.12  and expended  $55,337.48.
        Net profits were ($7,950.36).


Next Copyright © 1999 M-J. Dominus