Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Test::Warn, 'use' vs 'require'

by asarih (Hermit)
on Sep 27, 2006 at 19:50 UTC ( #575235=perlquestion: print w/replies, xml ) Need Help??

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

I want to use Test::Warn in my tests. Since it has many dependencies, I might not have that module on every system that I want to run the tests. So I need to skip the tests that uses this module.

Normally, I would do:

use Test::More qw(no_plan); SKIP: { eval { require Test::Warn }; skip 'Test::Warn not found', 1 if $@; warning_like { warn "warning!" } qr(warning), 'basic warning'; }
But, on a system that does have Test::Warn, this will fail with this diagnostic message:
Can't call method "warning_like" without a package or object reference
If I use Test::Warn instead, the above test works (and of course, fails on systems that don't have Test::Warn).

I read documentation on use and require, but I cannot figure out how to get around this problem. (Since perl is not complaining about the exported symbol "warning_like", I assume that import has been successfully run by the time that line is executed.)

I'd appreciate any help.

Replies are listed 'Best First'.
Re: Test::Warn, 'use' vs 'require'
by chromatic (Archbishop) on Sep 27, 2006 at 21:31 UTC

    You need to load Test::Warn before perl compiles the warning_like() statements, because that function has a prototype. The easiest way may be to set a flag.

    my $have_test_warn = 0; BEGIN { eval { require Test::Warn; Test::Warn->import(); $have_test_warn = 1; }; }

    Then test for $have_test_warn in your SKIP blocks.

        Make that:

        &warning_li­ke( sub { warn "warning!" }, qr(warning­), 'basic warning' +);

        (note the &) so it will work even if the prototype gets loaded soon enough.

        Update: Ah, yes, & prototypes don't have this problem like some other prototypes do.

        - tye        

Re: Test::Warn, 'use' vs 'require'
by Joost (Canon) on Sep 27, 2006 at 19:58 UTC
      The eval block appears to fail even on a system that does have Test::Warn. The content of "$@" is:
      Undefined subroutine &Test::Warn::import called at t/01-test_errors.t line 13.
      
Re: Test::Warn, 'use' vs 'require'
by asarih (Hermit) on Sep 28, 2006 at 02:04 UTC
    Thanks, chromatic and Joost! I ended up doing this:
    my $have_test_warn; BEGIN { eval { require Test::Warn; Test::Warn->import(); $have_test_warn = 1; }; } SKIP: { skip 'Test::Warn not installed', 4 unless $have_test_warn; .... }
    I didn't assign the value to $have_test_warn in the declaration. If I did, it is executed before it gets to the SKIP block, and the value is 0 again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://575235]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-08-17 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?