Something New Every Day

Just now, while attempting to do

print "$user's crontab is missing!\n"

I got the error “Use of uninitialized value in concatenation (.) or string…”, and after a bit of testing, I discovered that “$package'varname” is apparently an alias of “$package::varname” (in my case, Perl was trying—and failing—to print $user::s).

Did everyone else know this? This is literally the first time I have run across this in almost 20 years of Perl programming

(Of course a quick Google search turns this up in the opening paragraphs of the perlmod docs—I wonder if it’s time to read all of that stuff cover-to-cover?)

9 Comments

I only know it because the books I read for Perl 5.6 mentioned the apostrophe as an archaic and now-outdated package separator and admonished me to use double-colon. So, I suspect it is not something everyone knows, but nowadays I expect it's something most people learn about through doing what you did.

I think you meant to write:

I discovered that “$package'varname” is apparently an alias of “$package::varname”

It's a little-known corner of Perl history. But it can be used for CPAN jokes.

Sometimes it seems like everybody knows, other times I’m surprised how many don’t.

Note that in Perl 6, apostropes are valid parts of identifiers (with some restrictions
on their placement), so the issue that bit OP in Perl 6 complains about
undeclared variable `$user's` and uses Levenshtein distance to figure out that user might have meant the `$user` variable and suggests that.

There's something to be said for braces whenever a variable bumps up against anything that !~ m/\s/:

print "${user}'s crontab is missing!\n"

The JAPH in my .sig is based on this feature:

for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

Leave a comment

About morandimus

user-pic My real name is Jeremy Holland. I've been a programmer for 25 years, using primarily Perl since 2000.