You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Anthony Gardner <cy...@yahoo.co.uk> on 2005/09/16 13:57:23 UTC
Code Refs
This is just Perl related but as it's running under
mod_perl, I'm sure you lot won't mind ;)
I'm trying to benchmark a piece of code with timeit
and I need a code ref. The thing is, I can't make a
code ref out of ....
$self->method_to_invoke( $arg1, $arg2 );
I've tried various obvious ways but to no avail.
CIA
-Anthony
___________________________________________________________
To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre. http://uk.security.yahoo.com
Re: Code Refs
Posted by Jeff <mo...@aquabolt.com>.
> This is just Perl related but as it's running under
> mod_perl, I'm sure you lot won't mind ;)
>
> I'm trying to benchmark a piece of code with timeit
> and I need a code ref. The thing is, I can't make a
> code ref out of ....
>
> $self->method_to_invoke( $arg1, $arg2 );
>
> I've tried various obvious ways but to no avail.
>
> CIA
>
my $coderef = $self->can('method_to_invoke');
looks through the inheritance tree for a sub called 'method_to_invoke'
and returns the coderef if one exists.
Regards
Jeff
Re: Code Refs [OT]
Posted by John ORourke <jo...@o-rourke.org>.
Off topic but cc'd to the list to save dupes!...
Anthony Gardner wrote:
>Well, it wasn't obvous to me. So, what does sub{}
>actually do?
>
>
It simply creates a sub but just doesn't give it a name, and it has a
'return value' which is a code ref. Try to imagine normal subs as
simply assigning a code ref to a constant name (confused yet?)
>but my problem involved a ref to a class and a
>function name which I haven't seen documented.
>
>
Right, forget what you learned in C++ classes... in Perl an object
method is simply a sub with the object ref passed automatically as the
first parameter, so
$self->method($arg) is the same as &method($self,$arg)
Time to RT(F)M at this point, start with 'man perltoot' :)
Once you realise how simply perl handles subs, objects etc it all
becomes clear and easy.
hope this helps,
John
>Unless s.o. can point me to the right place.
>
>Thanks for the tip and any more info on what is
>actually happening when we invoke sub{} would be
>great.
>
>
>-Ants
>
>--- John ORourke <jo...@o-rourke.org> wrote:
>
>
>
>>Anthony Gardner wrote:
>>
>>
>>
>>>and I need a code ref. The thing is, I can't make a
>>>code ref out of ....
>>>
>>>$self->method_to_invoke( $arg1, $arg2 );
>>>
>>>
>>>
>>>
>>I'm going to get into trouble for stating the
>>obvious here but have you
>>tried:
>>
>>$code_ref = sub { return shift->method_to_invoke( @_
>>) }
>>
>>(ie. create a new anonymous method which is just a
>>wrapper for your
>>method - if your method is really quick and you're
>>iterating *lots* it
>>may affect timing a little)
>>
>>John
>>
>>
>>
>>
>
>
>
>
>___________________________________________________________
>How much free photo storage do you get? Store your holiday
>snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
>
>
>
Re: Code Refs
Posted by Anthony Gardner <cy...@yahoo.co.uk>.
Well, it wasn't obvous to me. So, what does sub{}
actually do?
I'm clear about ...
my $code_ref = sub{ print "sth" }
as I've wrapped some perlcode in a code ref ;)
I'm clear about ....
sub func_to_invoke() {
print "sth"
}
my $code_ref = \&func_to_invoke()
but my problem involved a ref to a class and a
function name which I haven't seen documented.
Unless s.o. can point me to the right place.
Thanks for the tip and any more info on what is
actually happening when we invoke sub{} would be
great.
-Ants
--- John ORourke <jo...@o-rourke.org> wrote:
> Anthony Gardner wrote:
>
> >and I need a code ref. The thing is, I can't make a
> >code ref out of ....
> >
> >$self->method_to_invoke( $arg1, $arg2 );
> >
> >
> I'm going to get into trouble for stating the
> obvious here but have you
> tried:
>
> $code_ref = sub { return shift->method_to_invoke( @_
> ) }
>
> (ie. create a new anonymous method which is just a
> wrapper for your
> method - if your method is really quick and you're
> iterating *lots* it
> may affect timing a little)
>
> John
>
>
___________________________________________________________
How much free photo storage do you get? Store your holiday
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
Re: Code Refs
Posted by John ORourke <jo...@o-rourke.org>.
Anthony Gardner wrote:
>and I need a code ref. The thing is, I can't make a
>code ref out of ....
>
>$self->method_to_invoke( $arg1, $arg2 );
>
>
I'm going to get into trouble for stating the obvious here but have you
tried:
$code_ref = sub { return shift->method_to_invoke( @_ ) }
(ie. create a new anonymous method which is just a wrapper for your
method - if your method is really quick and you're iterating *lots* it
may affect timing a little)
John
Re: Code Refs
Posted by Damyan Ivanov <di...@creditreform.bg>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Anthony Gardner wrote:
> This is just Perl related but as it's running under
> mod_perl, I'm sure you lot won't mind ;)
>
> I'm trying to benchmark a piece of code with timeit
> and I need a code ref. The thing is, I can't make a
> code ref out of ....
>
> $self->method_to_invoke( $arg1, $arg2 );
>
> I've tried various obvious ways but to no avail.
How about:
my $coderef = sub { $self->method( $arg1, $arg2) };
timethis( $coderef, 10_000 );
dam
- --
Damyan Ivanov 0x9725F63B Creditreform Bulgaria
divanov@creditreform.bg http://www.creditreform.bg/
phone: +359(2)928-2611, 929-3993 fax: +359(2)920-0994
mob. +359(88)856-6067 ICQ 3028500 dam@jabber.minus273.org/Gaim
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
iD8DBQFDKtK+Hqjlqpcl9jsRAnHYAJ4216rJmFD8EL0868sII4b/bV2yfQCgt+jd
44FahNDSQN4q8OMRY3DzsPw=
=RZJz
-----END PGP SIGNATURE-----