line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2015 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package App::perlsh; |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
59272
|
use strict; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
48
|
|
9
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
68
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
2
|
|
8
|
use List::Util qw( reduce ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
157
|
|
14
|
2
|
|
|
2
|
|
961
|
use Term::ReadLine; |
|
2
|
|
|
|
|
4432
|
|
|
2
|
|
|
|
|
52
|
|
15
|
2
|
|
|
2
|
|
697
|
use Term::Size; |
|
2
|
|
|
|
|
941
|
|
|
2
|
|
|
|
|
82
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
880
|
use Lexical::Persistence 0.98; |
|
2
|
|
|
|
|
9321
|
|
|
2
|
|
|
|
|
1858
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $COLS = Term::Size::chars \*STDOUT; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$SIG{WINCH} = sub { |
22
|
|
|
|
|
|
|
$COLS = Term::Size::chars \*STDOUT; |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
C - a simple F REPL based on C |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use App::perlsh; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
App::perlsh->run |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This module implements an application that provides a simple perl REPL |
38
|
|
|
|
|
|
|
("read-execute-print loop"); that is, an interactive shell-like program that |
39
|
|
|
|
|
|
|
repeatedly reads perl code from input, executes it, and prints the result in a |
40
|
|
|
|
|
|
|
readable manner. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Being based on L allows it to accumulate variables along |
43
|
|
|
|
|
|
|
the session, letting the user reuse them in later lines. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
$ perlsh |
46
|
|
|
|
|
|
|
eval: my $one = 1; |
47
|
|
|
|
|
|
|
'1' |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
eval: my $two = 2; |
50
|
|
|
|
|
|
|
'2' |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
eval: $one + $two |
53
|
|
|
|
|
|
|
'3' |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
eval: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new |
60
|
|
|
|
|
|
|
{ |
61
|
1
|
|
|
1
|
0
|
2
|
my $class = shift; |
62
|
1
|
|
|
|
|
3
|
return bless {}, $class; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# function |
66
|
0
|
0
|
|
0
|
|
0
|
sub _longest { reduce { length $a > length $b ? $a : $b } @_ } |
|
0
|
|
|
0
|
|
0
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# function |
69
|
|
|
|
|
|
|
sub _repr |
70
|
|
|
|
|
|
|
{ |
71
|
1
|
|
|
1
|
|
3
|
my ( $v, $leader, $leader2 ) = @_; |
72
|
|
|
|
|
|
|
|
73
|
1
|
50
|
|
|
|
4
|
$leader = "" if !defined $leader; |
74
|
1
|
50
|
|
|
|
3
|
$leader2 = $leader if !defined $leader2; |
75
|
|
|
|
|
|
|
|
76
|
1
|
50
|
|
|
|
4
|
if( !defined $v ) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
return "${leader}undef"; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
elsif( !ref $v ) { |
80
|
1
|
50
|
|
|
|
15
|
if( $v =~ m/[^\x20-\x7e]/ ) { |
81
|
0
|
|
|
|
|
0
|
$v =~ s{(["\\])}{\\$1}g; |
82
|
0
|
|
|
|
|
0
|
$v =~ s{\e}{\\e}g; |
83
|
0
|
|
|
|
|
0
|
$v =~ s{\t}{\\t}g; |
84
|
0
|
|
|
|
|
0
|
$v =~ s{\n}{\\n}g; |
85
|
0
|
|
|
|
|
0
|
$v =~ s{\r}{\\r}g; |
86
|
0
|
|
|
|
|
0
|
$v =~ s{([^\x20-\x7e])}{"\\x" . sprintf( "%02x", ord $1 ) }eg; |
|
0
|
|
|
|
|
0
|
|
87
|
0
|
|
|
|
|
0
|
return $leader . qq{"$v"}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
1
|
|
|
|
|
2
|
$v =~ s{(['\\])}{\\$1}g; |
91
|
1
|
|
|
|
|
7
|
return $leader . qq{'$v'}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif( ref $v eq "SCALAR" ) { |
95
|
0
|
|
|
|
|
0
|
my $rv = $$v; |
96
|
0
|
|
|
|
|
0
|
return "\\" . _repr( $rv ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
elsif( ref $v eq "ARRAY" ) { |
99
|
0
|
|
|
|
|
0
|
my @slots = map { _repr( $_ ) } @$v; |
|
0
|
|
|
|
|
0
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# See if we're going to use oneline representation |
102
|
0
|
|
|
|
|
0
|
my $oneline = 1; |
103
|
0
|
|
0
|
|
|
0
|
$_ =~ m/\n/ and $oneline = 0, last foreach @slots; |
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
0
|
if( $oneline ) { |
106
|
0
|
|
|
|
|
0
|
my $ret = $leader . "[ " . join( ", ", @slots ) . " ]"; |
107
|
0
|
0
|
|
|
|
0
|
return $ret if length $ret < $COLS; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
my ( $first, @rest ) = @$v; |
111
|
|
|
|
|
|
|
return $leader . "[ " . |
112
|
|
|
|
|
|
|
join( ",\n$leader2 ", _repr( $first, $leader, "$leader2 " ), |
113
|
0
|
|
|
|
|
0
|
map { _repr( $_, $leader2, "$leader2 " ) } @rest ) |
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
. " ]"; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
elsif( ref $v eq "HASH" ) { |
117
|
0
|
|
|
|
|
0
|
my @keys = sort keys %$v; |
118
|
0
|
|
|
|
|
0
|
my @values = map { _repr( $v->{$_} ) } @keys; |
|
0
|
|
|
|
|
0
|
|
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
0
|
my $oneline = 1; |
121
|
0
|
|
0
|
|
|
0
|
$_ =~ m/\n/ and $oneline = 0, last foreach @values; |
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
0
|
if( $oneline ) { |
124
|
0
|
|
|
|
|
0
|
my $ret = "{ " . join( ", ", map { "$keys[$_] => $values[$_]" } ( 0 .. $#keys ) ) . " }"; |
|
0
|
|
|
|
|
0
|
|
125
|
0
|
0
|
|
|
|
0
|
return $ret if length $ret < $COLS; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
0
|
my $keylen = length _longest @keys; |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
my $firstkey = shift @keys; |
131
|
|
|
|
|
|
|
return $leader . "{ " . |
132
|
|
|
|
|
|
|
join( ",\n$leader2 ", sprintf( '%-*s => %s', $keylen, $firstkey, _repr( $v->{$firstkey} ) ), |
133
|
0
|
|
|
|
|
0
|
map { sprintf( '%-*s => %s', $keylen, $_, _repr( $v->{$_} ) ) } @keys ) |
|
0
|
|
|
|
|
0
|
|
134
|
|
|
|
|
|
|
. " }"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
0
|
|
|
|
|
0
|
return "(Cannot represent ref ".(ref $v).")"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub read |
142
|
|
|
|
|
|
|
{ |
143
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
0
|
|
|
0
|
my $term = $self->{term} ||= Term::ReadLine->new("perlsh"); |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
0
|
return $term->readline( "eval: " ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub print |
151
|
|
|
|
|
|
|
{ |
152
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
153
|
0
|
|
|
|
|
0
|
print @_; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub run |
157
|
|
|
|
|
|
|
{ |
158
|
1
|
50
|
|
1
|
0
|
85
|
my $self = ref $_[0] ? shift : shift->new; |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
|
|
18
|
my $lp = Lexical::Persistence->new(); |
161
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
32
|
while ( defined( my $line = $self->read ) ) { |
163
|
|
|
|
|
|
|
# Append a linefeed in case of trailing comment |
164
|
1
|
|
|
|
|
9
|
my $sub = $lp->compile( "$line\n" ); |
165
|
1
|
50
|
|
|
|
72
|
if( !defined $sub ) { |
166
|
0
|
|
|
|
|
0
|
$self->print( "Compile error: $@\n" ); |
167
|
0
|
|
|
|
|
0
|
next; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
1
|
|
|
|
|
2
|
my $result = eval { $lp->call( $sub ) }; |
|
1
|
|
|
|
|
4
|
|
171
|
|
|
|
|
|
|
|
172
|
1
|
50
|
|
|
|
60
|
if( $@ ) { |
173
|
0
|
|
|
|
|
0
|
$self->print( "ERR: $@\n" ); |
174
|
0
|
|
|
|
|
0
|
next; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
1
|
|
|
|
|
3
|
$self->print( _repr( $result ) . "\n\n" ); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
1
|
|
|
|
|
14
|
$self->print( "\n" ); # Final linefeed |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 AUTHOR |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Paul Evans |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
0x55AA; |