Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Testing a .pl script

by perl_help26 (Beadle)
on Jul 01, 2015 at 14:26 UTC ( #1132828=perlquestion: print w/replies, xml ) Need Help??

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


I have a .pl script with the following structure:

use X; use Data::Dumper; sub func1 {....} sub func2 {....} until (should_stop()) { ..... .... .. }
I am trying to build a test module for this pl script. Is there a way to export the functions WITHOUT running the while loop? Preferably, I do not want to split the functions into a .pm file. Thanks ...

Replies are listed 'Best First'.
Re: Test automation
by Eily (Monsignor) on Jul 01, 2015 at 14:38 UTC

    You can do something like this:

    sub main { until (should_stop()) { # code goes here } } main unless caller;
    This way the main sub will be called only if the script is called directly and not if it is included in a test through require "".

      Suppose we are running the script from crontab, will that be considered as a caller?

        caller only knows about callers in the same perl process. So do or require will not trigger a main call, while cron, or even system "perl"; will.

Re: Testing a .pl script
by Monk::Thomas (Friar) on Jul 01, 2015 at 14:40 UTC
Re: Test automation
by Corion (Patriarch) on Jul 01, 2015 at 14:29 UTC

    Maybe define/override the function should_stop so it always returns false?

      I like that. So I should put something like this in my
      local *should_stop = sub { return 1; }; require ('');
      Also,suppose i change my code to:
      package X; sub func1 {....} sub func2 {....} until (should_stop()){...}
      Can i export the package into my ???

        You can still override the things:

        package X; use strict; use Exporter 'import'; use vars '@EXPORT_OK'; @EXPORT_OK = qw(func1 func2 ); sub func1 { ... }; sub func2 { ... }; sub should_stop { ... }; until (should_stop()){...}

        And then in your test file:

        local *X::should_stop = sub { return 1 }; require X; X->import('func1');
Re: Testing a .pl script
by Discipulus (Abbot) on Jul 01, 2015 at 20:02 UTC
Re: Testing a .pl script
by Anonymous Monk on Jul 01, 2015 at 17:47 UTC
    'Preferably, I do not want to split the functions into a .pm file.'

    But that is precisely what you should do.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-01-18 14:24 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (53 votes). Check out past polls.