Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Psh::Strategy::Eval; |
2 | ||
3 | =item * C<eval> | |
4 | ||
5 | All input will be evaluated by the perl interpreter without | |
6 | any conditions. | |
7 | ||
8 | =cut | |
9 | ||
10 | require Psh::Strategy; | |
11 | ||
12 | @Psh::Strategy::Eval::ISA=('Psh::Strategy'); | |
13 | ||
14 | sub new { Psh::Strategy::new(@_) } | |
15 | ||
16 | sub consumes { | |
17 | return Psh::Strategy::CONSUME_TOKENS; | |
18 | } | |
19 | ||
20 | sub applies { | |
21 | return 'perl evaluation'; | |
22 | } | |
23 | ||
24 | sub execute { | |
25 | my $todo= ${$_[1]}; | |
26 | ||
27 | if( $_[4]) { # we are second or later in a pipe | |
28 | my $code; | |
29 | $todo=~ s/\} ?([qg])\s*$/\}/; | |
30 | my $mods= $1 || ''; | |
31 | if( $mods eq 'q' ) { # non-print mode | |
32 | $code='while(<STDIN>) { @_= split /\s+/; '.$todo.' ; }'; | |
33 | } elsif( $mods eq 'g') { # grep mode | |
34 | $code='while(<STDIN>) { @_= split /\s+/; print $_ if eval { '.$todo.' }; } '; | |
35 | } else { | |
36 | $code='while(<STDIN>) { @_= split /\s+/; '.$todo.' ; print $_ if $_; }'; | |
37 | } | |
38 | return (1,sub {return 1,Psh::PerlEval::protected_eval($code,'eval'); }, [], 0, undef); | |
39 | } else { | |
40 | return (1,sub { | |
41 | local @Psh::tmp= Psh::PerlEval::protected_eval($todo,'eval'); | |
42 | return ((@Psh::tmp && $Psh::tmp[0])?1:0, @Psh::tmp); | |
43 | }, [], 0, undef); | |
44 | } | |
45 | } | |
46 | ||
47 | 1; |