Obscure Perl trick: single-quote separators
One of the delights of working in an old language with penchant for backwards-compatibility is discovering some of the artifacts that remain. A couple of weeks ago I was reading perlmod and came across this:
The old package delimiter was a single quote … which was there to make Ada programmers feel like they knew what was going on … the old-fashioned syntax is still supported for backwards compatibility
How interesting! Ada uses a single quote as an attribute delimiter, similar to the possessive in English:
Customer'name
In Perl, I can replace the use of the two colon separator with a single quote. So this simple package declaration and script:
package My::Customer;
sub name { 'Dobby the Sheep' }
package main;
print My::Customer::name();
Becomes:
package My'Customer;
sub name { 'Dobby the Sheep' }
package main;
print My'Customer'name();
You can see that the single quote can replace both namespace separators in the package name, and attribute accessors in the call to name()
. Running this code prints “Dobby the Sheep” as expected, but the syntax highlighting is pretty messed up in my editor.
References
Tags
David Farrell
David is the founder and editor of PerlTricks.com. An organizer of the New York Perl Meetup, he works for ZipRecruiter as a software developer.