On 21/07/2011 02:09, Shawn H Corey wrote:
On 11-07-20 07:03 PM, Uri Guttman wrote:
the other is a class method call. it has two major differences. first it
will pass its class (or object, the arg before ->) as the first arg in
the call. the second thing is that it will search the class hierarchy
(using the package global's @ISA) to find that method if it isn't in the
Lib class.

Not true in all cases:

#!/usr/bin/env perl

use strict;
use warnings;

package Foo;
sub foo {
print "foo\n";
}

package main;

Foo::foo();
Foo->foo();

Foo::bar();

__END__

The difference here is that `Foo:bar()` gives a run-time error; where
`Foo->bar()` gives a compile-time error.

Compile-time errors are always reported. Run-time errors are only
reported if the subroutine is called. If you have something like this,
the script would only stop for some rare condition:

if( come_rare_condition ){
Foo::bar();
}

Not something you want to discover after the code is in production. :)

I'm not sure what you mean here Shawn. Errors are generated at run time
in both cases, so code like

  if (0) {
    Foo::bar();
  }
  if (0) {
    Foo->bar();
  }

will generate no errors at all. In particular the method call cannot
generate a compile-time error as Perl supports dynamic binding in order
to implement object-orientation.

It would be possible to generate calls to undefined subroutines at
compile time, but because the symbol table can be modified at run time
with such trickery suck as *Foo::bar = \&Foo::foo it is also left until
run time to report any such errors.

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to