Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Testing Perl files rather than modules

by pamartin (Novice)
on Sep 17, 2008 at 05:19 UTC ( #711871=perlquestion: print w/replies, xml ) Need Help??

pamartin has asked for the wisdom of the Perl Monks concerning the following question:

I have decided that it is time for me to learn test driven development with the aid of Perl modules such as test::more. All the examples I can find involve the testing of complete Perl modules. Accordingly, the test script references the tested code with a use or require statement. My code isn't a module yet; I'm just writing the first subroutines. How do I allow my test script to access the code I am testing? As far as I can tell, Perl doesn't have an include statement like C or PHP, and strongly discourages substitutes. So, what is the preferred method here?

Replies are listed 'Best First'.
Re: Testing Perl files rather than modules
by CountZero (Bishop) on Sep 17, 2008 at 05:52 UTC
    You can include Perl scripts in your code by using do FILE.

    From the docs:

    do FILE

    The do FILE form uses the value of FILE as a filename and executes the contents of the file as a Perl script.

    So, if your script has a number of subroutines, you include these in your test-script and can test them.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Testing Perl files rather than modules
by repellent (Priest) on Sep 17, 2008 at 05:58 UTC
Re: Testing Perl files rather than modules
by Tanktalus (Canon) on Sep 17, 2008 at 13:54 UTC

    When I have this type of quandary, I simplify it. I put all my subs into a module, leaving a shell of a script to call it, and then I can use Test::More. In fact, with the CB stats, most of my cron jobs are as simple as "perl -MMy::Module -e 'My::Module::do_stuff()'" I've put all my real functionality into the modules, and the "programs" that get run are simply calls into the module. Real simple. It's even come in handy in that when I go to actually create the stats, I can ensure that all the data is there just by calling those same routines again, just to be sure. It's like calling system("otherprog"), but faster (because I share the same database handle).

Re: Testing Perl files rather than modules
by Erez (Priest) on Sep 17, 2008 at 07:48 UTC

    Perl doesn't have "include", but it has "use" and "require"

    #!/usr/bin/perl use strict; use warnings; sub hello { return "Hello, World!\n" } 1; #!/usr/bin/perl #test.t use strict; use warnings; use Test::More tests => 2; require_ok(""); #tests 'require "" is(hello(), "Hello, World!\n", 'hello() should return "Hello, World!"'

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

        I too have had a recent epiphany "must get serious about testing". So I bought this book too.</p.

        And I was left with "... but how do I get "there" from "here"??. Because it does tend to start with

        use YourMod::YourStuff;

        ... which is not very handy if you are not in a modularized position to begin with.

        I got a great tip from gabszab which suited my current situation (big monolithic CGI script), which was 'invoke the whole thing from your test and use cmp_ok', but that's another subject (although it might help in our situation?).

        This signature will be ready by Christmas
Re: Testing Perl files rather than modules
by andreas1234567 (Vicar) on Sep 18, 2008 at 07:38 UTC
Re: Testing Perl files rather than modules
by pamartin (Novice) on Sep 18, 2008 at 18:47 UTC
    I want to thank everyone for their responses. The tests are up and running. The posted code was especially helpful.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://711871]
Approved by GrandFather
Front-paged by CountZero
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-03-27 23:11 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (66 votes). Check out past polls.