<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test::Requires;
use strict;
use warnings;
our $VERSION = '0.11';
use base 'Test::Builder::Module';
use 5.006;

sub import {
    my $class = shift;
    my $caller = caller(0);

    # export methods
    {
        no strict 'refs';
        *{"$caller\::test_requires"} = \&amp;test_requires;
    }

    # test arguments
    if (@_ == 1 &amp;&amp; ref $_[0] &amp;&amp; ref $_[0] eq 'HASH') {
        while (my ($mod, $ver) = each %{$_[0]}) {
            test_requires($mod, $ver, $caller);
        }
    } else {
        for my $mod (@_) {
            test_requires($mod, undef, $caller);
        }
    }
}

sub test_requires {
    my ( $mod, $ver, $caller ) = @_;
    return if $mod eq __PACKAGE__;
    if (@_ != 3) {
        $caller = caller(0);
    }
    $ver ||= '';

    eval qq{package $caller; use $mod $ver}; ## no critic.
    if (my $e = $@) {
        my $skip_all = sub {
            my $builder = __PACKAGE__-&gt;builder;

            if (not defined $builder-&gt;has_plan) {
                $builder-&gt;skip_all(@_);
            } elsif ($builder-&gt;has_plan eq 'no_plan') {
                $builder-&gt;skip(@_);
                if ( $builder-&gt;can('parent') &amp;&amp; $builder-&gt;parent ) {
                    die bless {} =&gt; 'Test::Builder::Exception';
                }
                exit 0;
            } else {
                for (1..$builder-&gt;has_plan) {
                    $builder-&gt;skip(@_);
                }
                if ( $builder-&gt;can('parent') &amp;&amp; $builder-&gt;parent ) {
                    die bless {} =&gt; 'Test::Builder::Exception';
                }
                exit 0;
            }
        };
        
        my $msg = "$e";
        if ( $e =~ /^Can't locate/ ) {
            $msg = "Test requires module '$mod' but it's not found";
        }
        
        if ($ENV{RELEASE_TESTING}) {
            __PACKAGE__-&gt;builder-&gt;BAIL_OUT($msg);
        }
        else {
            $skip_all-&gt;($msg);
        }
    }
}

1;
__END__

=head1 NAME

Test::Requires - Checks to see if the module can be loaded

=head1 SYNOPSIS

    # in your Makefile.PL
    use inc::Module::Install;
    test_requires 'Test::Requires';

    # in your test
    use Test::More tests =&gt; 10;
    use Test::Requires {
        'HTTP::MobileAttribute' =&gt; 0.01, # skip all if HTTP::MobileAttribute doesn't installed
    };
    isa_ok HTTP::MobileAttribute-&gt;new, 'HTTP::MobileAttribute::NonMobile';

    # or
    use Test::More tests =&gt; 10;
    use Test::Requires qw( 
        HTTP::MobileAttribute
    );
    isa_ok HTTP::MobileAttribute-&gt;new, 'HTTP::MobileAttribute::NonMobile';

    # or
    use Test::More tests =&gt; 10;
    use Test::Requires;
    test_requires 'Some::Optional::Test::Required::Modules';
    isa_ok HTTP::MobileAttribute-&gt;new, 'HTTP::MobileAttribute::NonMobile';

=head1 DESCRIPTION

Test::Requires checks to see if the module can be loaded.

If this fails rather than failing tests this B&lt;skips all tests&gt;.

Test::Requires can also be used to require a minimum version of Perl:

    use Test::Requires "5.010";  # quoting is necessary!!
    
    # or
    use Test::Requires "v5.10";

=head1 AUTHOR

Tokuhiro Matsuno E&lt;lt&gt;tokuhirom @*(#RJKLFHFSDLJF gmail.comE&lt;gt&gt;

=head1 THANKS TO

    kazuho++ # some tricky stuff
    miyagawa++ # original code from t/TestPlagger.pm
    tomyhero++ # reported issue related older test::builder
    tobyink++ # documented that Test::Requires "5.010" works

=head1 ENVIRONMENT

If the C&lt;&lt; RELEASE_TESTING &gt;&gt; environment variable is true, then instead
of skipping tests, Test::Requires bails out.

=head1 SEE ALSO

L&lt;t/TestPlagger.pm&gt;

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
</pre></body></html>