What is the exact difference between ::
and ->
in Perl?
->
sometimes works where ::
does not.
When the right hand side is a function ->
passes its left hand side as the first argument to the function. So the following examples are equivalent if $foo
is an object blessed to package Foo and Bar is in package Foo. ->
will resolve inherited methods making it cleaner and more useful for objects.
$foo->Bar();
Foo::Bar($foo);
->
can also take a package name
Foo->Bar();
Foo::Bar('Foo');
This means that ->
is generally used in instance methods so that the object is passed its self and constructors so the constructors know which package to bless with. This is usually a parameter so it can be inherited.