Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test a standalone Perl script?

Tags:

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 the subs defined in the script and test these. Is there a way to have the script both standalone Perl script and Perl module? (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.)

Or is there a better way to test the script?

like image 890
zoul Avatar asked Jan 22 '09 10:01

zoul


People also ask

How do I know if Perl script is working?

Then all you have to do is visit the file with your browser, eg. http://www.yoursite.com/cgi-bin/test-perl.pl. print "Content-Type: text/htmlnn"; print "Perl is working!

How do I write a test case in Perl?

Write Test Cases with Test::Simple #!/usr/bin/perl use strict; use warnings; use Test::Simple tests => 2; sub hello_world { return "Hello world!"; } sub get_number { return int(rand(1000)); } ok( hello_world( ) eq "Hello world!", "My Testcase 1" ); ok( get_number( ) > 0, "My Testcase 2" );


1 Answers

Since brian seems to be asleep right now, here's a pointer to what he calls modulinos. This is basically a recipe for building scripts that act like modules or modules that can be run like scripts. Sounds exactly like what you are looking for.

Mastering Perl is definitely a book worth reading (and buying).

like image 74
innaM Avatar answered Dec 23 '22 19:12

innaM