LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-24-2005, 11:27 AM   #1
mschutte
Member
 
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68

Rep: Reputation: 15
method references in perl


Hi! I am trying to create a hard reference to a method bound to an object (yes, blessed reference) with perl, while 'use strict' is on. I am not able to find the correct syntax, neither
\&$self->method
nor
\&{$self->method}
compile. Can anybody help me?

Thanks, mschutte
 
Old 04-24-2005, 01:25 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
You can call a method stored in a variable by just putting the variable in the place you'd normally put the name of the method. For instance:

Code:
# say you've got an $object, and a method poke_me
# which sometimes takes an argument

my $method_ref = "poke_me";

$object->$method_ref;
# calls $object->poke_me

#or with an argument
$object->$method_ref("really hard")
# calls $object->poke_me("really hard")
 
Old 04-25-2005, 10:05 AM   #3
mschutte
Member
 
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68

Original Poster
Rep: Reputation: 15
Well, great stuff, but it's not what I need. Using this method, I still have to tell the blessed ref to operate on. Probably you understand what I mean if I explain what I need it for.

I am using XML::Parser in a class and want to assign the handlers to some methods. This means:
my $expat = new XML::Parser(Handlers => { Start => \&startElement });
but with a method instead of a function call:
my $expat = new XML::Parser(Handlers => { Start => \&$self->startElement });
when $self is a blessed reference. It doesn't work. Is there any way to make it work? If yes, please post a reply...

Thanks, mschutte
 
Old 04-25-2005, 12:25 PM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Ah, I see. Ok, well depending on whether you want to call the method as an object method or a class method, it will be different. If the method you want to call is dependent on the object's state, you want the object call, and you'd do it with a closure, like this:

Code:
my $object = Whatever->new;

my $expat = XML::Parser->new(Handlers => { Start => sub {$object->method} });
Here, the closure ( sub {$object->method} ) will "remember" the object for its (the closure's) entire life, even if the object goes out of scope elsewhere. If you want to call a class method, not bound to any particular instance of the class, try this:

Code:
my $object = Whatever->new;

my $expat = XML::Parser->new(Handlers => { Start => \&Whatever::method} });
Note that using fully qualified method names like this can be problematic with inherited classes. Using \&Whatever->method may also work, I'm not sure. You could also embed Whatever->method in a closure as in the first example. Please note the distinction, however, between Whatever->method and $object->method.

To anyone reading this, I highly recommend learning to use closures, they can be incredibly powerful and elegant solutions.
 
Old 04-26-2005, 09:46 AM   #5
mschutte
Member
 
Registered: Jan 2005
Location: Innsbruck, Austria
Distribution: Debian GNU/Linux Lenny
Posts: 68

Original Poster
Rep: Reputation: 15
Yes, really elegant. Thank you!

-- mschutte
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Not Allowed: The requested method POST is not allowed for the URL /writedhcp.p WiWa Linux - Networking 15 01-06-2011 01:20 PM
Perl undefined subroutine /can't locate object method cobien Programming 1 11-17-2003 10:47 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
references jenny_psion Programming 0 08-12-2003 03:06 AM
The Best Method to install Set up a Unix System with PERL, PHP and Apache rhuser Linux - Software 1 03-01-2003 10:31 PM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration