Beginners always want to write this:
print "The sum of three and four is: 3+4";
And they want the 3+4 part to be evaluated, so that it prints this:
The sum of three and four is: 7
Of course, it's a double-quoted string, so it's not evaluated. The only things that are evaluated in double-quoted strings are variable references. Maybe the thing that confuses beginners is that there are so darn many ways to write variable references in Perl:
print "$a and @a and $a[3] and $a{Ouch} and ${$a{Radikal}[1]}->[2]";
You sure can get a lot of stuff in there! And that leads people to use the following rather ugly and obfuscated construction when they want to interpolate the results of a function call into a string:
print "The return value is: @{[&function(arg)]}";
Wow, like, totally E2MUCHPUNCTUATION, dudes.
The @ tells Perl to interpolate an array; the {...} tells Perl to expect a reference instead of an array name; the [...] symbols construct an anonymous array and yield a reference to it, and inside a @{...} construction in a double-quoted string, Perl does evaluate expressions.There are three problems with this trick. It's ugly. It's obfuscated. And under some circumstances, it's wrong. The first two problems are obvious. Real obvious. The third is subtler: The function is evaluated in list context, and sometimes that's what you want, and sometimes it isn't. An example of when it isn't: You'd like to print the date, which is conveniently returned by the localtime function when it's invoked in scalar context:
$a = localtime; print "It is now $a.\n"; It is now Sun Mar 8 01:19:19 1998.
But if you try using the ugly construction:
print "It is now @{[localtime]}.\n"; It is now 19 19 1 8 2 98 0 66 0.
See, we really did need scalar context and not array context there.
There are solutions to this, but they make the already ugly and obfuscated construction even uglier and more obfuscated. Here are some examples:
print "It is now ${\do{my $a = localtime}}.\n"; print "It is now ${my $a = localtime; \$a}.\n"; print "It is now @{[scalar localtime]}.\n";
But I have a solution. It's clean. It's simple. It's attractive. Unfortunately, it's also somewhat ridiculous.
If you're amused or horrified (and I think you should be,) drop me a note and let me know. I'm really pleased with this. I'm also trying to decide what to call it before I upload it to CPAN, so if you have any suggestions, please suggest them. Yes, you heard right: I'm really planning to inflict this on the rest of the world.
Don't read this until you've looked at the sample programs, or might might not make sense, and it might contain spoilers.
But you don't want to go printing out
I see you live in ALBERTAor whatever capitalization ugliness it might contain. You also can't use
"I see you live in \u\L$STATE\E"because that gives the wrong result for New Brunswick: You get
I see you live in New brunswickand the b is lowercase when it should be uppercase.
The solution is to use this module; now it's easy to write
"I see you live in $ucwords{$STATE}" I see you live in New Brunswick
$SALARY = 35000; print "Your salary is $money{$SALARY}";could print
Your salary is $35,000.00
Return to: Universe of Discourse main page | What's new page | Perl Paraphernalia