martin has asked for the wisdom of the Perl Monks concerning the following question:
This is a question about a perl5 feature I could find no documentation about so far. I would be glad to know whether it is intended this way and safe to use or even recommended.
A bareword ending in a double colon, such as Foo::Bar::, evaluates to a string without the double colon suffix, "Foo::Bar" in this example. Using such a bareword is not prohibited under strict 'subs', but it triggers a warning if perl hasn't seen a package of that name yet.
Example:
use strict; use warnings; package Foo::Bar; package main; my $p = Foo::Bar::; print "$p\n"; # prints Foo::Bar my $q = Baz::Qux::; # warns: # Bareword "Baz::Qux::" refers to nonexistent package print "$q\n"; # prints Baz::Qux
I'll call this feature package-name-quoting, as appending two colons has the effect of quoting plus package name validation. I could use it where package name strings are needed, such as in arguments for isa or DOES. The warning would help me to catch typos.
So, after all, it is a useful feature. Is it documented? I would have expected perlmod to say something about it. Maybe I looked in the wrong place.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bareword Package Names
by tobyink (Canon) on Aug 08, 2012 at 14:08 UTC | |
by martin (Friar) on Aug 08, 2012 at 16:16 UTC | |
by jmlynesjr (Deacon) on Aug 10, 2012 at 03:03 UTC | |
Re: Bareword Package Names
by tobyink (Canon) on Nov 27, 2012 at 12:53 UTC |