File Coverage

blib/lib/Devel/Command.pm
Criterion Covered Total %
statement 47 57 82.4
branch 5 8 62.5
condition n/a
subroutine 9 12 75.0
pod 3 4 75.0
total 64 81 79.0


line stmt bran cond sub pod time code
1             package Devel::Command;
2 1     1   10523 use strict;
  1         2  
  1         33  
3 1     1   6 use warnings;
  1         1  
  1         30  
4 1     1   2944 use Data::Dumper;
  1         20170  
  1         79  
5              
6 1     1   944 use Module::Pluggable search_path=>["Devel::Command"], require=>1;
  1         11207  
  1         8  
7 1         5 use Module::Pluggable search_path=>['Devel::Command::DBSub'],
8 1     1   79 sub_name => 'DB_subs';
  1         3  
9              
10             our $VERSION = '0.11';
11              
12             sub import {
13             # Find and install all the plugins.
14             # Uncomment the following line to verify plugin/patch loading in the debugger.
15             # $DB::single=1;
16 1     1   11 my @plugins = __PACKAGE__->plugins;
17 1         4241 foreach my $plugin (@plugins) {
18             # Skip patch plugins.
19 5 100       20 next if $plugin =~ /^Devel::Command::DBSub/;
20              
21             # get the signature(s) (name, entry point).
22 1         12 my(@signatures) = $plugin->signature();
23              
24             # Install the command(s) in our lookup table.
25 1         5 while (@signatures) {
26 1         3 my $cmd_name = shift @signatures;
27 1         2 my $cmd_ref = shift @signatures;
28 1         5 $DB::commands{$cmd_name} = $cmd_ref;
29             }
30              
31             # Export our eval into the plugin.
32             {
33 1     1   176 no strict 'refs';
  1         2  
  1         66  
  1         3  
34 1         3 *{$plugin."::eval"} = \&eval;
  1         8  
35             }
36             }
37              
38             # Add our local 'cmds' command to the table.
39 1         4 $DB::commands{"cmds"} = \&cmds;
40              
41             # Install the alternate version of DB::DB.
42             {
43 1     1   4 no warnings 'redefine';
  1         2  
  1         413  
  1         2  
44 1         2 my $patch;
45 1         8 print STDERR map {"# $_"} Dumper(__PACKAGE__->DB_subs());
  4         4644  
46 1         15 foreach my $DB_module (__PACKAGE__->DB_subs) {
47 4         4164 my $subref;
48 4         12 warn "# Trying " .Dumper($DB_module);
49 4 100       306 if ($subref = $DB_module->import()) {
50             # This module could work for the current Perl.
51 2         8 $patch = [$subref, $DB_module];
52             }
53             }
54 1 50       4 if (! defined $patch) {
55 0         0 die "Your Perl can't be patched by Devel::Command (yours is Perl $])\n";
56             }
57             else {
58 1         24 print "Patching with ", $patch->[1], "\n";
59 1         1645 *DB::DB = $patch->[0];
60             }
61             }
62             }
63              
64             sub cmds {
65 0     0 1 0 for my $key (keys %DB::commands) {
66 0         0 print DB::OUT $key,"\n";
67             }
68 0         0 1;
69             }
70              
71             sub DB::afterinit {
72 0     0 0 0 my @plugins = __PACKAGE__->plugins;
73 0         0 foreach my $plugin (@plugins) {
74 0 0       0 $plugin->afterinit if $plugin->can('afterinit');
75             }
76             }
77              
78             sub eval {
79 0     0 1 0 my $arg = shift;
80 0         0 $DB::evalarg = $arg;
81 0         0 DB::eval();
82             }
83              
84             sub signature {
85 1     1 1 2 my $class = shift;
86             # Generate a command name based on the name
87             # of this plugin (the final qualifier),
88             # lowercased. Assumes that the actual
89             # code to execute the command is in a
90             # sub named 'command' in that package.
91 1         60 (lc(substr($class,rindex($class,'::')+2)),
92             eval "\\&".$class."::command");
93             }
94              
95             1;
96             __END__