Psh/Strategy/Perlfunc.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 82 CC 9 SCAR 40.7
CC
9
Cov
0%
applies
90.0
new
2.0
consumes
2.0
CRAP 58.6
1 package Psh::Strategy::Perlfunc;
2  
3 =item * C<perlfunc>
4  
5 Tries to detect perl builtins - this is helpful if you e.g. have
6 a print command on your system. This is a small, minimal version
7 without options which will react on your own sub's or on a limited
8 list of important perl builtins. Please also see the strategy
9 perlfunc_heavy
10  
11 =cut
12  
13 require Psh::Strategy;
14  
15 @Psh::Strategy::Perlfunc::ISA=('Psh::Strategy');
16  
17 sub new { Psh::Strategy::new(@_) }
18  
19 sub consumes {
20 return Psh::Strategy::CONSUME_TOKENS;
21 }
22  
23 sub runs_before {
24 return qw(perlscript auto_resume executable);
25 }
26  
27 my %perl_builtins = qw(
28 print 1 printf 1 push 1 pop 1 shift 1 unshift 1 system 1
29 package 1
30 chop 1 chomp 1 use 1 for 1 foreach 1 sub 1 do 1
31 );
32  
33 sub applies {
34 my @words= @{$_[2]};
35 my $line= ${$_[1]};
36  
37 my $fnname = $words[0];
38 my $parenthesized = 0;
39  
40 # catch "join(':',@foo)" here as well:
41 if ($fnname =~ m/\(/) {
42 $parenthesized = 1;
43 $fnname = (split('\(', $fnname))[0];
44 }
45  
46 my $qPerlFunc = 0;
47 if (exists $perl_builtins{$fnname}) {
48 my $needArgs = $perl_builtins{$fnname};
49 if ($needArgs > 0
50 and ($parenthesized
51 or scalar(@{$_[2]}) >= $needArgs)) {
52 $qPerlFunc = 1;
53 }
54 } elsif( $fnname =~ /^([a-zA-Z0-9_]+)\:\:([a-zA-Z0-9_:]+)$/) {
55 if( $1 eq 'CORE') {
56 my $needArgs = $perl_builtins{$2};
57 if ($needArgs > 0
58 and ($parenthesized or scalar(@{$_[2]}) >= $needArgs)) {
59 $qPerlFunc = 1;
60 }
61 } else {
62 $qPerlFunc = (Psh::PerlEval::protected_eval("defined(&{'$fnname'})"))[0];
63 }
64 } elsif( $fnname =~ /^[a-zA-Z0-9_]+$/) {
65 $qPerlFunc = (Psh::PerlEval::protected_eval("defined(&{'$fnname'})"))[0];
66 }
67  
68 return $line if $qPerlFunc;
69 return '';
70 }
71  
72 sub execute {
73 my @args= @_;
74 $args[4]=undef;
75 return Psh::Strategy::Eval::execute(@args);
76 }
77  
78 1;