| Author |
using function reference
|
|
|
| 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
| |
| Josef Moellers 2006-12-19, 8:06 am |
| vabby wrote:
> 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.
>=20
> So my Batlib.pm looks something like this:
>=20
> package Batlib22;
> use Exporter();
> @ISA =3D qw(Exporter);
> push(@EXPORT, '&fun2');
>=20
> sub fun2
> {
>=20
> my ($arg1) =3D $_[0];
> print "\n Now in Batlib2::fun2 fn \n";
> print $arg1;
>=20
> }
> 1;
>=20
> and my mod_main2.pl looks sthing like this.
>=20
> use Batlib22;
> print " before calling fun2 the traditional way";
> my $reffn =3D \&fun2(100);
Don't put any arguments here!
AFAICT with argument it parses as
my $reffn =3D \(&fun2(100));
meaning that you call fun2 "the deprecated way" and then assign a=20
reference to its return value (1, the return value of "print $arg1") to=20
$reffn.
Data::Dumper says:
$VAR1 =3D '1';
--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
| |
| Tad McClellan 2006-12-19, 8:06 am |
| vabby <vaibhav.aparimit@gmail.com> wrote:
> 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.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
|
| 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
| |
|
| 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
| |
| anno4000@radom.zrz.tu-berlin.de 2006-12-19, 8:06 am |
| vabby <vaibhav.aparimit@gmail.com> wrote in comp.lang.perl.misc:
> 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
| |
| Mirco Wahab 2006-12-19, 8:06 am |
| vabby wrote:
> 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
| |
| Ingo Menger 2006-12-21, 7:02 pm |
|
anno4000@radom.zrz.tu-berlin.de schrieb:
> vabby <vaibhav.aparimit@gmail.com> wrote in comp.lang.perl.misc:
>
> my $reffn = sub { fun2( @args) };
>
> but that's a kludge
Who cares.
> and not particularly robust.
It's no less robust than anything else in perl.
| |
| anno4000@radom.zrz.tu-berlin.de 2006-12-21, 7:02 pm |
| Ingo Menger <quetzalcotl@consultant.com> wrote in comp.lang.perl.misc:
>
> anno4000@radom.zrz.tu-berlin.de schrieb:
>
>
> 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
| |
| Ingo Menger 2006-12-22, 8:02 am |
|
anno4000@radom.zrz.tu-berlin.de schrieb:
> Ingo Menger <quetzalcotl@consultant.com> wrote in comp.lang.perl.misc:
>
> 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.
|
|
|
|