Skip Menu |

This queue is for tickets about the Object-Lazy CPAN distribution.

Report information
The Basics
Id: 61506
Status: resolved
Priority: 0/
Queue: Object-Lazy

People
Owner: Nobody in particular
Requestors: user42 [...] zip.com.au
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: can() coderef return
Date: Tue, 21 Sep 2010 11:27:07 +1000
To: bug-Object-Lazy [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
In Object::Lazy 0.03 it seems calling the coderef returned by can() reaches the target method with the lazy object not the target object. For example the program foo.pl below prints hello called with: Object::Lazy=HASH(0x99a0110) where I hoped for hello called with: MyClass=HASH(0x9f08b50) Calling the can() coderef isn't hugely common, but I think it's supposed to be allowed, presumably to save a couple of nanoseconds not doing a method lookup a second time. Something like Convert::Color convert_to() seems fairly typical (other modules have more complicated uses), if( $code = $self->can( "convert_to_$to_space" ) ) { return $code->( $self ); } It could be good for maximum interoperation if Object::Lazy arranged to return a wrapper sub from can() which would dispatch to the real method with the real object. Perhaps something like (only tested a little bit! :-), sub can { my ($self, $method) = @_; my $built_object = $build_object->($self); my $coderef; return (($coderef = $built_object->can($method)) && sub { $_[0] = $built_object; goto $coderef; }); }
use strict; use warnings; { package MyClass; sub new { my ($class) = @_; return bless { name => 'myclass' }, $class; } sub hello { my ($self) = @_; print "hello called with: $self\n"; } } use Object::Lazy; my $lazy = Object::Lazy->new (sub{ return MyClass->new(); }); my $coderef = $lazy->can('hello'); $coderef->($lazy);
resolved in Object-Lazy-0.07 I always have to write back the build object to $_[0]. I forgot that at can. my $object = Object::Lazy->new(...); # ref $object eq 'Object::Lazy' $object->can('method'); # ref $object eq 'RealObject' now Thank you.
Subject: Re: [rt.cpan.org #61506] can() coderef return
Date: Thu, 30 Sep 2010 11:49:44 +1000
To: bug-Object-Lazy [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
"Steffen Winkler via RT" <bug-Object-Lazy@rt.cpan.org> writes: Show quoted text
> > I always have to write back the build object to $_[0].
The docs could explain that that's done, I didn't realize it worked that way. (I thought each method might be dispatched dynamically or something.) Incidentally, I see $lazy->VERSION returns the Object::Lazy version number, not the target object. Is that intentional? The docs could note that, or not. I suppose other methods from UNIVERSAL.pm are affected similarly (AUTOLOAD() not run if a superclass can satisfy a method). The docs might note that as a limitation, in case other modules augment UNIVERSAL. Show quoted text
> $object->can('method'); > # ref $object eq 'RealObject' now
It might be worth noting the can() coderef is only good for the particular caller, it can't be used with another scalar holding the lazy object, nor another lazy of the same type. A more sophisticated coderef return could keep hairy combinations out of trouble ...
Thank you for the quick response. I have fixed the bug quickly. Next I change the documentation and think about UNIVERSAL.pm methods. May I send you the next version before I upload this to CPAN? So you can see if I understand all your hints correctly. Regards Steffen
I have released a new version 0.09. The documentation ist extended. New UNIVERSAL.pm methods are implemented.