Pacific-Design.com

    
Home Index

1. Perl5

2. Perl Debug

+ Alias

+ Carp

+ Ref

Perl5 / Perl Debug /

Simple Help to Debug in Perl

#!/usr/bin/env perl 
use strict;
use warnings;
use Debug;
use subs 'puts';
*puts = \&Debug::puts;

#---------------------------------------------------------------------#
print_subroutine_name();

#---------------------------------------------------------------------#
my $log1 = { 
    'Action' => 'POST', 
    'FILE'   => __FILE__,
};
puts(__LINE__, $log1);

#---------------------------------------------------------------------#
my $log2 = {                                                                                                                                                                                 
    'Key1' => 'Value1',
    'Key2' => 'Value2',
    'Key3' => 'Value3',
};
puts(__LINE__, $log2);

#---------------------------------------------------------------------#
sub print_subroutine_name {
    print __FILE__ . ":" . ( caller(0) )[3] . ":" . __LINE__ . "\n";
}

#---------------------------------------------------------------------#


package Debug;
use strict;
use warnings;
use Data::Dumper;
use vars qw( $counter );
#------------------------------------------------------------------------------------#
sub puts {
    my($line, $content) = @_;                                                                                                                                                                

    if ($content) {
        $counter++;
        my $line = sprintf("%03d", $line);

        my $filename = 'content' .  $counter . '-' . $line . '.html';
        open( FILE, '>', $filename );

        if ( ( ref($content) eq "HASH" ) || ( ref($content) eq "ARRAY" ) ) { 
            print FILE Dumper($content);
        }   
        else { print FILE $content; }

        close(FILE);
    }   
    return;
}
#------------------------------------------------------------------------------------#
1;