http://qs321.pair.com?node_id=175644


in reply to Re: Problem with My New Perl Object
in thread Problem with My New Perl Object

To make even neater write:

sub new { my $pkg = shift; return bless {}, ref $pkg || $pkg; }
to be able to pull off tricks like this:

use strict; use warnings; package BaseClass; sub new { my $pkg = shift; bless {}, ref $pkg || $pkg; } sub bar { $_[0]->{'BaseClass::bar'} = $_[1]; } package SubClass; our @ISA = qw/BaseClass/; package main; my $obj = SubClass->new; my $foo = $obj->new; printf "obj isa %s\nfoo isa %s\n", $obj, $foo;

Using the  bless {}, ref $pkg || $pkg idiom you are writing classes that are safe to subclass.

Do have a look at Damian Conways excellent book Object Oriented Perl

Kay