But in most cases, $foo and "$foo" produce the same result, except the latter is slightly wasteful and slightly uglier. And in the fewer cases where they do differ, "$foo" produces the wrong result 99.9% of the time. So if it’s what you want then there’s nothing wrong with it, it’s just exceedingly rare that you do want it.
And because of that, I find that writing it as "$foo" doesn’t sufficiently emphasise that I specifically wanted that, so I write those cases as something like '' . $foo so that it will be absolutely clear that the code was written that way deliberately. So actually, I do in fact never write "$foo".
So that’s what is wrong with it after all: nothing from the computer’s perspective, but it communicates intent poorly to the next programmer.
This remains my favorite example: perl -e 'print reverse "dog"'
The funniest thing is that not even the perldoc reference manual has anything that would prevent one from assuming that it will work as expected. The default context of built-in functions is never specified anywhere!
reverse is documented here https://perldoc.perl.org/functions/reverse. The title is “reverse LIST”, which at least to me implies that the default context is list context.
The reverse of a one-element list is the same list.
perl -e 'print scalar reverse "dog"' prints god, as per the documentation.
A type error, or sensible duck typing. Or at least documentation that says that reverse() will treat scalars as one-item lists. Anything but what’s actually going on.
Context is key. reverse "dog" is equivalent to reverse ("dog"). One can argue that being able to refer to lists without enclosing parentheses is a wart.
But the documentation is clear that without the qualification using scalar the default context is list context.
I’m not going to say that Perl isn’t full of context gotchas, it is. But Perl’s documentation is generally clear.
What’s wrong with “$foo”?
The value of
$foo
will be interpolated within the double quotes.And if that’s what you want what’s wrong with it?
Nothing.
But in most cases,
$foo
and"$foo"
produce the same result, except the latter is slightly wasteful and slightly uglier. And in the fewer cases where they do differ,"$foo"
produces the wrong result 99.9% of the time. So if it’s what you want then there’s nothing wrong with it, it’s just exceedingly rare that you do want it.And because of that, I find that writing it as
"$foo"
doesn’t sufficiently emphasise that I specifically wanted that, so I write those cases as something like'' . $foo
so that it will be absolutely clear that the code was written that way deliberately. So actually, I do in fact never write"$foo"
.So that’s what is wrong with it after all: nothing from the computer’s perspective, but it communicates intent poorly to the next programmer.
If only it helped. ;)
This remains my favorite example:
perl -e 'print reverse "dog"'
The funniest thing is that not even the perldoc reference manual has anything that would prevent one from assuming that it will work as expected. The default context of built-in functions is never specified anywhere!
reverse
is documented here https://perldoc.perl.org/functions/reverse. The title is “reverse LIST”, which at least to me implies that the default context is list context.The reverse of a one-element list is the same list.
perl -e 'print scalar reverse "dog"'
printsgod
, as per the documentation.Edit structure
A type error, or sensible duck typing. Or at least documentation that says that
reverse()
will treat scalars as one-item lists. Anything but what’s actually going on.Context is key.
reverse "dog"
is equivalent toreverse ("dog")
. One can argue that being able to refer to lists without enclosing parentheses is a wart.But the documentation is clear that without the qualification using
scalar
the default context is list context.I’m not going to say that Perl isn’t full of context gotchas, it is. But Perl’s documentation is generally clear.
more on this can also be found via
perldoc -f reverse