How can I test a standalone Perl script?

前端 未结 7 1412
太阳男子
太阳男子 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:52

    (I do not want to split the script into a separate module and the “executable”, since I plan to distribute the script as a single file.)

    Most people stitch files together like this at build time. App::Ack on the CPAN is an example of something that can be built like this.

    If you really want to test your application properly, you need to put the functionality in a module, and then write a Test::More-based test script that exercises the functions that the module provides. Then the actual script is just a thin wrapper around the module, usually something like:

    #!/usr/bin/env perl
    use Your::Class;
    Your::Class->new(args => \@ARGV)->run;
    

    See also: MooseX::Getopt.

提交回复
热议问题