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

Dear Monks I've struggled over this simple problem, I'd really like not to print any of the output from:
use_ok('Somemodule');
but store it in a file or variable. Of course I can probably find a way to redirect STDOUT. But isn't there something cleaner or simpler? Thank you Hugh B

Replies are listed 'Best First'.
Re: use_ok output
by ikegami (Patriarch) on Sep 15, 2011 at 20:38 UTC
    I'm confused. Are you trying to capture of use_ok('Somemodule') (which doesn't make much sense) or of use Somemodule;?

    If it's the latter, and if you mean "exception" when you say "output", then

    my $output = eval("use Somemodule; 1") ? '' : $@;
      Just to expand on the question. I have an install checker for my application. This needs a series of modules and the checker needs to find out whether they are on the target system. However I'm trying to make it as simple as possible, so I don't want to use extra non-core modules for this, for example or modify modules.

      Thanks for all the replies. Actually, testing the modules via eval and giving up on use_ok looks promising.
Re: use_ok output
by Kc12349 (Monk) on Sep 15, 2011 at 20:58 UTC

    I assume the OP is using Test::More, which contains the use_ok sub. If that is the case take a look at the Extending and Embedding Test::More section of the Test::More perldoc. You can access the underlying object and play around with the implementation as needed. I recall a similar thread here that discussed redirecting output to file, Redirect output of Test::More/Simple. That would be a good starting point.

Re: use_ok output
by luis.roca (Deacon) on Sep 15, 2011 at 22:05 UTC

    To customize the output of your tests to STDOUT take a look at TAP::Harness. With TAP::Harness you can set the verbosity level for your tests individually. *(like suppressing use_ok($module);)

    To capture the output of a test take a look at TAP::Parser. There's a recipe for outputting results to a file on the TestAnything.org site.

    I did some digging in those docs and in Perl Testing: A Developer's Notebook but did not find any specific examples of storing the output of use_ok();. Hopefully you or another kind monk will have better luck.


    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." — Don Quixote