Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Bareword Package Names

by martin (Pilgrim)
on Aug 08, 2012 at 16:16 UTC ( #986319=note: print w/ replies, xml ) Need Help??


in reply to Re: Bareword Package Names
in thread Bareword Package Names

What tobyink demonstrated is that a bareword will act as a procedure call if (and only if) a subroutine of that name is known. Perl's auto-quoting of barewords will only kick in in the absence of such a subroutine. Any kind of explicit quoting, of course, will also avoid procedure calls.

my $object3 = 'Foo::Bar'->new; say ref $object3; # says "Foo::Bar"

So we now have established by different examples that trailing double-colons can act like quoting.

There may be caveats though. In some places where perl looks for barewords, the package-quoted bareword does not seem to lose its bareword-likeness.

use strict; use warnings; package Foo::Bar; package main; print Foo::Bar::; # warns: # Name "Foo::Bar" used only once: possible typo # print() on unopened filehandle Bar print Foo::Bar::, "Baz\n"; # prints Foo::BarBaz

Perl has to distinguish whether print with a single argument is called as print HANDLE or print LIST, and apparently uses a rather simple heuristic for this.

I try to avoid ambiguity with print and always use the print HANDLE LIST syntax for printing to handles.


Comment on Re^2: Bareword Package Names
Select or Download Code
Re^3: Bareword Package Names
by jmlynesjr (Pilgrim) on Aug 10, 2012 at 03:03 UTC

    See Programming Perl 4th Edition page 389 - Symbol Tables.

    "Symbol tables are stored in a hash whose name is the same as the package, but with two colons appended. The main symbol table's name is thus %main::."

    "Likewise, the symbol table or the Red::Blue package is named %Red::Blue::."

    James

    Also see page 423 - Package-Quoted Classes.

    "The final syntactic ambiguity with the indirect object style of method invocation is that it may not be parsed as a method call at all, because the current package may have a subroutine of the same name as the method...there is a way to resolve this ambiguity while still keeping the indirect object syntax: package-quote the class name by appending a double colon to it."

       $obj = method CLASS::;   # forced to be "CLASS"->method

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986319]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-09-30 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (373 votes), past polls