Test::Dependencies::Heavy - Heavy style for checking for dependencies.


Test-Dependencies documentation Contained in the Test-Dependencies distribution.

Index


Code Index:

NAME

Top

Test::Dependencies::Heavy - Heavy style for checking for dependencies.

SYNOPSIS

Top

You shouldn't have to include this module yourself. Look at the 'style' option of Test::Dependencies.

This module exports exactly one function.

EXPORTED FUNCTIONS

Top

get_modules_used_in_file

Returns an array ref of all the modules that the passed file uses. This style determines this list by actually compiling the code. This could be a dangerous operation if the file does bad things in BEGIN blocks!

AUTHOR

Top

* Zev Benjamin <zev at cpan.org>

Please report any bugs or feature requests to bug-test-dependencies at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Dependencies. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Test::Dependencies::Heavy

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Test-Dependencies

* CPAN Ratings

http://cpanratings.perl.org/d/Test-Dependencies

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Dependencies

* Search CPAN

http://search.cpan.org/dist/Test-Dependencies

LICENCE AND COPYRIGHT

Top


Test-Dependencies documentation Contained in the Test-Dependencies distribution.
package Test::Dependencies::Heavy;

use warnings;
use strict;

use B::PerlReq;
use IPC::Cmd qw/run/;
use PerlReq::Utils qw(path2mod);
use Exporter 'import';

our @EXPORT = qw/get_modules_used_in_file/;

sub get_modules_used_in_file {
  my $file = shift;
  my $perl = $^X;
  my %deps;

  my $taint = _taint_flag($file);
  my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
    run(command => [$perl, $taint, '-MO=PerlReq', $file]);
  return undef unless $success;

  # for some reason IPC::Run doesn't always split lines correctly
  my @lines;
  push @lines, split /\n/ foreach @$stdout_buf;

  foreach my $line (@lines) {
    chomp $line;
    my $x = $line;
    $line =~ m/^perl\((.+)\)$/;
    # path2mod sucks, but the mod2path that B::PerlReq uses sucks, too
    $deps{path2mod($1)}++;
  }
  return [keys %deps];
}

sub _taint_flag {
  my $filename = shift;
  open FILE, $filename
    or warn "Could not open '$filename': $!";
  my $shebang = <FILE>;
  close FILE;
  if (defined $shebang) {
    chomp $shebang;
    if ($shebang =~ m/^#!.*perl.*-T/) {
      return '-T';
    }
  }
  return '';
}

1;