I am learning subroutine reference. I written a code but it is not working
use strict;
use warnings;
sub test1 {
#$arg = shift;
print "arg";
}
sub test2 {
return \&test1;
}
&test2;
Output is nothing. Any thoughts why my code is not working.
&test2 does almost the same as test2(). It runs test2, which returns the reference to test1, but does not run it. You have to dereference the reference to call the referenced code:
test2->();
Or even
test2()->();
First of all,
&test2;
is a poor way of calling a subroutine. Don't use & if you don't even know what it does! That should be
test2;
or
test2();
On to your problem: You never call the returned subroutine!
test2()->();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With