Hi monks,
I want to write one method inside one package that returns subroutine reference.
Then from calling program I want to call that method. Below is my code :
package myTest;
use strict;
use vars qw($VERSION %EMP @ARR $VAL);
$VERSION = '0.01';
%EMP = ("1", "Ravish", "2", "Mike");
@ARR = (5,6,7,8);
sub new {
my $package = shift;
return bless({}, $package);
}
sub add {
my $self = shift;
my ($a, $b) = @_;
my $c = $a + $b;
push @ARR, $c;
return @ARR;
}
sub greet {
my($grt) = @_;
return sub {
my($subject)= @_;
print "$grt $subject \n";
};
}
1;
this is my calling program :
use strict;
use myTest;
my @chk = @myTest::ARR;
my $obj = myTest->new();
my $rs1 = $obj->greet("hello");
my $rs2 = $obj->greet("my fair");
&$rs1("world") ; # it should print "hello world" but not
&$rs2("lady") ; # it should "my fair lady" but not
Is this the right way to call that function from subroutine ?
Any help will be appriciated.
Thanks in advance