Home > Archive > PERL Beginners > December 2006 > probrlm using function reference
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
probrlm 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
| |
| nobull67@gmail.com 2006-12-19, 8:00 am |
| On Dec 19, 11:39 am, "vabby" <vaibhav.apari...@gmail.com> wrote:
> my $reffn = \&fun2(100);
> &$reffn();
A Perl function reference is a refererence to the function without an
argument list. The argument list is provided when you _call_ the
function.
my $reffn = \&fun2;
&$reffn(100);
If you want a function reference that when called calls fun2(100) then
you need to create another function that simply calls fun2(100) and use
a reference to that function instead. This can be an anonymous
function.
my $reffn = sub{ fun2(100) };
&$reffn();
Note: These days...
&$reffn(LIST);
....is usually written...
$reffn->(LIST);
> PLz advise me where am I agoing wrong. I would really appreciate ur
> help, here as I have a deadline to meet.
Is the deadline so tight you can't afford to type words in full?
| |
|
| thanks for ethe speedy reponse
When using the anonymous subroutine funda.
>
> my $reffn = sub{ fun2(100) };
> &$reffn();
>
I get the followng error.
syntax error at ./mod_main2.pl line 30, near "sub(fun2($arg));"
Global symbol "$reffn" requires explicit package name at ./mod_main2.pl
line 31.
Execution of ./mod_main2.pl aborted due to compilation errors.
Vaibhav
| |
| Paul Lalli 2006-12-19, 8:00 am |
| vabby wrote:
> thanks for ethe speedy reponse
> When using the anonymous subroutine funda.
> I get the followng error.
> syntax error at ./mod_main2.pl line 30, near "sub(fun2($arg));"
Look at that syntax error and contrast it to the code you claimed to
have used. See the difference?
Please post your REAL code, do not retype.
Paul Lalli
|
|
|
|
|