File Coverage

blib/lib/ExtUtils/Builder/Action/Function.pm
Criterion Covered Total %
statement 52 58 89.6
branch 10 14 71.4
condition 6 13 46.1
subroutine 11 14 78.5
pod 7 8 87.5
total 86 107 80.3


line stmt bran cond sub pod time code
1             package ExtUtils::Builder::Action::Function;
2             $ExtUtils::Builder::Action::Function::VERSION = '0.020';
3 2     2   375695 use strict;
  2         5  
  2         95  
4 2     2   10 use warnings;
  2         4  
  2         162  
5              
6 2     2   13 use Carp 'croak';
  2         4  
  2         147  
7 2     2   1099 use ExtUtils::Builder::Util 'get_perl';
  2         6  
  2         174  
8              
9 2     2   511 use parent 'ExtUtils::Builder::Action::Perl';
  2         386  
  2         19  
10              
11             sub new {
12 1     1 0 277702 my ($class, %args) = @_;
13 1 50       29 croak 'Attribute module is not defined' if not defined $args{module};
14 1 50       7 croak 'Attribute function is not defined' if not defined $args{function};
15 1         7 $args{fullname} = join '::', $args{module}, $args{function};
16 1   50     5 $args{exports} ||= !!0;
17 1   50     4 $args{arguments} //= [];
18 1         19 my $self = $class->SUPER::new(%args);
19 1         5 return $self;
20             }
21              
22             sub modules {
23 0     0 1 0 my ($self) = @_;
24 0         0 return $self->{module};
25             }
26              
27             sub module {
28 0     0 1 0 my ($self) = @_;
29 0         0 return $self->{module};
30             }
31              
32             sub function {
33 0     0 1 0 my ($self) = @_;
34 0         0 return $self->{function};
35             }
36              
37             sub arguments {
38 6     6 1 12 my ($self) = @_;
39 6         12 return @{ $self->{arguments} };
  6         28  
40             }
41              
42             sub execute {
43 2     2 1 1977 my ($self, %args) = @_;
44 2         11 my $module = $self->{module};
45 2         8 (my $filename = $module) =~ s{::}{/}g;
46 2         598 require "$filename.pm";
47              
48 2 100       326 if (!$args{quiet}) {
49 1   33     7 my $message = $self->{message} // sprintf "%s(%s)", $self->{fullname}, join ", ", $self->arguments;
50 1         5 print "$message\n";
51             }
52              
53 2     2   858 my $code = do { no strict 'refs'; \&{ $self->{fullname} } };
  2         4  
  2         1001  
  2         5  
  2         4  
  2         10  
54 2         8 $code->($self->arguments);
55             }
56              
57             sub to_code {
58 2     2 1 2276 my ($self, %args) = @_;
59 2   66     15 my $shortcut = $args{skip_loading} && $args{skip_loading} eq 'main' && $self->{exports};
60 2 100       9 my $name = $shortcut ? $self->{function} : $self->{fullname};
61 2 100       9 my @modules = $args{skip_loading} ? () : "require $self->{module}";
62 2 50       6 my $arguments = $self->arguments ? do {
63 2         744 require Data::Dumper; (Data::Dumper->new([ [ $self->arguments ] ])->Terse(1)->Indent(0)->Dump =~ /^ \[ (.*) \] $/x)[0]
  2         10138  
64             } : '';
65 2         192 return join '; ', @modules, sprintf '%s(%s)', $name, $arguments;
66             }
67              
68             sub to_command {
69 1     1 1 2149 my ($self, %opts) = @_;
70 1 50       9 my $module = $self->{exports} eq 'explicit' ? "-M$self->{module}=$self->{function}" : "-M$self->{module}";
71 1   33     10 my $perl = $opts{perl} // get_perl(%opts);
72 1         6 return [ $perl, $module, '-e', $self->to_code(skip_loading => 'main') ];
73             }
74              
75             1;
76              
77             #ABSTRACT: Actions for perl function calls
78              
79             __END__