A
Anno Siegel
It is possible to use the name of a class also as the name of a subroutine,
which may create objects of that class. To fix the ideas, assume a class
package Race::Horse;
sub new {
my $class = shift;
bless {
name => shift,
# ...
}, $class;
}
# more methods...
It is now possible to define
sub Race::Horse {
Race::Horse->new( @_);
}
Instead of the always slightly cumbersome
my $horse = Race::Horse->new( 'Stewball');
we can now say
my $horse = Race::Horse( 'Stewball');
I find this creator intuitive, and I'm thinking of equipping two or
three CPAN modules of mine that way.
Of course, there are problems. Aren't there always?
First off, it's thin ice that it works at all (if you know what I mean).
In sub Race::Hose, "Race::Horse->new..." is ambiguous syntax. What is
left of the arrow can be any expression, so Perl would be in its rights
to read it as "Race::Horse()->new..." and plunge into deep recursion.
It Does What I Mean, but I don't think the feature is documented.
I don't think it is likely to go away. That would break old code.
Another problem is name space violation. Race::Horse defines a sub
in package Race, where it has no business defining things. Of course,
there is no rule that explicitly forbids it, but it might be considered
poor (even rude) style. Actual collisions are improbable, however.
If package Race contains subs, their names are likely to be lower case.
A favorable point is that the additional creator doesn't interfere with
the traditional ->new (or any other creator that might be used). One
can replace the other anywhere, and no-one is forced to use the new
style.
Anno
which may create objects of that class. To fix the ideas, assume a class
package Race::Horse;
sub new {
my $class = shift;
bless {
name => shift,
# ...
}, $class;
}
# more methods...
It is now possible to define
sub Race::Horse {
Race::Horse->new( @_);
}
Instead of the always slightly cumbersome
my $horse = Race::Horse->new( 'Stewball');
we can now say
my $horse = Race::Horse( 'Stewball');
I find this creator intuitive, and I'm thinking of equipping two or
three CPAN modules of mine that way.
Of course, there are problems. Aren't there always?
First off, it's thin ice that it works at all (if you know what I mean).
In sub Race::Hose, "Race::Horse->new..." is ambiguous syntax. What is
left of the arrow can be any expression, so Perl would be in its rights
to read it as "Race::Horse()->new..." and plunge into deep recursion.
It Does What I Mean, but I don't think the feature is documented.
I don't think it is likely to go away. That would break old code.
Another problem is name space violation. Race::Horse defines a sub
in package Race, where it has no business defining things. Of course,
there is no rule that explicitly forbids it, but it might be considered
poor (even rude) style. Actual collisions are improbable, however.
If package Race contains subs, their names are likely to be lower case.
A favorable point is that the additional creator doesn't interfere with
the traditional ->new (or any other creator that might be used). One
can replace the other anywhere, and no-one is forced to use the new
style.
Anno