redisp.pm

filter: Statementstmt 31.8% 50 / 157 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 39.3% 13 / 33 Podpod 0.0% 0 / 5 totaltotal 21.8% 63 / 289 CC 41 SCAR 39.7
CC
41
Cov
21%
_find_referenced
156.0
run
110.0
_setup_ties_for_code
71.4
CRAP 52.9
↑ Summary redisp/Commands.pm »
1 package App::redisp;
2 {
3 1 $App::redisp::VERSION = '0.13';
4 }
5 package # hide
6 main;
7 0 ▶sub eval_ctx { eval "sub { $_[0] }" } # Here to avoid any closures
Subroutine eval_ctx: not called CC=1 SCAR=6.9
8  
9 package App::redisp;
10 # ABSTRACT: Perl redis shell
11  
12 1 ▶use B qw(svref_2object);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dump qw(pp);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Tie::Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use constant HAVE_READKEY => eval { require Term::ReadKey };
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use App::redisp::Commands qw(@COMMANDS);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Eval::WithLexicals;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 has eval_with_lexicals => (
26 is => 'ro',
27 0 ▶ default => sub { Eval::WithLexicals->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 in_package => 'main'
29 );
30 }
31 1 );
32  
33 has host => (
34 is => 'rw',
35 0 ▶ default => sub { 'localhost' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 1 );
37  
38 has port => (
39 is => 'rw',
40 0 ▶ default => sub { 6379 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 1 );
42  
43 has serialize => (
44 is => 'rw',
45 0 ▶ default => sub { '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 1 );
47  
48 has redis => (
49 is => 'rw',
50 lazy => 1,
51 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my($self) = @_;
53 0 tie my %h, 'Tie::Redis',
54 host => $self->host, port => $self->port, use_recv => 1;
55 0 tied %h;
56 }
57 1 );
58  
59 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
60 0 ▶ $ENV{DEBUG} && print "-- ", colored(['green'], @_), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DEBUG"} and print "-- ", colored(["green"], @_), "\n" 0 0 0
61 }
62  
63 # Special handling code
64 my %special = (
65 0 ▶ keyword => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
66 0 my($param) = @_;
67 0 ▶ if($param =~ /^\s*(\w+)\s+([^\%\@\$].*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^\s*(\w+)\s+([^\%\@\$].*)/) 0 0 0
68 0 $param = "redis(q{$1}, $2)";
69 0 debug "Replaced with '$param'\n";
70 }
71 0 return $param;
72 }
73 1 );
74  
75 # Handle these commands specially
76 my %redis_special_commands = (
77 keys => $special{keyword},
78 exists => $special{keyword},
79 1 );
80  
81 my %util_cmds = (
82 0 ▶ encoding => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
83  
84 },
85 1 );
86  
87 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my($self) = @_;
89  
90 0 $self->_install_commands;
91 }
92  
93 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my($class, $verbosity) = @_;
95  
96 0 ▶ pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
$verbosity == 1 ? : 0 0 0
97 -verbose => $verbosity == 1 ? (99, -sections => 'USAGE') : $verbosity,
98 -input => __FILE__
99 );
100 }
101  
102 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
103 0 my($self) = @_;
104  
105 0 ▶ my $short_server = $self->host =~ /[0-9]$/
Branch: 1 branch
BranchTrueFalseTotal
$self->host =~ /[0-9]$/ ? : 0 0 0
106 ? $self->host # IP address
107 : ($self->host =~ /^([^.]+)/)[0];
108  
109 0 my $read = Term::ReadLine->new($short_server);
110 0 my $prompt = "$short_server> ";
111  
112 0 while(1) {
113 0 my $line = $read->readline($prompt);
114 0 ▶ exit unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
115 0 ▶ $read->addhistory($line) if $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\S/ 0 0 0
116  
117 0 ▶ if($line =~ /^(?:\?|help)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^(?:\?|help)$/) { } 0 0 0
elsif ($line =~ /^\.(\w+)(?:\s+(.*))?/) { } 0 0 0
elsif ($line =~ /^\s*(\w+)/ and exists $redis_special_commands{$1}) { } 0 0 0
118 0 print <DATA>;
119 0 next;
120 } elsif($line =~ /^\.(\w+)(?:\s+(.*))?/) {
121 0 ▶ ($util_cmds{$1} || sub { warn "Unknown command\n" })->($2);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$util_cmds{$1} || sub { warn "Unknown command\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$util_cmds{$1} || sub { warn "Unknown command\n"; }
$util_cmds{$1} sub { warn "Unknown command\n"; }
Truth table0%0/3
A || B
A= $util_cmds{$1}B= sub { warn "Unknown command\n"; }
ABresult
1X1
011
000
122 0 next;
123 ▶ } elsif($line =~ /^\s*(\w+)/ && exists $redis_special_commands{$1}) {
Condition0%0/3
$line =~ /^\s*(\w+)/ and exists $redis_special_commands{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^\s*(\w+)/ and exists $redis_special_commands{$1}
$line =~ /^\s*(\w+)/ exists $redis_special_commands{$1}
Truth table0%0/3
A and B
A= $line =~ /^\s*(\w+)/B= exists $redis_special_commands{$1}
ABresult
0X0
100
111
124 0 $line = $redis_special_commands{$1}->($line);
125 }
126  
127 # TODO: Consider Parse::Perl, but I like no-non-core XS deps for now.
128 0 my $code = ::eval_ctx $line;
129 0 ▶ unless(ref $code eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code eq "CODE") 0 0 0
130 0 chomp $@;
131 0 print colored(['red'], $@), "\n";
132 0 next;
133 }
134  
135 0 $self->_setup_ties_for_code($code);
136  
137 0 $self->eval($line);
138 }
139 }
140  
141 0 ▶sub eval {
Subroutine eval: not called CC=4 SCAR=30.0
Pod undocumented
142 0 my($self, $line) = @_;
143  
144 0 ▶ Term::ReadKey::ReadMode(0) if HAVE_READKEY;
Branch: 1 branch
BranchTrueFalseTotal
if &HAVE_READKEY() 0 0 0
145 0 my @ret;
146 eval {
147 0 ▶ local $SIG{INT} = sub { die "Interrupt\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 @ret = $self->eval_with_lexicals->eval($line);
149 0 1;
150 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"INT"} = sub { die "Interrupt\n"; } ; @ret = $self->eval_with_lexicals->eval($line); 1 } } or do { chomp $@; print colored(["red"], $@), "\n"; return } 0 0 0
151 0 chomp $@;
152 0 print colored(['red'], $@), "\n";
153 0 return;
154 };
155 0 ▶ Term::ReadKey::ReadMode(1) if HAVE_READKEY;
Branch: 1 branch
BranchTrueFalseTotal
if &HAVE_READKEY() 0 0 0
156 0 pp @ret;
157 }
158  
159 0 ▶sub _setup_ties_for_code {
Subroutine _setup_ties_for_code: not called CC=9 SCAR=42.7
160 0 my($self, $code) = @_;
161 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 0 for my $var(_find_referenced($code)) {
164 # Avoid special variables
165 0 ▶ next if $var->[0] =~ /^(?:.*::|[\x01-\x1f].*|\W|[0-9]+|_|ENV|SIG)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $var->[0] =~ /^(?:.*::|[\x01-\x1f].*|\W|[0-9]+|_|ENV|SIG)$/ 0 0 0
166  
167 0 ▶ if($var->[1] eq 'sv') {
Branch: 3 branches
BranchTrueFalseTotal
if ($var->[1] eq 'sv') { } 0 0 0
elsif ($var->[1] eq 'hv') { } 0 0 0
elsif ($var->[1] eq 'av') { } 0 0 0
168 0 ▶ next if tied ${"::" . $var->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if tied ${'::' . $var->[0];} 0 0 0
169 0 debug qq{Tie \${\"$var->[0]\"}};
170 0 tie ${"::" . $var->[0]}, 'Tie::Redis::Scalar',
171 redis => $self->redis, key => $var->[0];
172  
173 } elsif($var->[1] eq 'hv') {
174 0 ▶ next if tied %{"::" . $var->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if tied %{'::' . $var->[0];} 0 0 0
175 0 debug qq{Tie \%{\"$var->[0]\"}};
176 0 tie %{"::" . $var->[0]}, 'Tie::Redis::Hash',
177 redis => $self->redis, key => $var->[0];
178  
179 } elsif($var->[1] eq 'av') {
180 0 ▶ next if tied @{"::" . $var->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if tied @{'::' . $var->[0];} 0 0 0
181 0 debug qq{Tie \@{\"$var->[0]\"}};
182 0 tie @{"::" . $var->[0]}, 'Tie::Redis::List',
183 redis => $self->redis, key => $var->[0];
184 }
185 }
186 }
187  
188 0 ▶sub _install_commands {
Subroutine _install_commands: not called CC=3 SCAR=21.1
189 0 my($self) = @_;
190  
191 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
192 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
193  
194 0 for my $cmd(@COMMANDS) {
195 0 ▶ next if exists $redis_special_commands{$cmd};
Branch: 1 branch
BranchTrueFalseTotal
if exists $redis_special_commands{$cmd} 0 0 0
196 0 ▶ *{"main::$cmd"} = sub(@) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
197 0 my @items = $self->redis->{_conn}->$cmd(@_);
198  
199 0 ▶ if(@items == 1 && ref $items[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@items == 1 and ref $items[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
@items == 1 and ref $items[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@items == 1 and ref $items[0] eq "ARRAY"
@items == 1 ref $items[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @items == 1B= ref $items[0] eq "ARRAY"
ABresult
0X0
100
111
200 0 return @{$items[0]};
201 } else {
202 0 return @items;
203 }
204 0 };
205 }
206  
207 0 ▶ *{"main::redis"} = sub(@) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
208 0 my($cmd, @args) = @_;
209 0 my @items = $self->redis->{_conn}->$cmd(@args);
210  
211 0 ▶ if(@items == 1 && ref $items[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@items == 1 and ref $items[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
@items == 1 and ref $items[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
@items == 1 and ref $items[0] eq "ARRAY"
@items == 1 ref $items[0] eq "ARRAY"
Truth table0%0/3
A and B
A= @items == 1B= ref $items[0] eq "ARRAY"
ABresult
0X0
100
111
212 0 return @{$items[0]};
213 } else {
214 0 return @items;
215 }
216 0 };
217 }
218  
219 0 ▶sub _find_referenced {
Subroutine _find_referenced: not called CC=12 SCAR=50.5
220 0 my($code) = @_;
221  
222 # Muahah!
223 0 my @vars;
224 0 my $cv = svref_2object($code);
225 0 my $op = $cv->START;
226 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$op = $op->next and $op->isa("B::OP") and do { do { if ($op->name =~ /^(?:gv|gvsv|aelemfast|const)$/) { my $type = $op->name eq "gvsv" ? "sv" : ($op->name eq "aelemfast" ? "av" : ($op->next->name =~ /2(.*)/)[0]); if ($type) { my $idx = $op->isa("B::SVOP") ? $op->targ : $op->padix; my $sv; if ($op->isa("B::PADOP") or not ${$op->sv;}) { $sv = (($cv->PADLIST->ARRAY)[1]->ARRAY)[$idx]; } else { $sv = $op->sv; } ; my $gv_name = $sv->can("NAME") ? $sv->NAME : $sv->PV; push @vars, [$gv_name, $type] if $gv_name; } ; } } } 0 0 0
Condition0%0/3
$op = $op->next and $op->isa("B::OP")
!ll&&!rl&&r
000
MC/DC0%0/2
$op = $op->next and $op->isa("B::OP")
$op = $op->next $op->isa("B::OP")
Truth table0%0/3
A and B
A= $op = $op->nextB= $op->isa("B::OP")
ABresult
0X0
100
111
227 0 ▶ if($op->name =~ /^(?:gv|gvsv|aelemfast|const)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->name =~ /^(?:gv|gvsv|aelemfast|const)$/) 0 0 0
228 0 ▶ my $type = $op->name eq 'gvsv' ? 'sv' :
Branch: 2 branches
BranchTrueFalseTotal
$op->name eq 'gvsv' ? : 0 0 0
$op->name eq 'aelemfast' ? : 0 0 0
229 $op->name eq 'aelemfast' ? 'av' :
230 ($op->next->name =~ /2(.*)/)[0];
231  
232 # B::Concise::concise_op was helpful here
233 0 ▶ if($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 0 0 0
234 0 ▶ my $idx = $op->isa("B::SVOP") ? $op->targ : $op->padix;
Branch: 1 branch
BranchTrueFalseTotal
$op->isa('B::SVOP') ? : 0 0 0
235  
236 0 my $sv;
237 0 ▶ if($op->isa("B::PADOP") || !${$op->sv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->isa('B::PADOP') or not ${$op->sv;}) { } 0 0 0
Condition0%0/3
$op->isa("B::PADOP") or not ${$op->sv;}
l!l&&r!l&&!r
000
MC/DC0%0/2
$op->isa("B::PADOP") or not ${$op->sv;}
$op->isa("B::PADOP") not ${$op->sv;}
Truth table0%0/3
A or not(B)
A= $op->isa("B::PADOP")B= not ${$op->sv;}
ABresult
1X1
011
000
238 0 $sv = (($cv->PADLIST->ARRAY)[1]->ARRAY)[$idx];
239 } else {
240 0 $sv = $op->sv;
241 }
242 0 ▶ my $gv_name = $sv->can("NAME") ? $sv->NAME : $sv->PV;
Branch: 1 branch
BranchTrueFalseTotal
$sv->can('NAME') ? : 0 0 0
243 0 ▶ push @vars, [$gv_name, $type] if $gv_name;
Branch: 1 branch
BranchTrueFalseTotal
if $gv_name 0 0 0
244 }
245 }
246 } while $op = $op->next and $op->isa("B::OP");
247  
248 0 return @vars;
249 }
250  
251  
252  
253  
254 =pod
255  
256 =head1 NAME
257  
258 App::redisp - Perl redis shell
259  
260 =head1 VERSION
261  
262 version 0.13
263  
264 =head1 SYNOPSIS
265  
266 $ redisp
267 localhost> keys "foo*"
268 "foobar", "food"
269 localhost> set foobarbaz, 12
270 "OK"
271  
272 # Or in perl style
273 localhost> $foobar
274 10
275  
276 # Actually these next ones aren't implemented yet...
277 localhost> .encoding utf-8
278 localhost> .server xxx
279 localhost> .reconnect
280 localhost> .output json
281  
282 =head1 DESCRIPTION
283  
284 Redis and Perl share similar data types, therefore I thought it would be useful
285 to have a Redis shell interface that appears to behave as Perl. This is a Perl
286 Read-Eval-Print Loop (REPL) that happens to understand Redis.
287  
288 The use of Redis aims to be transparent, you just use a variable like C<$foo>
289 and it will be read or saved to Redis. For a temporary variable that is only visible to Perl use C<my $foo>.
290  
291 =for Pod::Coverage eval_with_lexicals host port debug eval BUILD run serialize usage
292  
293 =head1 USAGE
294  
295 redisp [--help] [--server=host] [--port=port] [--encoding=encoding]
296 [--serialize=serializer]
297  
298 =head1 OPTIONS
299  
300 =over 4
301  
302 =item * B<--help>
303  
304 This document.
305  
306 =item * B<--server>
307  
308 Host to connect to Redis on.
309  
310 =item * B<--port>
311  
312 Port to connect to Redis on.
313  
314 =item * B<--encoding>
315  
316 Encoding to use with Redis, B<UTF-8> is recommended (but the default is none).
317  
318 =item * B<--serialize>
319  
320 Serializer to use, see the L<Tie::Redis> documentation for details on supported
321 serializers and the limitations.
322  
323 =back
324  
325 =head1 LIMITATIONS
326  
327 The main noticable thing is common key naming styles in Redis such as
328 C<"foo-bar"> or C<"foo:bar"> require quoting on the Perl side. For example to
329 access a top level key of foo:bar you need to access C<${"foo:bar"}>.
330  
331 In Redis a key has one type; in Perl a glob reference may have HASH, ARRAY,
332 SCALAR, etc values. This application makes Perl match the Redis behaviour, it's
333 invalid to use more than one type at a particular name. The error will be:
334 C<ERR Operation against a key holding the wrong kind of value>.
335  
336 Due to the way this works it's impossible to use symbolic references (e.g.
337 C<${"foo$a"}>), your code needs to reference top level keys it uses at compile
338 time.
339  
340 =head1 EXAMPLES
341  
342 Yet more examples, because the synopsis section was getting sort of big.
343  
344 C<info> is a command that returns a hash, so to grab something like the version
345 you can do this:
346  
347 localhost> info
348 [returns big hash]
349  
350 localhost> info->{redis_version}
351 "2.1.10"
352  
353 Due to some commands clashing with Perl keywords you can't use them as
354 functions. C<Keys> and C<exists> is something notable for this.
355  
356 localhost> keys "foo*" # Special cased
357  
358 localhost> sort keys "foo*" # doesn't work as you'd expect
359  
360 localhost> sort redis qw(keys foo*) # does what you wanted
361  
362 Pub/sub can be used, but you need to write some code yourself:
363 XXX: This doesn't work at all yet!
364  
365 localhost> subscribe foo, sub { print "@_\n" }
366 [prints messages, ^C stops, but you'll need to unsubscribe manually]
367 localhost> unsubscribe foo
368  
369 =head1 BUGS
370  
371 This goes I<quite> close to the internals of Perl so there may be issues with
372 constructs I haven't thought of. Raise bugs via L<http://rt.cpan.org>.
373  
374 The output produced by:
375  
376 ANYEVENT_REDIS_DEBUG=1 DEBUG=1 redisp
377  
378 for your issue would be helpful.
379  
380 =head1 SEE ALSO
381  
382 L<Tie::Redis>, L<http://redis.io/commands>, L<Eval::WithLexicals>,
383 L<Term::ReadLine::Perl> (I recommend you install this or ::Gnu).
384  
385 =head1 AUTHOR
386  
387 David Leadbeater <dgl@dgl.cx>
388  
389 =head1 COPYRIGHT AND LICENSE
390  
391 This software is copyright (c) 2011 by David Leadbeater.
392  
393 This program is free software. It comes without any warranty, to the extent
394 permitted by applicable law. You can redistribute it and/or modify it under the
395 terms of the Beer-ware license revision 42.
396  
397 =cut
398  
399  
400 __DATA__
↑ Summary redisp/Commands.pm »