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


in reply to Re: A few Perl OOP questions.
in thread A few Perl OOP questions.

my $class = ref( $type ) || $type;
[...]That is an idiom which is disparaged these days as too admissive of bugs.

It is disparaged by some people. I've repeatedly criticized people for disparaging it without specifying what one should replace it with. I find the most obvious way of not using it to be too likely to produce way-too-confusing errors:

package Purified::Confusion; sub new { # my $proto = shift(@_); # my $class = ref($proto) || $proto; # To restore sanity, replace the line below # with the above two commented lines. my $class = shift(@_); bless [@_], $class; } sub get { my $self = shift(@_); return @$self if ! @_; return @{$self}[@_]; } 1;
and
#!/usr/bin/perl -w use strict; use Purified::Confusion; my $one= Purified::Confusion->new('a'); print $one->get(), $/; # A quite common idiom in Perl code # which produces no errors nor warnings: my $two= $one->new('b'); # ...but that produces not at all what is desired, # if the module author heeded the FUD; as we see here: print $two->get(), $/;
produces the following output:
a Can't locate object method "get" via package "Purified::Confusion=ARRA +Y(0x182f0d0)" at confusion.pl line 13.
which I'll bet would confuse the vast majority of module users.

If you are going to spread Cargo Cult FUD about

my $class = ref( $proto ) || $proto;
then at least take the responsibility to tell people what you suggest they replace it with! Perhaps even keep handy a link to a discussion of both sides of the controversy.

I have my own techniques for avoiding such problems when I feel it is appropriate (usually by having my class and instance methods in different name spaces) which are way too much trouble to be the default idiom for writing OO Perl. So I advocate people follow this fine example found in the standard Perl documentation until such time as they feel the desire/need to move to more advanced techniques that can properly address class/instance method collisions without introducing such horridly confusing possibilities.

                - tye