For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > :: and ->









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author :: and ->
Ken Perl

2006-02-17, 3:55 am

what is the difference of :: and -> in this statements?

$authInstance =3D Operation::Auth::getInstance($session,$u
->authMethod,$u->=
userId)

$authInstance =3D Operation::Auth->getInstance($session,$u->authMethod,$u->=
userId)

--
perl -e 'print unpack(u,"62V5N\"FME;G\!E<FQ`9VUA:6PN8V]M\"\@``
")'
Hans Meier

2006-02-17, 7:55 am

Ken Perl am Freitag, 17. Februar 2006 02.34:
> what is the difference of :: and -> in this statements?
>
> $authInstance =
> Operation::Auth::getInstance($session,$u
->authMethod,$u->userId)
>
> $authInstance =
> Operation::Auth->getInstance($session,$u->authMethod,$u->userId)


The first '::' is part of a package name, the package is

package Operation::Auth

The last '::' (in the first example) denotes a sub in this package and is a
procedure invocation, the sub getting only the passed arguments.

The '->' is a method invocation, and the class name ('Operation::Auth' in this
case) is implicitly passed to getInstance as first argument (as Chas already
said).

The two notations are not interchangeable if the sub definition is for example

sub getInstance {
my ($class, $session, $meth, $uid)=@_;
...
}

intended to be called as method.

The first invocation would leed to an error since the first argument
getInstance receives is $session, and not a class name.


hth,
joe
Ken Perl

2006-02-18, 3:56 am

This is really very clear.

On 2/17/06, Hans Meier (John Doe) <security.department@tele2.ch> wrote:
> Ken Perl am Freitag, 17. Februar 2006 02.34:
>
> The first '::' is part of a package name, the package is
>
> package Operation::Auth
>
> The last '::' (in the first example) denotes a sub in this package and is=

a
> procedure invocation, the sub getting only the passed arguments.
>
> The '->' is a method invocation, and the class name ('Operation::Auth' in=

this
> case) is implicitly passed to getInstance as first argument (as Chas alre=

ady
> said).
>
> The two notations are not interchangeable if the sub definition is for ex=

ample
>
> sub getInstance {
> my ($class, $session, $meth, $uid)=3D@_;
> ...
> }
>
> intended to be called as method.
>
> The first invocation would leed to an error since the first argument
> getInstance receives is $session, and not a class name.
>
>
> hth,
> joe
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
perl -e 'print unpack(u,"62V5N\"FME;G\!E<FQ`9VUA:6PN8V]M\"\@``
")'
Bob Showalter

2006-02-18, 6:56 pm

Ken Perl wrote:
> what is the difference of :: and -> in this statements?
>
> $authInstance = Operation::Auth::getInstance($session,$u
->authMethod,$u->userId)


This is a subroutine call, passing 3 arguments.

>
> $authInstance = Operation::Auth->getInstance($session,$u->authMethod,$u->userId)


This is called a class method invocation. Perl will look for a
getInstance sub in package Operation::Auth and its ancestors (via @ISA),
and finally in package UNIVERSAL. If no sub is found, it will look for
an AUTOLOAD sub using the same procedure and invoke it. Once a sub is
found, it will be called with the string 'Operation::Auth' as the first
argument, followed by the three remaining arguments.

--
Well I can't stop here all day...I'm on a cycling tour of North Cornwall!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com