using function reference

V

vabby

Hi
I have a a problem with function reference. I have a main module,
mod_main2.pl in which I call the function of a another module
Batlib22.pm, in the traditional way, ie by exporting the symbols in the
Batlib22.pm and not using the object oriented way.

So my Batlib.pm looks something like this:

package Batlib22;
use Exporter();
@ISA = qw(Exporter);
push(@EXPORT, '&fun2');

sub fun2
{

my ($arg1) = $_[0];
print "\n Now in Batlib2::fun2 fn \n";
print $arg1;

}
1;

and my mod_main2.pl looks sthing like this.

use Batlib22;
print " before calling fun2 the traditional way";
my $reffn = \&fun2(100);
&$reffn();
exit(0);


There are two problems I am facing :
1) As soon as I make the declaration my $reffn = \&fun2(100);
fun2() gets executed . i dont want this to happen . i want to store its
reference ina variable and use that variable latyer on for execution
purposes.
2)the call &$reffn(); gives rise to run time error:Not a CODE reference
at ./mod_main2.pl

PLz advise me where am I agoing wrong. I would really appreciate ur
help, here as I have a deadline to meet.

Tx in advance.
Vaibhav
 
J

Josef Moellers

vabby said:
Hi
I have a a problem with function reference. I have a main module,
mod_main2.pl in which I call the function of a another module
Batlib22.pm, in the traditional way, ie by exporting the symbols in the
Batlib22.pm and not using the object oriented way.

So my Batlib.pm looks something like this:

package Batlib22;
use Exporter();
@ISA = qw(Exporter);
push(@EXPORT, '&fun2');

sub fun2
{

my ($arg1) = $_[0];
print "\n Now in Batlib2::fun2 fn \n";
print $arg1;

}
1;

and my mod_main2.pl looks sthing like this.

use Batlib22;
print " before calling fun2 the traditional way";
my $reffn = \&fun2(100);

Don't put any arguments here!

AFAICT with argument it parses as
my $reffn = \(&fun2(100));
meaning that you call fun2 "the deprecated way" and then assign a
reference to its return value (1, the return value of "print $arg1") to
$reffn.

Data::Dumper says:

$VAR1 = \'1';
 
T

Tad McClellan

vabby said:
There are two problems I am facing :
1) As soon as I make the declaration my $reffn = \&fun2(100);
fun2() gets executed .


That is the semantic for the syntax that you are using.

i dont want this to happen .


Then don't use that syntax. :)

i want to store its
reference ina variable and use that variable latyer on for execution
purposes.


my $reffn = \&fun2; # make a code ref

2)the call &$reffn(); gives rise to run time error:Not a CODE reference
at ./mod_main2.pl


$reffn->(100); # call a code ref

I have a deadline to meet.


That is irrelevant to everyone else but you.
 
V

vabby

thanks for the speedy response.
ok ay so how should I make teh call. even doing te faloowing gives me
teh same error
my $arg = 100;
my $reffn = \&fun2($arg);
&$reffn();

Tx
Vaibhav
 
V

vabby

I really would not want to do stjing like this
my $reffn = \&fun2;

as then i will need to store the args to be passed to the functions
seperate;y .. Is there any other way out..

Tx
Vaibhav
 
A

anno4000

vabby said:
I really would not want to do stjing like this
my $reffn = \&fun2;

as then i will need to store the args to be passed to the functions
seperate;y .. Is there any other way out..

my $reffn = sub { fun2( @args) };

but that's a kludge and not particularly robust.

Anno
 
M

Mirco Wahab

vabby said:
I really would not want to do stjing like this
my $reffn = \&fun2;

as then i will need to store the args to be passed to the functions
seperate;y .. Is there any other way out..

Closure?

---- 8< Batlib22.pm -----------

package Batlib22;

use vars qw'@ISA';
require Exporter;

@ISA = qw(Exporter);
push(@EXPORT, '&fun2');

sub fun2 {
my $arg1 = shift;
return
sub {
my $arg = $arg1;
print "\n Now in Batlib2::fun2 fn \n";
print $arg;
}
}
1;

---- 8< vabby.pl -----------------------

use Batlib22;

print " before calling fun2 the traditional way";
my $reffn = fun2(100);

$reffn->(); # variant 1
&$reffn(); # variant 2 (almost the same)


Regards

Mirco
 
A

Arved Sandstrom

Tad McClellan said:
That is irrelevant to everyone else but you.

Not necessarily. Your mutual fund may hold shares in the company he works
for. :)

AHS
 
A

anno4000

Ingo Menger said:
Who cares.


It's no less robust than anything else in perl.

My point in particular was that @args will be evaluated at call time,
not when $reffn is assigned. That may come unexpected. If it isn't
what the user wants, a closure can be used:

my $reffn = do {
my @args = @args; # make a copy in local scope...
sub { fun2( @args) }; # ...and use that
};

Anno
 
I

Ingo Menger

My point in particular was that @args will be evaluated at call time,
not when $reffn is assigned. That may come unexpected.

Or it may be just what was intended. After all, it works just like in a
named subroutine.

But "not robust" could be understood quite differently, for example,
that the part of the interpreter that deals with closures and anonymous
subs will not deliver consistent results or even crash occasionally.
This is simply not the case. And the semantics of accessing a global
(relative to the sub body) variable are quite clear and consistent.
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,110
Latest member
OdetteGabb
Top