Single quotes in a variable name in Perl?
Asked Answered
E

1

5

I was writing some Perl code in vim and accidentally typed a single quote character in a variable name and noticed that it highlighted it in a different color than normal single quoted strings.

enter image description here

I thought that was odd, so I wrote a small test program (shown above) and tried to run it to see how Perl would handle it and I got this error:

"my" variable $var::with::apostrophes can't be in a package

What exactly is going on here? Are there situations where single quotes in variable names are actually valid? If so, what meaning do single quotes have when used in this context?

Evadnee answered 6/4, 2021 at 16:1 Comment(4)
That's how Damian writes things like Acme::Don't :-)Giffard
Why is this a thing...? I especially love the section on Bugs: "Unlikely, since it doesn't actually do anything. However, bug reports and other feedback are most welcome." LOLEvadnee
In case it's not clear, anything in the Acme namespace should not be taken seriously.Giffard
Although I actually kind of wish this worked in combination with unless. It would be pretty cool to be able to write don't die unless $error; hahaEvadnee
C
7

The single quote is the namespace separator used in Perl 4, replaced by the double colon :: in Perl 5. Because Perl is mostly backwards compatible, this still works. It's great for golfing, but not much else.

Here's an article about it on perl.com that doesn't explain it.

Contraption answered 6/4, 2021 at 16:6 Comment(3)
I can't find any documentation for it online. Maybe I don't have good Google Fu today. I'm sure I've read this in a book at some point.Contraption
See perldoc.perl.org/perlvar#The-Syntax-of-Variable-NamesDextran
That's a good find @clamp, but it also doesn't explain why this exists.Contraption

© 2022 - 2024 — McMap. All rights reserved.