Anonymous Scalars
If you’ve done much Perl programming you’ve probably encountered the Anonymous Hash, Anonymous Array, and probably Anonymous Subroutines as well. Did you know there’s also an Anonymous Scalar? It’s one of those things that is not very useful most of the time, but is the perfect tool for some situations. Here are two situations where you can use them, however:
- Unmodifiable scalar.
- Interpolating complex expressions into a string.
If you’re doing some mathematics and want to have a variable $pi that cannot be modified, just put
our $PI; *PI = 3.14;
into your program. Then you can use $PI just like any variable, but Perl will die with a "Modification of a read-only value attempted" error if you try to modify the value of $PI. (This won’t work with "my" because it uses a typeglob ("*PI") to work its magic.)
Sometimes you want to put an expression that produces a scalar value into a double-quoted string. But you can only do that with variables - a scalar, array element, or hash element. You can’t put a subroutine call, mathematical formula, etc. into a string because Perl will just print it literally. For our example, we’ll say you want to print the month, day, and year that are returned by localtime().
But localtime() returns a month from 0 to 11, and the year minus 1900. For example the date "March 24, 2003" would be returned as month=2 and year=103. Did you ever seen a Web site that shows the year as 19100? That’s because they’re prepending "19" instead of adding 1900 to the year, one of the most common Y2K bugs in Perl programs written before the year 2000.
Of course you can do it in three lines, and this might be preferable for clarity. Here’s the three-line version:
my $display_month = $month + 1; my $display_year = $year + 1900; print "$display_month/$day/$display_yearn";
But if for some reason you need that to be a single statement, you can do this instead:
print "${($month + 1)}/$day/${($year + 1900)}n";
How does that work? Well, the above is actually equivalent to this:
my $display_month = ($month + 1); my $display_year = ($year + 1900); print "$$display_month/$day/$$display_yearn";
In other words, $display_month and $display_year are references to anonymous scalars containing the adjusted month and year, respectively. Inside the print statement, we dereference them (see the $$ at the front of each?) to extract the values they contain.
Why do we have to do this? Because only $, @, and \ have special meaning inside a double-quoted string. If you try to do it without the references (this will not work):
print "$month + 1/$day/$year + 1900n";
You will see something like this:
"2 + 1/24/103 + 1900"
Why? Perl has know way of knowing whether you want the + operator and the number that follows to be printed or executed, so it just prints it. To execute it you need to wrap it in the appropriate brackets as shown above.
But personally, I would just use this:
printf("%d/%d/%d", $month+1, $day, $year+1900).
There’s More Than One Way To Do It, after all.

your $PI will not be that “read only” at all, it can easily be changed by *PI = \”go away, constant freak!”; …
Comment by saga — January 26, 2006 @ 12:35 pm
That’s a good point. But at least you won’t be able to change $PI by saying $PI = 22/7. Perl’s never been about stopping people from doing things, but things like this make it a little harder to do the wrong thing.
Comment by William Ward — January 26, 2006 @ 3:18 pm