Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > reference to object method

Reply
Thread Tools

reference to object method

 
 
Hobo Salesman
Guest
Posts: n/a
 
      06-14-2006
Can I store a reference to an object method in a scalar and use that to
call the method? Something like (and I'm sure this is wrong):

$methodRef = \$object->method();
&{$methodRef}($argument);

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-15-2006
"Hobo Salesman" <(E-Mail Removed)> wrote:
> Can I store a reference to an object method in a scalar and use that to
> call the method? Something like (and I'm sure this is wrong):
>
> $methodRef = \$object->method();
> &{$methodRef}($argument);



my $ref = sub { $object->method(@_) };

$ref->($argument);

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      06-15-2006
>>>>> "x" == xhoster <(E-Mail Removed)> writes:

x> "Hobo Salesman" <(E-Mail Removed)> wrote:
>> Can I store a reference to an object method in a scalar and use that to
>> call the method? Something like (and I'm sure this is wrong):
>>
>> $methodRef = \$object->method();
>> &{$methodRef}($argument);



x> my $ref = sub { $object->method(@_) };

x> $ref->($argument);

much better to use UNIVERSAL::can:

my $meth = $obj->can( 'method_name' ) ;

$meth->( $arg ) ;

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-15-2006

Quoth Uri Guttman <(E-Mail Removed)>:
> >>>>> "x" == xhoster <(E-Mail Removed)> writes:

>
> x> "Hobo Salesman" <(E-Mail Removed)> wrote:
> >> Can I store a reference to an object method in a scalar and use that to
> >> call the method? Something like (and I'm sure this is wrong):
> >>
> >> $methodRef = \$object->method();
> >> &{$methodRef}($argument);

>
> x> my $ref = sub { $object->method(@_) };


Note that $object must be a lexical here, or the anon sub won't close
over it. (But it was anyway, of course... )

> x> $ref->($argument);
>
> much better to use UNIVERSAL::can:
>
> my $meth = $obj->can( 'method_name' ) ;
>
> $meth->( $arg ) ;


....except that just calls the method as a sub, i.e. the invocant is not
preserved. For that you need another closure, hence Xho's answer is the
simplest possible.

Ben

--
I touch the fire and it freezes me, [(E-Mail Removed)]
I look into it and it's black.
Why can't I feel? My skin should crack and peel---
I want the fire back... Buffy, 'Once More With Feeling'
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-15-2006
Uri Guttman wrote:
> xhoster <(E-Mail Removed)> writes:
>> "Hobo Salesman" <(E-Mail Removed)> wrote:
>>> Can I store a reference to an object method in a scalar and use that to
>>> call the method?

>>
>> my $ref = sub { $object->method(@_) };
>>
>> $ref->($argument);

>
> much better to use UNIVERSAL::can:

^^^^^^^^^^^

> my $meth = $obj->can( 'method_name' ) ;
>
> $meth->( $arg ) ;


Why?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-15-2006
Uri Guttman wrote:

>>>>>> "x" == xhoster <(E-Mail Removed)> writes:

>
> x> "Hobo Salesman" <(E-Mail Removed)> wrote:
> >> Can I store a reference to an object method in a scalar and use that
> >> to call the method? Something like (and I'm sure this is wrong):
> >>
> >> $methodRef = \$object->method();
> >> &{$methodRef}($argument);

>
>
> x> my $ref = sub { $object->method(@_) };
>
> x> $ref->($argument);
>
> much better to use UNIVERSAL::can:
>
> my $meth = $obj->can( 'method_name' ) ;
>
> $meth->( $arg ) ;


Ah, but these are not the same. The way I read the OP, the idea is to
freeze both the object and the method in a scalar, and that's what Xhos
solution does. Yours only catches the method, the object would still
have to be supplied as an argument.

Anno
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-15-2006
>>>>> "GH" == Gunnar Hjalmarsson <(E-Mail Removed)> writes:

GH> Uri Guttman wrote:
>> xhoster <(E-Mail Removed)> writes:
>>> "Hobo Salesman" <(E-Mail Removed)> wrote:
>>>> Can I store a reference to an object method in a scalar and use that to
>>>> call the method?
>>> my $ref = sub { $object->method(@_) };
>>> $ref->($argument);

>> much better to use UNIVERSAL::can:

GH> ^^^^^^^^^^^

>> my $meth = $obj->can( 'method_name' ) ;
>> $meth->( $arg ) ;


GH> Why?

you don't need the extra complexity of a closure. why do something that
is not needed?

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-15-2006
>>>>> "AS" == Anno Siegel <(E-Mail Removed)-berlin.de> writes:

AS> Uri Guttman wrote:
>>>>>>> "x" == xhoster <(E-Mail Removed)> writes:

>>

x> "Hobo Salesman" <(E-Mail Removed)> wrote:
>> >> Can I store a reference to an object method in a scalar and use that
>> >> to call the method? Something like (and I'm sure this is wrong):
>> >>
>> >> $methodRef = \$object->method();
>> >> &{$methodRef}($argument);

>>
>>

x> my $ref = sub { $object->method(@_) };
>>

x> $ref->($argument);
>>
>> much better to use UNIVERSAL::can:
>>
>> my $meth = $obj->can( 'method_name' ) ;
>>
>> $meth->( $arg ) ;


AS> Ah, but these are not the same. The way I read the OP, the idea is to
AS> freeze both the object and the method in a scalar, and that's what Xhos
AS> solution does. Yours only catches the method, the object would still
AS> have to be supplied as an argument.

i read it as i answered it. we can agree the OP is somewhat ambiguous
there. i see reference to a method (found via an object). he seems to
have the object floating around and so it doesn't need to be stored in
the closure (and so the closure isn't needed). so the OP would need to
clarify this.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-15-2006
Uri Guttman <(E-Mail Removed)> wrote:
> >>>>> "x" == xhoster <(E-Mail Removed)> writes:

>
> x> "Hobo Salesman" <(E-Mail Removed)> wrote:
> >> Can I store a reference to an object method in a scalar and use that
> >> to call the method? Something like (and I'm sure this is wrong):
> >>
> >> $methodRef = \$object->method();
> >> &{$methodRef}($argument);

>
> x> my $ref = sub { $object->method(@_) };
>
> x> $ref->($argument);
>
> much better to use UNIVERSAL::can:


Neat, I've never considered can to return more than a boolean.

> my $meth = $obj->can( 'method_name' ) ;
>
> $meth->( $arg ) ;


Doesn't that call the method as a regular subroutine, with $arg where $self
should be?

Wouldn't that need to be: ?
$obj->$meth($arg);

The anonymous sub method stores the object as a closure (well, as long
as it is a lexical, anyway), whereas the UNIVERSAL::can method requires you
to keep your hands on the object your own self. I thought that the point
of the OP was not to have to do that, but maybe I misread his point.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Hobo Salesman
Guest
Posts: n/a
 
      06-15-2006
Hobo Salesman wrote:
> Can I store a reference to an object method in a scalar and use that to
> call the method?


Thanks for the solutions. I don't have a specific application, I'm just
learning OOP and figuring out different ways to structure things.
THanks again.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb question: in documentation, why Object#method, and not Object.method ? Elf M. Sternberg Ruby 15 07-29-2009 01:20 AM
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
ASP.NET 2.0: master pages and web user controls: reference to a non-shared member requires an object reference bminder ASP .Net 0 06-24-2005 12:22 AM
asp.net 2005 question re: "reference to a non-shared member requires an object reference" ce ASP .Net 1 06-23-2005 09:15 PM



Advertisments