hi,
I was exercising package creation and its usage in Perl. I started with a simple program,
1. Created a package called A
2. Created a package called B
3. A program which includes those packages, and uses its services.
# A.pm
package A;
use warnings;
my $l_var = 10; # lexically scoped variable
$g_var = 20; # global variable
sub print_arg{
my $v = shift;
print "\nyou passed: $v\n";
}
sub sum{
my ($var1, $var2) = @_;
@cal = caller;
print "\nfunc2: im called by, $cal[0]::$cal[1] at $cal[2]";
return $var1+$var2;
}
#$res = func2(1,1);
1;
# B.pm
package B;
use warnings;
$b = 'package B';
sub fib{
my $range = shift;
($a, $b) = (0, 1);
while($b<$range){
print "$b ";
($a, $b) = ($b, $b+$a);
}
}
#fib(30);
1;
# demo_package_test.pl
use A;
use B;
A::print_arg('this is an arg.');
my $res = A::sum(10, $A::g_var);
print "\nres = $res";
print "\n---\nb = $B::b";
&B::fib(50);
Output:
you passed: this is an arg.
func2: im called by, main::test_package.pl at 7
res = 30
---
b = Undefined subroutine &B::fib called at test_package.pl line 11.
I dont understand, why im not able to call a function which is defined in the module or Why does Perl says 'Undefined subroutine'.
thanks,
katharnakh.