Reputation: 1018
I just encountered my first odd "quirk" of Perl in a long time. I can't seem to find any documentation of it online and I don't know what Perl is doing.
Let's just cut to the chase:
my $name = "Steve";
my $v = "$name's name is $name";
The value of $v would turn out to be " name is Steve". I use vim and the first $name turns an odd color (green instead of the normal teal). So I know this is a known feature, but I don't understand what is happening.
For those who might want to suggest alternatives, I know I can do either of the following without any problems:
$v = $name . "'s name is $name";
$v = "${name}'s name is $name";
I just am curious as to what Perl is doing in the first case I gave and what its uses are.
Upvotes: 4
Views: 352
Reputation: 1018
Quoting perlmod,
The old package delimiter was a single quote, but double colon is now the preferred delimiter, in part because it's more readable to humans, and in part because it's more readable to emacs macros. It also makes C++ programmers feel like they know what's going on--as opposed to using the single quote as separator, which was there to make Ada programmers feel like they knew what was going on. Because the old-fashioned syntax is still supported for backwards compatibility, if you try to use a string like
"This is $owner's house"
, you'll be accessing$owner::s
; that is, the $s variable in packageowner
, which is probably not what you meant. Use braces to disambiguate, as in"This is ${owner}'s house"
.
You could also use "This is $owner\'s house"
.
Upvotes: 6