| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Devel::STDERR::Indent; | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use namespace::clean -except => "meta"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Sub::Exporter -setup => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | exports => [qw(indent)], | ||||||||||||||||||||||||||||
| 14 | 1 | }; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | our $VERSION = "0.06"; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | sub indent { | ||||||||||||||||||||||||||
|
Subroutine
indent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 8 | my $h = __PACKAGE__->new(@_); | |||||||||||||||||||||||||||
| 20 | 8 | $h->enter; | |||||||||||||||||||||||||||
| 21 | 8 | return $h; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 8 | my ( $class, @args ) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | ▶ | unshift @args, "message" if @args % 2 == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 8 | return {@args}; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | has message => ( | |||||||||||||||||||||||||||
| 33 | isa => "Str", | ||||||||||||||||||||||||||||
| 34 | is => "ro", | ||||||||||||||||||||||||||||
| 35 | predicate => "has_message", | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | has indent_string => ( | |||||||||||||||||||||||||||
| 39 | isa => "Str", | ||||||||||||||||||||||||||||
| 40 | is => "ro", | ||||||||||||||||||||||||||||
| 41 | default => " ", | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | has enter_string => ( | |||||||||||||||||||||||||||
| 45 | isa => "Str", | ||||||||||||||||||||||||||||
| 46 | is => "ro", | ||||||||||||||||||||||||||||
| 47 | default => " -> ", | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | has leave_string => ( | |||||||||||||||||||||||||||
| 51 | isa => "Str", | ||||||||||||||||||||||||||||
| 52 | is => "ro", | ||||||||||||||||||||||||||||
| 53 | default => " <- ", | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | has _previous_hook => ( | |||||||||||||||||||||||||||
| 57 | is => "rw", | ||||||||||||||||||||||||||||
| 58 | predicate => "_has_previous_hook", | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | has _active => ( | |||||||||||||||||||||||||||
| 62 | isa => "Bool", | ||||||||||||||||||||||||||||
| 63 | is => "rw", | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 8 | ▶ | sub DEMOLISH { | ||||||||||||||||||||||||||
|
Subroutine
DEMOLISH: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 8 | $self->leave; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | ▶ | sub enter { | ||||||||||||||||||||||||||
|
Subroutine
enter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 8 | ▶ | return if $self->_active; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 8 | $self->install; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | if ( $self->has_message ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 4 | $self->emit( $self->enter_string . $self->message, "\n" ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | $self->_active(1); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 8 | ▶ | sub leave { | ||||||||||||||||||||||||||
|
Subroutine
leave: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 8 | ▶ | return unless $self->_active; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | ▶ | if ( $self->has_message ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 4 | $self->emit( $self->leave_string . $self->message, "\n" ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 8 | $self->uninstall; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 8 | $self->_active(0); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 44 | ▶ | sub warn { | ||||||||||||||||||||||||||
|
Subroutine
warn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 44 | my ( $self, @output ) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 44 | $self->emit( $self->format(@output) ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 52 | ▶ | sub emit { | ||||||||||||||||||||||||||
|
Subroutine
emit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 52 | my ( $self, @output ) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 52 | ▶ | if ( my $hook = $self->_previous_hook ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 30 | $hook->(@output); | |||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||
| 111 | 22 | local $,; | |||||||||||||||||||||||||||
| 112 | 22 | local $\; | |||||||||||||||||||||||||||
| 113 | 22 | print STDERR @output; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 44 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 44 | my ( $self, @str ) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 44 | my $str = join "", @str; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 44 | ▶ | if ( $self->should_indent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 37 | my $indent = $self->indent_string; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # indent every line | ||||||||||||||||||||||||||||
| 126 | 37 | $str =~ s/^/$indent/gm; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 37 | return $str; | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 7 | return $str; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 44 | ▶ | sub should_indent { | ||||||||||||||||||||||||||
|
Subroutine
should_indent: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 44 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # always indent if there's an enter/leave message | ||||||||||||||||||||||||||||
| 138 | 44 | ▶ | return 1 if $self->has_message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # indent if we're nested | ||||||||||||||||||||||||||||
| 141 | 16 | ▶ | if ( $self->_has_previous_hook ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 9 | my $hook = $self->_previous_hook; | |||||||||||||||||||||||||||
| 143 | 9 | ▶ | if ( blessed($hook) and $hook->isa("Devel::STDERR::Indent::Hook") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($hook) and $hook->isa("Devel::STDERR::Indent::Hook")
MC/DC0%0/2
builtin::blessed($hook) and $hook->isa("Devel::STDERR::Indent::Hook")
builtin::blessed($hook)
$hook->isa("Devel::STDERR::Indent::Hook")
Truth table33%1/3
A and B
A= builtin::blessed($hook)B= $hook->isa("Devel::STDERR::Indent::Hook")
| |||||||||||||||||||||||||||||
| 144 | 9 | return 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # otherwise we're at the top level, don't indent unnecessarily, it's distracting | ||||||||||||||||||||||||||||
| 149 | 7 | return; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 8 | ▶ | sub install { | ||||||||||||||||||||||||||
|
Subroutine
install: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 8 | my $weak = $self; | |||||||||||||||||||||||||||
| 156 | 8 | weaken($weak); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 8 | ▶ | if ( my $prev = $SIG{__WARN__} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 6 | $self->_previous_hook($prev); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 8 | ▶ | $SIG{__WARN__} = bless sub { $weak->warn(@_) }, "Devel::STDERR::Indent::Hook"; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 8 | ▶ | sub uninstall { | ||||||||||||||||||||||||||
|
Subroutine
uninstall: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 8 | ▶ | if ( my $prev = $self->_previous_hook ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 6 | $SIG{__WARN__} = $prev; | |||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | 2 | delete $SIG{__WARN__}; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | __PACKAGE__; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||