<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Test::Needs;
use strict;
use warnings;
no warnings 'once';
our $VERSION = '0.002010';
$VERSION =~ tr/_//d;

BEGIN {
  *_WORK_AROUND_HINT_LEAKAGE
    = "$]" &lt; 5.011 &amp;&amp; !("$]" &gt;= 5.009004 &amp;&amp; "$]" &lt; 5.010001)
    ? sub(){1} : sub(){0};
  *_WORK_AROUND_BROKEN_MODULE_STATE
    = "$]" &lt; 5.009
    ? sub(){1} : sub(){0};

  # this allows regexes to match wide characters in vstrings
  if ("$]" &gt;= 5.006001 &amp;&amp; "$]" &lt;= 5.006002) {
    require utf8;
    utf8-&gt;import;
  }
}

our @EXPORT = qw(test_needs);

our $Level = 0;

sub _try_require {
  local %^H
    if _WORK_AROUND_HINT_LEAKAGE;
  my ($module) = @_;
  (my $file = "$module.pm") =~ s{::|'}{/}g;
  my $err;
  {
    local $@;
    eval { require $file }
      or $err = $@;
  }
  if (defined $err) {
    delete $INC{$file}
      if _WORK_AROUND_BROKEN_MODULE_STATE;
    die $err
      unless $err =~ /\ACan't locate \Q$file\E/;
    return !1;
  }
  !0;
}

sub _croak {
  my $message = join '', @_;
  my $i = 1;
  while (my ($p, $f, $l) = caller($i++)) {
    next
      if $p =~ /\ATest::Needs(?:::|\z)/;
    die "$message at $f line $l.\n";
  }
  die $message;
}

sub _try_version {
  my ($module, $version) = @_;
  local $@;
  !!eval { $module-&gt;VERSION($version); 1 };
}

sub _numify_version {
  for ($_[0]) {
    return
        !$_ ? 0
      : /^[0-9]+(?:\.[0-9]+)?$/ ? sprintf('%.6f', $_)
      : /^v?([0-9]+(?:\.[0-9]+)*)$/
        ? sprintf('%d.%03d%03d', ((split /\./, $1), 0, 0)[0..2])
      : /^([\x05-\x07])(.*)$/s
        ? sprintf('%d.%03d%03d', ((map ord, /(.)/gs), 0, 0)[0..2])
      : _croak qq{version "$_" does not look like a number};
  }
}

sub _find_missing {
  my @bad = map {
    my ($module, $version) = @$_;
    $module eq 'perl' ? do {
      $version = _numify_version($version);
      "$]" &lt; $version ? (sprintf "perl %s (have %.6f)", $version, $]) : ()
    }
    : $module =~ /^\d|[^\w:]|:::|[^:]:[^:]|^:|:$/
      ? _croak sprintf qq{"%s" does not look like a module name}, $module
    : _try_require($module) ? (
      defined $version &amp;&amp; !_try_version($module, $version)
        ? "$module $version (have ".(defined $module-&gt;VERSION ? $module-&gt;VERSION : 'undef').')'
        : ()
    )
    : $version ? "$module $version"
    : $module;
  }
  _pairs(@_);
  @bad ? "Need " . join(', ', @bad) : undef;
}

sub import {
  my $class = shift;
  my $target = caller;
  if (@_) {
    local $Level = $Level + 1;
    test_needs(@_);
  }
  no strict 'refs';
  *{"${target}::$_"} = \&amp;{"${class}::$_"}
    for @{"${class}::EXPORT"};
}

sub test_needs {
  my $missing = _find_missing(@_);
  local $Level = $Level + 1;
  if ($missing) {
    if ($ENV{RELEASE_TESTING}) {
      _fail("$missing due to RELEASE_TESTING");
    }
    else {
      _skip($missing);
    }
  }
  return 1;
}

sub _skip {
  local $Level = $Level + 1;
  _fail_or_skip($_[0], 0)
}
sub _fail {
  local $Level = $Level + 1;
  _fail_or_skip($_[0], 1)
}

sub _pairs {
  map +(
    ref eq 'HASH' ? do {
      my $arg = $_;
      map [ $_ =&gt; $arg-&gt;{$_} ], sort keys %$arg;
    }
    : ref eq 'ARRAY' ? do {
      my $arg = $_;
      map [ @{$arg}[$_*2,$_*2+1] ], 0 .. int($#$arg / 2);
    }
    : [ $_ ]
  ), @_;
}

sub _fail_or_skip {
  my ($message, $fail) = @_;
  if ($INC{'Test2/API.pm'}) {
    my $ctx = Test2::API::context(level =&gt; $Level);
    my $hub = $ctx-&gt;hub;
    if ($fail) {
      $ctx-&gt;ok(0, "Test::Needs modules available", [$message]);
    }
    else {
      my $plan = $hub-&gt;plan;
      my $tests = $hub-&gt;count;
      if ($plan || $tests) {
        my $skips
          = $plan &amp;&amp; $plan ne 'NO PLAN' ? $plan - $tests : 1;
        $ctx-&gt;skip("Test::Needs modules not available") for 1 .. $skips;
        $ctx-&gt;note($message);
      }
      else {
        $ctx-&gt;plan(0, 'SKIP', $message);
      }
    }
    $ctx-&gt;done_testing;
    $ctx-&gt;release if $Test2::API::VERSION &lt; 1.302053;
    $ctx-&gt;send_event('+'._t2_terminate_event());
  }
  elsif ($INC{'Test/Builder.pm'}) {
    local $Test::Builder::Level = $Test::Builder::Level + $Level;
    my $tb = Test::Builder-&gt;new;
    my $has_plan = Test::Builder-&gt;can('has_plan') ? 'has_plan'
      : sub { $_[0]-&gt;expected_tests || eval { $_[0]-&gt;current_test($_[0]-&gt;current_test); 'no_plan' } };
    my $tests = $tb-&gt;current_test;
    if ($fail) {
      $tb-&gt;plan(tests =&gt; 1)
        unless $tb-&gt;$has_plan;
      $tests++;
      $tb-&gt;ok(0, "Test::Needs modules available");
      $tb-&gt;diag($message);
    }
    else {
      my $plan = $tb-&gt;$has_plan;
      if ($plan || $tests) {
        my $skips
          = $plan &amp;&amp; $plan ne 'no_plan' ? $plan - $tests : 1;
        $tb-&gt;skip("Test::Needs modules not available")
          for 1 .. $skips;
        $tests += $skips;
        Test::Builder-&gt;can('note') ? $tb-&gt;note($message) : print "# $message\n";
      }
      else {
        $tb-&gt;skip_all($message);
      }
    }
    $tb-&gt;done_testing($tests)
      if Test::Builder-&gt;can('done_testing');
    die bless {} =&gt; 'Test::Builder::Exception'
      if Test::Builder-&gt;can('parent') &amp;&amp; $tb-&gt;parent;
  }
  else {
    if ($fail) {
      print "1..1\n";
      print "not ok 1 - Test::Needs modules available\n";
      print STDERR "# $message\n";
      exit 1;
    }
    else {
      print "1..0 # SKIP $message\n";
    }
  }
  exit 0;
}

my $terminate_event;
sub _t2_terminate_event () {
  return $terminate_event
    if $terminate_event;
  local $@;
  $terminate_event = eval sprintf &lt;&lt;'END_CODE', __LINE__+2, __FILE__ or die "$@";
#line %d "%s"
    package # hide
      Test::Needs::Event::Terminate;
    use Test2::Event ();
    our @ISA = qw(Test2::Event);
    sub no_display { 1 }
    sub terminate { 0 }
    __PACKAGE__;
END_CODE
    (my $pm = "$terminate_event.pm") =~ s{::}{/}g;
    $INC{$pm} = __FILE__;
    $terminate_event;
}

1;
__END__

=pod

=encoding utf-8

=head1 NAME

Test::Needs - Skip tests when modules not available

=head1 SYNOPSIS

  # need one module
  use Test::Needs 'Some::Module';

  # need multiple modules
  use Test::Needs 'Some::Module', 'Some::Other::Module';

  # need a given version of a module
  use Test::Needs {
    'Some::Module' =&gt; '1.005',
  };

  # check later
  use Test::Needs;
  test_needs 'Some::Module';

  # skips remainder of subtest
  use Test::More;
  use Test::Needs;
  subtest 'my subtest' =&gt; sub {
    test_needs 'Some::Module';
    ...
  };

  # check perl version
  use Test::Needs { perl =&gt; 5.020 };

=head1 DESCRIPTION

Skip test scripts if modules are not available.  The requested modules will be
loaded, and optionally have their versions checked.  If the module is missing,
the test script will be skipped.  Modules that are found but fail to compile
will exit with an error rather than skip.

If used in a subtest, the remainder of the subtest will be skipped.

Skipping will work even if some tests have already been run, or if a plan has
been declared.

Versions are checked via a C&lt;&lt; $module-&gt;VERSION($wanted_version) &gt;&gt; call.
Versions must be provided in a format that will be accepted.  No extra
processing is done on them.

If C&lt;perl&gt; is used as a module, the version is checked against the running perl
version (L&lt;$]|perlvar/$]&gt;).  The version can be specified as a number,
dotted-decimal string, v-string, or version object.

If the C&lt;RELEASE_TESTING&gt; environment variable is set, the tests will fail
rather than skip.  Subtests will be aborted, but the test script will continue
running after that point.

=head1 EXPORTS

=head2 test_needs

Has the same interface as when using Test::Needs in a C&lt;use&gt;.

=head1 SEE ALSO

=over 4

=item L&lt;Test::Requires&gt;

A similar module, with some important differences.  L&lt;Test::Requires&gt; will act
as a C&lt;use&gt; statement (despite its name), calling the import sub.  Under
C&lt;RELEASE_TESTING&gt;, it will BAIL_OUT if a module fails to load rather than
using a normal test fail.  It also doesn't distinguish between missing modules
and broken modules.

=item L&lt;Test2::Require::Module&gt;

Part of the L&lt;Test2&gt; ecosystem.  Only supports running as a C&lt;use&gt; command to
skip an entire plan.

=item L&lt;Test2::Require::Perl&gt;

Part of the L&lt;Test2&gt; ecosystem.  Only supports running as a C&lt;use&gt; command to
skip an entire plan.  Checks perl versions.

=item L&lt;Test::If&gt;

Acts as a C&lt;use&gt; statement.  Only supports running as a C&lt;use&gt; command to skip
an entire plan.  Can skip based on subref results.

=back

=head1 AUTHORS

haarg - Graham Knop (cpan:HAARG) &lt;haarg@haarg.org&gt;

=head1 CONTRIBUTORS

None so far.

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2016 the Test::Needs L&lt;/AUTHORS&gt; and L&lt;/CONTRIBUTORS&gt;
as listed above.

This library is free software and may be distributed under the same terms
as perl itself. See L&lt;http://dev.perl.org/licenses/&gt;.

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