Commit | Line | Data |
---|---|---|
86530b38 AT |
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; |