in reply to I don't use printf enough
The way it does it, is by tieing (most commonly) a hash to a function, making use of perl's remarkable feature that it always treats the index as code, and executes it before looking up the associated hash value. This function can simply pass its parameter along, allowing you simply insert a calculated scalar.
I see basically two basic ways to use it. The first route, is using the official import interface, like this:
which produces:# "Official" import mechanism use Interpolation commify => sub { local $_ = scalar reverse shift; s/(\d+\.)|(\d\d\d)(?=\d)/$1 || "$2,"/ge; return scalar reverse $_; }; $a = 12345.21; $b = 1357.98; print "$a*$b with commas inserted looks like '$commify{$a*$b}'.\n";
12345.21*1357.98 with commas inserted looks like '16,764,548.2758'.Oh, yes. That does indeed perform a multiplication.
Do you see the relationship between the parameter for use, the string "commify", and the global hash %commify? You do?!? Well I don't, that' why I like the tie approach better, as you can tie any hash, a global, a variable from another package, or a lexical. Incidently, this snippet does basically the same thing as the previous one:
It's lots less obscured how it does what it does, so scoping issues etc. will be much more on familiar terrain.# Non-obfuscated form, using tie(): use Interpolation; my %commify; tie %commify, Interpolation => sub { local $_ = scalar reverse shift; s/(\d+\.)|(\d\d\d)(?=\d)/$1 || "$2,"/ge; return scalar reverse $_; }; $a = 12345.21; $b = 1357.98; print "$a*$b with commas inserted looks like '$commify{$a*$b}'.\n";