It's All About Context
Randal L. Schwartz
A recent article on Slashdot (http://www.slashdot.org) discussed the surprising story of a high-school student who made a small Perl programming mistake that got him into a big amount of trouble. On his dynamically generated Web page, he had used the code:
my($f) = `fortune';
when what he should have done was:
my $f = `fortune';
Now, both of these invoke the fortune program, capturing its random quip of text. In this particular case, when the school administrators visited the boy's page, fortune had selected a quote from a William Gibson novel:
I put the shotgun in an Adidas bag and padded it out with four pairs of tennis socks, not my style at all, but that was what I was aiming for: If they think you're crude, go technical; if they think you're technical, go crude. I'm a very technical boy. So I decided to get as crude as possible. These days, though, you have to be pretty technical before you can even aspire to crudeness.
--Johnny Mnemonic, by William Gibson
Now, if you can't tell what would be in $f for both of the code fragments above, read on, and you'll see how an unwitting mistake can leave someone with an unexpected police file.
The problem is a matter of context (in more ways than one). Perl's operators are "context sensitive", in that the operator can detect whether it is being used in a place looking for a scalar rather than looking for a list, and return an appropriate result. In this case, the backtick operator returns a differing result, depending on whether it was invoked in a scalar context or a list context.
To understand this, first look at how to detect context. Starting with the basics, the right-hand side of an assignment to a scalar variable must be a scalar value:
$a = ..
|