On 5/18/07, Tom Metro <[EMAIL PROTECTED]> wrote:
> Ben Tilly wrote:
[...]
> > Also note that AUTOLOAD and inheritance do NOT play well together.
> > That's another reason to avoid that solution.
>
> I had that thought as well. Isn't there a workaround where your AUTOLOAD
> handler can explicitly hand off to the superclass AUTOLOAD?

Nope.  And even if there was, that's insufficient in the face of
multiple inheritance.

At one point I started coding a solution to the problem.  It is on
CPAN as Class::AutoloadCAN.  I meant to flesh it out further (eg
properly implement private, protected and public methods) but lost
interest.  If someone else wants to take it over, be my guest.  I
discovered part way through that I'm the wrong person to solve
problems that I'm not particularly interested in...  (Particularly
since several people have come out with object systems that solve it,
and Perl 6 promises to solve it as well.)

See http://www.perlmonks.org/?node_id=342804 and
http://www.perlmonks.org/?node_id=446700 for more background.

[...]
> That's also a nice solution. Should get this stuff on CPAN.

Feel free to take it and run with it.

> > The only big drawbacks to this are that code that checks ref can
> > break, and we might break code that depends on the stringification of
> > $object.  (Think inside-out objects.)
>
> Isn't there also a potential problem if the if something upstream of the
> reblessing saves a reference to the object, or is this true:
>
>    use Scalar::Util 'refaddr';
>    my $obj = {};
>    my $before = refaddr($obj);
>    my $newobj = bless($obj, 'foo');
>    print "true\n" if $before eq refaddr($newobj);
>
> Perl says it is. Makes sense, as although bless returns an object, it
> shouldn't create a new one.

There is no such problem.  Though the following code may surprise you.

my %hash;
my $ref = \%hash;
my $obj = bless \%hash, "Test";
print "ref: $ref\nobj: $obj\n";

> (The man page for Scalar::Util[1] doesn't say what data type refaddr()
> returns, but the examples show what appears to be a decimal number,
> rather than a hex string, as I would have expected, so that should
> probably be a numeric test above.)

Good point.

> 1. http://search.cpan.org/~gbarr/Scalar-List-Utils-1.19/lib/Scalar/Util.pm
>
>
> >>      my $subclass = __PACKAGE__ . '::test_method1::Module::Under::Test';
> >>      my $mut = Module::Under::Test::new($subclass);
> >> [...]
> >> But if new() is declared in a superclass, that breaks.
> >
> > New being declared in a superclass isn't a problem.
>
> Are you sure? I thought these:
>    Module::Under::Test->new
>    new Module::Under::Test
> would cause a search for new() in the class hierarchy, but
>    Module::Under::Test::new
> will only try to run new() in package Module::Under::Test.

Oops, missed that you were making a function call rather than  a
method call.  But you can change that line and you're fine.

> Reblessing is a better solution, anyway.
>
> Thanks Ben. An informative post, as usual.

You're welcome and *blush*.

Ben
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to