README
1
2 Interpolation.pm
3
4(Current version 0.73; last updated 13 June 2009)
5
6 Suppose your program needs to print out a lot of dollar amounts, and
7 you'd like to print each one out with a leading dollar sign, commas
8 every three places, and always two places for cents after a decimal
9 point, so that the number 1234.5 should be formatted as $1,234.50. One
10 way to do it is to define a commify routine to insert the punctuation
11 (which you can crib from the FAQ) and do something like this:
12
13 $com_SALARY = commify($SALARY);
14 print "Last year I made $com_SALARY.\n";
15
16 That can get old pretty quick---you end up with a lot of useless
17 variables like $com_SALARY cluttering up your program. Or you could
18 use printf:
19
20 printf "Last year I made %s.\n", commify($SALARY);
21
22 This is all right, but a little hard to read, because you have to
23 match up the formatting codes in the first argmuent with the values in
24 the rest of the arguments.
25
26 The bottom line here is that `commify' is just cosmetic, for output
27 formatting, not really an interesting or important part of the
28 program, and you'd really like to sweep it under the rug and make it
29 take up as little space as possible.
30
31 You can do that with Interpolation. Here's what that example looks
32 like if you use Interpolation:
33
34 print "Last year I made $money{$SALARY}.\n";
35
36 One line, no extra variables, and no sign of the formatting except the
37 descriptive word money there to say what the format is. If you're
38 going to be doing a lot of money, and the word money is too long, you
39 can use M instead:
40
41 print "Last year I made $M{$SALARY}.\n";
42
43 Or you can use whatever name you prefer, even _. If you're going to be
44 printing out a lot of percentages to two decimal places, you might
45 name the interpolator %, so that you could write this:
46
47 print "Sales have increased by $%{$increase}.\n";
48
49 And, since $% is an abbreviation for `format in a way appropriate for
50 percentages', what it would print would look like:
51
52 print "Sales have increased by 3.12%.\n";
53
54 You can have as many different formats as you want, and you can give
55 them whatever names you want. You can install a formatter in one part
56 of your program, and uninstall it again when you're done with it.
57
58 Here's another example: You do a database call and get back the name
59 of a U.S. state of Canadian province. The database doesn't capitalize
60 these consistently; sometimes they're correct, sometimes all
61 uppercase, sometimes all lowercase. You need to capitalize correctly
62 when you rpint out the results.
63
64 Rather than explicitly calling a capitalization function each and
65 every time you get data from the database, you can use an
66 interpolator, like this:
67
68 print "Database returned: $Cap{$RESULT}.\n";
69
70 $C is an interpolator that fixes the capitalization of anything it
71 gets, in this case the contents of $RESULT.
72
73 The argument to an interpolator can be any Perl expression. In the
74 context of the money example, that means you can do something like
75 this:
76
77 print "If I get a 6% raise, I'll be making $money{$SALARY * 1.06}.\n";
78
79 And again you save on space and on useless temporary variables. It
80 seems like this is prone to abuse, but in many cases, like this one,
81 it does seem to make the code clearer, putting emphasis on the
82 important parts and preventing a lot of excess verbiage that would
83 obscure what was really going on.
84
85 As a special case, you can evaluate arbitrary expressions into
86 strings, like this: "1 + 2 = $eval{1 + 2}", which turns into _1 + 2 =
87 3_.
88
89See also:
90
91 Interpolation.pm page:
92 http://www.plover.com/~mjd/perl/Interpolation/
93 M-J. Dominus Perl Paraphernalia:
94 http://www.plover.com/~mjd/perl/
95 Jenda's page
96 http://Jenda.Krynicky.cz
97
98