| 1 | package Devel::REPL::Plugin::Carp::REPL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.18'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Devel::REPL::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Devel::LexAlias; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Devel::StackTrace::WithLexicals; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Data::Dump::Streamer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | sub BEFORE_PLUGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEFORE_PLUGIN: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 13 | 2 | $self->load_plugin('LexEnv'); | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has stacktrace => ( | ||||||||||||||||||||||||||||
| 17 | is => 'ro', | ||||||||||||||||||||||||||||
| 18 | isa => 'Devel::StackTrace::WithLexicals', | ||||||||||||||||||||||||||||
| 19 | handles => [qw/frame_count/], | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=22.0
| |||||||||||||||||||||||||||||
| 21 | 2 | my $stacktrace = Devel::StackTrace::WithLexicals->new( | |||||||||||||||||||||||||||
| 22 | ignore_class => ['Carp::REPL', __PACKAGE__], | ||||||||||||||||||||||||||||
| 23 | unsafe_ref_capture => 1, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # skip all the Moose metaclass frames | ||||||||||||||||||||||||||||
| 27 | 34 | shift @{ $stacktrace->{raw} } | |||||||||||||||||||||||||||
| 28 | 36 | until @{ $stacktrace->{raw} } == 0 | |||||||||||||||||||||||||||
| 29 | 2 | ▶ | || $stacktrace->{raw}[0]{caller}[3] eq 'Carp::REPL::repl'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$stacktrace->{"raw"};} == 0 or $stacktrace->{"raw"}[0]{"caller"}[3] eq "Carp::REPL::repl"
MC/DC50%1/2
@{$stacktrace->{"raw"};} == 0 or $stacktrace->{"raw"}[0]{"caller"}[3] eq "Carp::REPL::repl"
@{$stacktrace->{"raw"};} == 0
$stacktrace->{"raw"}[0]{"caller"}[3] eq "Carp::REPL::repl"
Truth table66%2/3
A or B
A= @{$stacktrace->{"raw"};} == 0B= $stacktrace->{"raw"}[0]{"caller"}[3] eq "Carp::REPL::repl"
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # get out of Carp:: | ||||||||||||||||||||||||||||
| 32 | 0 | shift @{ $stacktrace->{raw} } | |||||||||||||||||||||||||||
| 33 | 2 | until @{ $stacktrace->{raw} } == 0 | |||||||||||||||||||||||||||
| 34 | 2 | ▶ | || $stacktrace->{raw}[0]{caller}[0] !~ /^Carp(?:::|$)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@{$stacktrace->{"raw"};} == 0 or not $stacktrace->{"raw"}[0]{"caller"}[0] =~ /^Carp(?:::|$)/
MC/DC0%0/2
@{$stacktrace->{"raw"};} == 0 or not $stacktrace->{"raw"}[0]{"caller"}[0] =~ /^Carp(?:::|$)/
@{$stacktrace->{"raw"};} == 0
not $stacktrace->{"raw"}[0]{"caller"}[0] =~ /^Carp(?:::|$)/
Truth table33%1/3
A or not(B)
A= @{$stacktrace->{"raw"};} == 0B= not $stacktrace->{"raw"}[0]{"caller"}[0] =~ /^Carp(?:::|$)/
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | shift @{ $stacktrace->{raw} } | |||||||||||||||||||||||||||
| 37 | 2 | ▶ | until @{ $stacktrace->{raw} } == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@{$stacktrace->{"raw"};} == 0 or $Carp::REPL::bottom_frame-- <= 0
MC/DC0%0/2
@{$stacktrace->{"raw"};} == 0 or $Carp::REPL::bottom_frame-- <= 0
@{$stacktrace->{"raw"};} == 0
$Carp::REPL::bottom_frame-- <= 0
Truth table33%1/3
A or B
A= @{$stacktrace->{"raw"};} == 0B= $Carp::REPL::bottom_frame-- <= 0
| |||||||||||||||||||||||||||||
| 38 | || $Carp::REPL::bottom_frame-- <= 0; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | return $stacktrace; | |||||||||||||||||||||||||||
| 41 | }, | ||||||||||||||||||||||||||||
| 42 | 3 | ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | has frame_index => ( | |||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||
| 46 | isa => 'Int', | ||||||||||||||||||||||||||||
| 47 | default => 0, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 90 | ▶ | sub frame { | ||||||||||||||||||||||||||
|
Subroutine
frame: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 90 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | 90 | ▶ | my $i = @_ ? shift : $self->frame_index; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 90 | return $self->stacktrace->frame($i); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 90 | ▶ | around 'frame_index' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=23.7
| |||||||||||||||||||||||||||||
| 58 | 90 | my $orig = shift; | |||||||||||||||||||||||||||
| 59 | 90 | my ($self, $index) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 90 | ▶ | return $orig->(@_) if !defined($index); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($index < 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | 0 | warn "You're already at the bottom frame.\n"; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif ($index >= $self->frame_count) { | ||||||||||||||||||||||||||||
| 67 | 0 | warn "You're already at the top frame.\n"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||||||||
| 70 | 0 | $orig->(@_); | |||||||||||||||||||||||||||
| 71 | 0 | my $frame = $self->frame; | |||||||||||||||||||||||||||
| 72 | 0 | my ($file, $line) = ($frame->filename, $frame->line); | |||||||||||||||||||||||||||
| 73 | 0 | $self->print("Now at $file:$line (frame $index)."); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 3 | }; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # this is totally the wrong spot for this. oh well. | ||||||||||||||||||||||||||||
| 78 | 15 | ▶ | around 'read' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=16 SCAR=45.5
| |||||||||||||||||||||||||||||
| 79 | 15 | my $orig = shift; | |||||||||||||||||||||||||||
| 80 | 15 | my ($self, @rest) = @_; | |||||||||||||||||||||||||||
| 81 | 15 | my $line = $self->$orig(@rest); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 15 | ▶ | return if !defined($line) || $line =~ /^\s*:q\s*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $line or $line =~ /^\s*:q\s*/
MC/DC50%1/2
not defined $line or $line =~ /^\s*:q\s*/
not defined $line
$line =~ /^\s*:q\s*/
Truth table66%2/3
not(A) or B
A= not defined $lineB= $line =~ /^\s*:q\s*/
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 14 | ▶ | if ($line =~ /^\s*:b?t\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | $self->print($self->stacktrace); | |||||||||||||||||||||||||||
| 87 | 0 | return ''; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 14 | ▶ | if ($line =~ /^\s*:top\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->frame_index($self->frame_count - 1); | |||||||||||||||||||||||||||
| 92 | 0 | return ''; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 14 | ▶ | if ($line =~ /^\s*:b(?:ot(?:tom)?)?\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->frame_index(0); | |||||||||||||||||||||||||||
| 97 | 0 | return ''; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 14 | ▶ | if ($line =~ /^\s*:up?\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $self->frame_index($self->frame_index + 1); | |||||||||||||||||||||||||||
| 102 | 0 | return ''; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 14 | ▶ | if ($line =~ /^\s*:d(?:own)?\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | $self->frame_index($self->frame_index - 1); | |||||||||||||||||||||||||||
| 107 | 0 | return ''; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 14 | ▶ | if ($line =~ /^\s*:l(?:ist)?\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | my $frame = $self->frame; | |||||||||||||||||||||||||||
| 112 | 0 | my ($file, $num) = ($frame->filename, $frame->line); | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | open my $handle, '<', $file or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | warn "Unable to open $file for reading: $!\n"; | |||||||||||||||||||||||||||
| 115 | 0 | return ''; | |||||||||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||||||||||
| 117 | 0 | my @code = <$handle>; | |||||||||||||||||||||||||||
| 118 | 0 | chomp @code; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $min = $num - 6; | |||||||||||||||||||||||||||
| 121 | 0 | my $max = $num + 4; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | $min = 0 if $min < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $max = $#code if $max > $#code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my @lines; | |||||||||||||||||||||||||||
| 126 | 0 | $self->print("File $file:\n"); | |||||||||||||||||||||||||||
| 127 | 0 | for my $cur ($min .. $max) { | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | next if !defined($code[$cur]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | push @lines, sprintf "%s%*d: %s", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | $cur + 1 == $num ? '*' : ' ', | ||||||||||||||||||||||||||||
| 132 | length($max), | ||||||||||||||||||||||||||||
| 133 | $cur + 1, | ||||||||||||||||||||||||||||
| 134 | $code[$cur]; | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | $self->print(join "\n", @lines); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | return ''; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 14 | ▶ | if ($line =~ /^\s*:e(?:nv)?\s*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $self->print(Dump($self->frame->lexicals)->Names('Env')->Out); | |||||||||||||||||||||||||||
| 144 | 0 | return ''; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 14 | return $line; | |||||||||||||||||||||||||||
| 148 | 3 | }; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Provide an alias for each lexical in the current stack frame | ||||||||||||||||||||||||||||
| 151 | 14 | ▶ | around 'mangle_line' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 14 | my $orig = shift; | |||||||||||||||||||||||||||
| 153 | 14 | my ($self, @rest) = @_; | |||||||||||||||||||||||||||
| 154 | 14 | my $line = $self->$orig(@rest); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 14 | my $frame = $self->frame; | |||||||||||||||||||||||||||
| 157 | 14 | my $package = $frame->package; | |||||||||||||||||||||||||||
| 158 | 14 | my $lexicals = $frame->lexicals; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | my $declarations = join "\n", | ||||||||||||||||||||||||||||
| 161 | 14 | map {"my $_;"} | |||||||||||||||||||||||||||
| 162 | keys %$lexicals; | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 14 | my $aliases = << ' ALIASES'; | |||||||||||||||||||||||||||
| 165 | while (my ($k, $v) = each %{ $_REPL->frame->lexicals }) { | ||||||||||||||||||||||||||||
| 166 | Devel::LexAlias::lexalias 0, $k, $v; | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | my $_a; Devel::LexAlias::lexalias 0, '$_a', \$_REPL->frame->{args}; | ||||||||||||||||||||||||||||
| 169 | ALIASES | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 14 | return << " CODE"; | |||||||||||||||||||||||||||
| 172 | package $package; | ||||||||||||||||||||||||||||
| 173 | no warnings 'misc'; # declaration in same scope masks earlier instance | ||||||||||||||||||||||||||||
| 174 | no strict 'vars'; # so we get all the global variables in our package | ||||||||||||||||||||||||||||
| 175 | $declarations | ||||||||||||||||||||||||||||
| 176 | $aliases | ||||||||||||||||||||||||||||
| 177 | $line | ||||||||||||||||||||||||||||
| 178 | CODE | ||||||||||||||||||||||||||||
| 179 | 3 | }; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 3 | 1; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||