0

I have a Perl Module that i created and i want to run one of the subroutine in it on a schedule. I know I can just make a small perl script that calls the subroutine and call it from the crontab but if there is a way to call the subroutine right from the crontab that would be cool!

Is this possible?

Bill
  • 1,237
  • 4
  • 21
  • 44

3 Answers3

3

You can use Perl's -e switch for executing code from the command line, e.g.

perl -e 'use your_module; your_function()'

Make that even shorter with the -M switch for loading a module:

perl -Myour_module -e 'your_function()'

The perlrun man page is your friend.

Moritz Bunkus
  • 11,592
  • 3
  • 37
  • 49
2

You can run the subroutine from the command line using something like

perl -MYour::Module=some,functions,to,import,such,as,foo -e 'foo();'

So you will be able to do the same from the crontab. Note that the cron usually runs with a restricted set of environment variables, so you may need to add a -I/path/to/your/modules option.

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
2

If you want a more elegant solution, your module can be configured to detect that it is being run as a script and behave differently in that situation. See this discussion: In Perl, how can I find out if my file is being used as a module or run as a script?

Community
  • 1
  • 1
dan1111
  • 6,576
  • 2
  • 18
  • 29