in reply to Re^5: Perl "new" command
in thread Perl "new" command
Perl leaves to the programmer the task of allocating the object storage, setting the dispatch table and initializing the object for greater flexibility.In theory.
In practice, the overwhelming majority of the programmers screws this up, each and every time they create a class. I bet there are seasoned Perl programmers who haven't managed not not screw up.
Consider:
Whee. Now I can make colour objects, and age objects:package Colour; sub new {bless {colour => $_[1]}, $_[0]} sub colour {$_[0]{colour}} package Age; sub new {bless {age => $_[1]}, $_[0]}} sub age {$_[0]{age}}
Goodie. Now, if there was any flexibility, I'd be able to create a class that's both, using multiple inheritance, and without breaking encapsulation (that is, peeking and making use of the implementation of a parent class):use Colour; use Age; my $c_obj = Colour::->new("orange"); my $a_obj = Age::->new(42); print "I have an object with colour ", $c_obj->colour; print "And another object aged ", $a_obj->age;
If I call Age::->new, I get back an object initialized with an age, but no colour; but if I call Colour::->new, I get one with a colour, but no age.package Age_and_Colour; use Age; use Colour; our @ISA = qw[Age Colour]; sub new { my ($class, $age, $colour) = @_: ... Now what? ... }
Greater flexibility is fine, but if this requires a lot more effort from the programmer, it's pointless. That's like saying C has greater flexibility when it comes to regular expressions: it gives you a minimal string implementation, and leaves everything else to the programmer. Sure, you *can* make more fantastic regular expressions in C than in Perl, but I don't see many people actually doing that.
(cue the Pavlov reactions: "but you shouldn't use MI anyway")
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: Perl "new" command
by tobyink (Canon) on Mar 01, 2012 at 13:42 UTC | |
by JavaFan (Canon) on Mar 01, 2012 at 16:17 UTC |