Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Wrapping a method of an instance

3 views
Skip to first unread message

Bill Moseley

unread,
Oct 17, 2012, 1:01:24 AM10/17/12
to mo...@perl.org
I need some help with understanding the Class::MOP::Method docs. Can I use
that to apply a modifier?


Catalyst used to call $c->prepare_body_chunk which made it easy for a
plugin or role to track upload progress. Now, this all happens in a loop
in the Catalyst::Request instance in its prepare_body() method.

while ( defined ( my $buffer = $self->read() ) ) {
$self->prepare_body_chunk($buffer);
}

For upload progress I need to know the query params, have access to the
cache (to save the progress), and the bytes read so far, of course. But,
I'm not sure how best to get at that.

I can get at the request object before this code is run
($c->req->meta->get_method). Is is possible to use
Class::MOP::Method::Wrapped to wrap, say, read with a closure that has the
cache object and some unique upload ID?

Essentially what I want is something like this, unless, of course, there's
an easier way I've missed:

my $cache = $c->cache;
my $token = $c->req->parameters->{token};

# somehow apply this to "after" $c->request->meta->get_method( 'read' )
sub { $cache->increment( $token, shift->_read_position ) };


Is it possible to add a modifier like that?

Thanks,


--
Bill Moseley
mos...@hank.org

Tomas Doran

unread,
Oct 17, 2012, 3:31:17 AM10/17/12
to Bill Moseley, mo...@perl.org

>
> Is it possible to add a modifier like that?


Yes, but you probably don't want to.

It's almost certainly better to apply a request role. Have a look at the changes I did to C::P::UploadProgress:

http://dev.catalystframework.org/svnweb/Catalyst/revision?rev=14346
http://dev.catalystframework.org/svnweb/Catalyst/revision?rev=14347

to work due to exactly the same issue.

Cheers
t0m

Bill Moseley

unread,
Oct 17, 2012, 9:17:02 AM10/17/12
to Tomas Doran, mo...@perl.org
On Wed, Oct 17, 2012 at 12:31 AM, Tomas Doran <bobt...@bobtfish.net> wrote:

>
> >
> > Is it possible to add a modifier like that?
>
>
> Yes, but you probably don't want to.
>
> It's almost certainly better to apply a request role. Have a look at the
> changes I did to C::P::UploadProgress:
>

Yes, I think that's a better approach. But, you added some hooks in
Catalyst to make it possible.

But, I'd still like to understand Moose a bit better, and this is a good
example.

Granted may not be appropriate in this case, but I'd like to understand how
to programmatically add a method modifier like in my original message. I
didn't get far looking at the Class::MOP::Method docs. (I'm also a bit
curious how that's different than monkey patching.)

And more specific to this problem:

I'm a bit lost by this code -- Is that to get at the require_class_accessor
attribute? Or is there a reason you cannot use "with"?


Class::MOP::class_of('MooseX::RelatedClassRoles')
->apply($c->meta, name => 'request', require_class_accessor => 0);
0 new messages