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?

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • That's how Damian writes things like [Acme::Don't](https://metacpan.org/pod/Acme::Don::t) :-) – Dave Cross Apr 06 '21 at 16:28
  • 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." LOL – tjwrona1992 Apr 06 '21 at 19:00
  • 1
    In case it's not clear, anything in the Acme namespace should not be taken seriously. – Dave Cross Apr 07 '21 at 10:35
  • 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;` haha – tjwrona1992 Apr 07 '21 at 21:43

1 Answers1

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.

simbabque
  • 53,749
  • 8
  • 73
  • 136