problem regarding function reference

V

vabby

Hi
So this is my problem scenario. I have a main perl module, mod_main.pl,
and two other modules Batlib1.pm and Batlib2.pm .
In mod_main.pl I create an instance of Batlib2 :
$batlibobj2 = new_Batlib2 Batlib2();

And then an instance of Batlib1 where I pass the object of Batlib2.

$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);

I then make a call to a test function fun1 in Batlib1.
$batlibobj1->fun1()


In my Batlib2.pm I have a simple test function fun2();

Inside Batlib1::fun1() , if I try to call Batlib2::fun2() by
$self->{batlibobj2}->fun2();
It works.

Also if I try to create a reference to the batlibobj2 ref and then call
fun2 the following way it still works:

my $refobj = \$self->{ batlibobj2};
$$refobj->fun2();

But when I try to create a reference to the function fun2 directly and
use it, perl starts cribbing.

my $reffn = \&$self->{batlibobj2}->fun2; ## the perl gives my runtime
error at this point . the error is "Not a CODE reference at
Batlib1.pm line 34".
&$reffn();


Is it that we cant have references to function which are defined in
other modules.
I am actually very new to perl programming . Plz tell where I am going
wrong.
Thanks in advance..

Vaibhav
 
P

Paul Lalli

vabby said:
my $reffn = \&$self->{batlibobj2}->fun2; ## the perl gives my runtime
error at this point . the error is "Not a CODE reference at
Batlib1.pm line 34".
&$reffn();

Is it that we cant have references to function which are defined in
other modules.

No. It's that you can't create a reference to a method call. You can,
however, create a reference to an anonymous subroutine that does
nothing but invoke that method call:

my $reffn = sub { $self->{batlibobj2}->fun2() };

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top