package Poet::Util::Debug; BEGIN { $Poet::Util::Debug::VERSION = '0.07'; } use Carp qw(longmess); use Data::Dumper; use strict; use warnings; use base qw(Exporter); our @EXPORT_OK = map { ( "$_", "$_" . "s", "$_" . "_live", "$_" . "s_live" ) } qw(dc dd dh dp); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK ); my $console_log; sub _dump_value_with_caller { my ( $value, $func_name ) = @_; my $dump = Data::Dumper->new( [$value] )->Indent(1)->Sortkeys(1)->Quotekeys(0) ->Terse(1)->Dump(); my @caller = caller(1); return sprintf( "[%s at %s line %d.] [%d] %s\n", $func_name, $caller[1], $caller[2], $$, $dump ); } sub _define { my ( $func, $code ) = @_; no strict 'refs'; my $funcs = $func . "s"; my $func_live = $func . "_live"; my $funcs_live = $func . "s_live"; *$func = sub { return unless Poet::Environment->current_env->conf->is_development; $code->( _dump_value_with_caller( $_[0], $func ) ); }; *$funcs = sub { return unless Poet::Environment->current_env->conf->is_development; $code->( longmess( _dump_value_with_caller( $_[0], $funcs ) ) ); }; *$func_live = sub { $code->( _dump_value_with_caller( $_[0], $func_live ) ); }; *$funcs_live = sub { $code->( longmess( _dump_value_with_caller( $_[0], $funcs_live ) ) ); }; } _define( 'dc', sub { $console_log ||= Poet::Environment->current_env->logs_path("console.log"); open( my $fh, ">>$console_log" ); $fh->print( $_[0] ); } ); _define( 'dd', sub { die $_[0]; } ); _define( 'dh', sub { return "
\n$_[0]\n"; } ); _define( 'dp', sub { print STDERR $_[0]; } ); 1; =pod =head1 NAME Poet::Util::Debug - Debug utilities =head1 SYNOPSIS # In a script... use Poet::Script; # In a module... use Poet; # Automatically available in Mason components # then... # die with value dd $data; # print value to STDERR dp $data; # print value to logs/console.log dc $data; # return value prepped for HTML dh $data; # same as above with full stacktraces dds $data; dps $data; dcs $data; dhs $data; =head1 DESCRIPTION These debug utilities are automatically imported wherever C