Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
While we're at it, may I point you towards the module Interpolation too? MJD (who originally wrote and released it, it is now maintained by another person) originally thought of it as nothing but a joke, but it's actually quite a handy way to insert often used function calls of one parameter into doublequotish strings.

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:

# "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";
which produces:
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:

# 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";
It's lots less obscured how it does what it does, so scoping issues etc. will be much more on familiar terrain.

In reply to (Re: I don't use printf enough) Interpolation.pm by bart
in thread I don't use printf enough by dreadpiratepeter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (8)
    As of 2014-07-31 21:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (253 votes), past polls