Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^2: Bareword Package Names

by martin (Friar)
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.

Replies are listed 'Best First'.
Re^3: Bareword Package Names
by jmlynesjr (Hermit) 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::."


    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986319]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-12-15 20:36 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (443 votes). Check out past polls.