How can I test a standalone Perl script?

前端 未结 7 1515
太阳男子
太阳男子 2021-02-01 05:07

I have written a small Perl script and now I would like to create a test suite for it. I thought it would be nice to be able to use the script as a module, import t

7条回答
  •  时光说笑
    2021-02-01 05:34

    Since I just wanted to test one sub, I used the hacky solution of extracting that sub into a module with a perl one-liner, which is easy to understand and doesn't require to install any modules (I extract the paragraph with the sub name which is of course brittle). So in my Makefile I have:

    MyModule.pm: my_script.pl
        perl -00 -lnE 'if(/^\s*sub my_sub \{/){say "package MyModule;";say;say "1;";}' my_script.pl >|MyModule.pm
    

提交回复
热议问题