tags:

views:

104

answers:

1

I can have a constructor like this :

sub create {
  my $class = shift;
  my $self  = {};
  return bless $self,$class;
}

and when I create an object, I can write this:

my $object = create Object;

Is this:

my $object = Object::create("Object");

the only equivalent to that constructor call?

+5  A: 

No, the equivalent call is

my $object = Object->create();

If you use the fully qualified name of the create function without the arrow syntax, you aren't going through Perl's OO method dispatch, and therefore any inherited methods will not work.

The arrow syntax is preferred over the "indirect" create Object syntax. For the reasons why, see this question.

friedo
could you tell me all the equivalent calls? I don't care about the side effects of each, just the syntax.
Geo
The indirect `new Object` and arrow syntax `Object->new` are equivalent, except for the syntax ambiguity problems I mentioned with the indirect form. The only other way to call a function in another package is with its fully-qualified name, e.g. `Object::new()` which behaves exactly the same as calling `new()` in the current package.
friedo