Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 (Parson) 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 (Pope) 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 (Monsignor) 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
Node Status?
node history
Node Type: perlquestion [id://1132828]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-01-20 16:22 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.