[EMAIL PROTECTED] wrote:
Michael G Schwern wrote:
you're right that the case of $class being false may be of interest,
but that's not what this common idiom actually does. The code will
blithely pass a false value to bless (with potentially unexpected
results depending on whether $class is 0, "", or undef). That failure
is an example of where correctness can't be validated by coverage --
where the error lies between the ears of the programmer. :-) If
$class were explictly tested, then Devel::Cover would pick it up
properly, such as in "bless {}, ref $class || $class || die".
I'd say this idiom is one of the ones I am most often affected by in the
work I do for the Kwalitee project - the my$class = ref$proto||$proto;
idiom in constructors. I usually do the following
1. Add code to handle the 'both false' case, similiar to
my $class = ref $proto || $proto;
warn 'wrong calling convention for Class::Constructor::new - try
Class::Constructor->new' and return unless $class;
2. Add a test that makes ref $proto || $proto false, and tidy up the
harness so the warning doesn't mess up the output
my @warn;
my $rc;
eval {
local $SIG{__WARN__} = sub {push @warn, @_};
$rc = Class::Constructor::new();
};
is(@warn, 1, 'warning on calling convention');
like(shift(@warn), qr(wrong calling convention for
Class::Constructor::new - try Class::Constructor->new at ), 'expect
message');
is($rc, undef, 'no object created');
Now D::C is happy, and the code is more robust - to me a win-win. Now
newbies who dont really know perl's OO conventions are gently steered to
the path to enlightenment, and everyone else is only penalised with a
very lightweight unless test.This seems OK to me, but I know opinions on
this cover a wide spectrum.
The only caveat is in regards to those psycho's who like to bless into
the '0' namespace....I believe '' and undef result in blessing into main::
--
Leif Eriksen
Snr Developer
http://www.hpa.com.au/
phone: +61 3 9217 5545
email: [EMAIL PROTECTED]