Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

::'s role in strings

by eff_i_g (Curate)
on Apr 27, 2006 at 17:11 UTC ( [id://546067]=perlquestion: print w/replies, xml ) Need Help??

eff_i_g has asked for the wisdom of the Perl Monks concerning the following question:

Syntactical Saints,

The following statement caught my eye during a recent skimming of the Perl Pocket Reference, Fourth Edition on Page 9 under "Literal Values" => "Scalar Values" => "String":
    Class::
        A value that is mostly equivalent to "Class".
I don't understand what this is trying to convey.

I know :: is used as a directory separator for Modules, and to specify Package names. Am I missing something else?

Thanks.

P.S. Is :: referred to as "the double colon" within the Perl coummunity, or does it have a neater name?

Update: Here is the surrounding material to provide more context (i.e., as much clarification as I can):
Literal Values Scalar Values ... ... String 'abc' Literal string, no variable interpolation...except... "abc" A string in which variables are interpolated... `command` Evaluates to the output of the command. Class:: A value that is mostly equivalent to "Class". 1.2.3 v.5.6.0.1 A string ("v-string") composed of the specified ordinals. +... <<identifier Shell-style "here document." __FILE__ The name of the program file. __PACKAGE__ The name of the current package.

Replies are listed 'Best First'.
Re: ::'s role in strings
by mantadin (Beadle) on Apr 27, 2006 at 17:34 UTC
    I do not know, what this book is trying to say, either. Yet I think, :: does have more meanings in addition to those you mentioned, at least in OO-context. AFAIK,
    Animal->foo()
    is the same like
    Animal::foo("Animal")
    Now, if Dog is an Animal, and has no own implementation of the foo()-method,
    Dog->foo()
    is the same as Dog::foo("Animal") update Animal::foo("Dog") (thanks to ikegami)

    Maybe you can clarify your question a little bit further.

    As to your p.s., I know :: as the 'scope resolution operator', but I don't know whether this name is really very common in the Perl community.

      You got it backwards
      Dog::foo("Animal")
      should be
      Animal::foo("Dog")

      Your first two examples are not necessarily equivalent. Function invocation uses a very different lookup mechanism from method dispatch.

      The postfix double colons exist to disambiguate classnames from barewords in the parser.

Re: ::'s role in strings
by ikegami (Patriarch) on Apr 27, 2006 at 18:47 UTC

    It means you can do the following:

    >perl -e "$a = Class::; print $a" Class

    It even works under strict, and it's doesn't issue any warnings if the package exists.

      Here is what I tried on my machine:
      > perl -v This is perl, v5.8.6 built for PA-RISC2.0 Copyright 1987-2004, Larry Wall ... > perl -e "$a = Class::; print $a" a: Undefined variable.
      I guess my follow up question is: Why does/should this work?

        I used 5.6.1.

        a: Undefined variable.

        I'm susprised by the output you get. Is that really what you got for output? from perl? No, it's from your shell. Use single quotes.

        Why does/should this work?

        It's probably just an unintentional side-effect. It's to avoid ambiguitities when using indirect method calls. I wouldn't use :: as a quoting mechanism.

        Updated: Thanks to japhy and jhourcle for the corrections/elaborations.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://546067]
Approved by Mutant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found