in reply to why "Bless" in OO Perl
{ package MyClass; sub new { my ($class) = @_; my $self = {}; return $self; } sub test { print("Hi!"); } } my $o = MyClass->new(); $o->test();
outputs
Can't call method "test" on unblessed reference
Because the hash has not been associated with a package, Perl doesn't know which test you meant to call. bless is the means by which you create this association.
{ package MyClass; sub new { my ($class) = @_; my $self = {}; bless($self, $class); # Added return $self; } sub test { print("Hi!"); } } my $o = MyClass->new(); $o->test();
outputs
Hi!
So how do Java and C++ work without something like bless? It's a combination of having constructors and of the finer grain of their type systems.
|
|---|