KingsInnerSoul KingsInnerSoul - 1 month ago 10
Perl Question

Execute a Perl sub routine via the command prompt?

I have a perl (.pm) file with multiple sub routines. I want to execute one sub routine which takes a single parameter as argument. I tried

perl /full_file_path/ mySubRoutine myArgument

but nothing was returned. What is the correct format?

Answer Source

If your Perl module is in one of the @INC list of directories then you can write

perl -Mfile_name -e 'mySubRoutine(myArgument)'

if it is elsewhere then you need to add the path, like

perl -M/full_file_path/file_name -e 'mySubRoutine(myArgument)'

and, as ysth points out, if the module file has a package MyPackage at the start then you may need to add that to your call, like

perl -M/full_file_path/file_name -e 'MyPackage::mySubRoutine(myArgument)'

however in that case the file should be called and the actual command would look something like below (notice that there is no .pm appended to filename when used with -M argument.

perl -M/full_file_path/MyPackage -e 'MyPackage::mySubRoutine(myArgument)'