Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Beatnik (493)

Beatnik
  (email not shown publicly)
http://www.ldl48.org/

A 29 year old belgian who likes Mountain Dew, Girl Scout Cookies, Tim Hortons French Vanilla Flavoured Cappucinno, Belgian beer, Belgian chocolate, Belgian women, Magners Cider, chocolate chipped cookies and Perl. Likes snowboarding, snorkling, sailing and silence. Bach can really cheer him up! He still misses his dog.

Project Daddy of Spine [sf.net], a mod_perl based CMS.

In his superhero time (8.30 AM to 5.30 PM), he works on world peace.

Journal of Beatnik (493)

Wednesday November 08, 2006
02:45 AM

Referencing methods

[ #31539 ]
Yesterday evening, after a long days work (just in my defense), I was working on some wrapper code. One of the features required for a method reference. I initially had this:

$method = $object->method;

which wasn't doing anything close to what I needed. I soon realized that I didn't need the output but the actual subroutine reference.. so I came up with:

$method = \&{$object->method};

At that time, it was about 1 AM (and I had spend the better part of the evening watching TV while not trying to think about code in an effort to come to an aha-erlebnis. Something like

$method = \&Object::method;

might be a better idea, as long as I don't forget to pass the $object as the first argument.

Todays lesson: Never work on code while watching Kate Beckinsale jump around in weird outfits. Never post a blog entry about coding while watching Kate Beckinsale in weird outfits before drinking coffee.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Why not just sub{}?
      $method = sub { $object->$method };

    -Dom

    • Now that I've had some coffee, I can't really remember anymore what I already got and what I was considering on using... *must .. race .. home .. quickly*
      • I sympathise. I've often wondered deeply on the following day about code that got written after midnight...

        -Dom

  • $method = \&Object::method;

    Hardcoding considered suboptimal (for a range of reasons). You want this:

    $method = $object->can( 'method' );

    You have to remember to pass a reference to $object in either case, though. No getting around that in Perl.

    Therefore you would normally use a closure as per Dominic’s suggestion.