debugging code
Matt S Trout [Tue, 12 Oct 2010 15:48:42 +0000 (16:48 +0100)]
lib/HTML/Zoom/MaybeDebug.pm [new file with mode: 0644]

diff --git a/lib/HTML/Zoom/MaybeDebug.pm b/lib/HTML/Zoom/MaybeDebug.pm
new file mode 100644 (file)
index 0000000..ba91c2c
--- /dev/null
@@ -0,0 +1,28 @@
+package HTML::Zoom::MaybeDebug;
+
+use strictures 1;
+
+sub import { }
+
+if (my $level = $ENV{'HTML_ZOOM_DEBUG'}) {
+  foreach my $mod (qw(Smart::Comments Data::Dumper::Concise JSON)) {
+    (my $file_stem = $mod) =~ s/::/\//g;
+    die "HTML_ZOOM_DEBUG env var set to ${level} - this requires the ${mod}\n"
+        ."module but it failed to load with error:\n$@"
+      unless eval { require "${file_stem}.pm"; 1 };
+  }
+  my @smartness = map '#'x$_, 3 .. $level+2;
+  no warnings 'redefine';
+  *import = sub { Smart::Comments->import(@smartness) };
+  my $j = JSON->new->space_after;
+  my $d = \&Data::Dumper::Concise::Dumper;
+  *Smart::Comments::Dumper = sub {
+    my $r;
+    unless (eval { $r = $j->encode($_[0]); 1 }) {
+      $r = $d->($_[0]);
+    }
+    $r;
+  };
+}
+
+1;