| 1 | package HTML::Zoom::MaybeDebug; | ||
| 2 | |||
| 3 | use strictures 1; | ||
| 4 | |||
| 5 | sub import { } | ||
| 6 | |||
| 7 | if (my $level = $ENV{'HTML_ZOOM_DEBUG'}) { | ||
| 8 | foreach my $mod (qw(Smart::Comments Data::Dumper::Concise JSON)) { | ||
| 9 | (my $file_stem = $mod) =~ s/::/\//g; | ||
| 10 | die "HTML_ZOOM_DEBUG env var set to ${level} - this requires the ${mod}\n" | ||
| 11 | ."module but it failed to load with error:\n$@" | ||
| 12 | unless eval { require "${file_stem}.pm"; 1 }; | ||
| 13 | } | ||
| 14 | my @smartness = map '#'x$_, 3 .. $level+2; | ||
| 15 | no warnings 'redefine'; | ||
| 16 | *import = sub { Smart::Comments->import(@smartness) }; | ||
| 17 | my $j = JSON->new->space_after; | ||
| 18 | my $d = \&Data::Dumper::Concise::Dumper; | ||
| 19 | *Smart::Comments::Dumper = sub { | ||
| 20 | my $r; | ||
| 21 | unless (eval { $r = $j->encode($_[0]); 1 }) { | ||
| 22 | $r = $d->($_[0]); | ||
| 23 | } | ||
| 24 | $r; | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | |||
| 28 | 1; |