I'm having some trouble figuring out how to make a reference to a subroutine in an external module file. Right now, I'm doing this:
External file
package settingsGeneral;
sub printScreen {
print $_[0];
}
Main
use settingsGeneral;
my $printScreen = settingsGeneral::printScreen;
&$printScreen("test");
but this result into an error: Can't use string ("1") as a subroutine ref while "strict refs" in use
As noted in perlmodlib, you should start your module's name with an uppercase letter:
One way to call a sub defined in another package is to fully qualify that sub's name when you call it:
If all you want is a reference to
printScreen
, grab it with the backslash operatorand call it with one of
You could create an alias in your current package:
Skip the parentheses (necessary because the sub in the current package wasn't known at compile time) by writing:
The Exporter module can do this custodial work for you:
SettingsGeneral.pm:
main: