1: ##################################################
   2: #This is my first craft post and a very simple   #
   3: #one - so be nice, please - but it can help when #
   4: #you start to bang your head to the wall because #
   5: #your script is not working...                   #
   6: #                                                #
   7: #It's a simple sub to print out var's values.    #
   8: #                                                #
   9: #Include it on your script and call it like this #
  10: #Debug_aid(var,output_type)                      #
  11: #                                                #
  12: # output_type:                                   #
  13: # w - creates a new file from scratch            #
  14: # a - append to the file, if it already exists   #
  15: ##################################################
  16: sub Debug_aid
  17: {
  18:     @t=(localtime)[0..5];
  19:     $dt=sprintf "%02u/%02u/%02u %02u:%02u:%02u",$t[3],
  20:     $t[4]+1,$t[5]+1900,$t[2],$t[1],$t[0];
  21:     
  22:     $out = $_[0];
  23:     $type = $_[1];
  24:     
  25:     if ($type eq "w")
  26:     {
  27:         open (DEB, ">debug.log");
  28:     }
  29:     elsif ($type eq "a")
  30:     {
  31:         open (DEB, ">>debug.log");
  32:     }
  33: 
  34:     print DEB "[$dt] -> $out\n\n";
  35:     close (DEB);
  36: }

Replies are listed 'Best First'.
Re: Debugging Aid
by rinceWind (Monsignor) on Apr 12, 2002 at 15:32 UTC
    As this is intended to be a general purpose subroutine, it's a good idea to localise all your variables with my just in case your calling code uses variables called $out, $type, etc. Then you can put a use strict; at the top, and still get a clean compile :-).

    Another suggested improvement:

    my @t=(localtime); $t[4]++; $t[5] += 1900; my $dt=sprintf "%02u/%02u/%02u %02u:%02u:%02u",@t[3,4,5,2,1,0];

    though you might want to consider using POSIX::strftime.

Re: Debugging Aid
by thelenm (Vicar) on Apr 12, 2002 at 15:09 UTC
    One suggested improvement: you don't really need the "Start" tag... in place of the if/else/Start, this should be sufficient:
    unlink "debug.log" if $type eq 'w';
    Actually, even that statement should be unnecessary, too. :-) It looks like it's simple and good for what it does, but is there a reason for creating a separate script like this instead of using the perl debugger or even print statements within the original script?

    Update: Ah, sorry, I think I misunderstood how it's to be used. In effect, this is a way of putting print statements in your original script. You may also want to look into Data::Dumper, which will let you print out data for types other than scalar.

      I've used the Start tag mainly to prevent an error. If you take a look at the line that says:
      unlink "debug.log" or goto Start
      You'll see that if the debug.log file doesn't exist, the script goes to the Start tag, instead of generating an error.

      Er Galvão Abbott
      a.k.a. Lobo, DaWolf
      Webdeveloper
        DaWolf, unlink will not error unless you tell it to. So, your default behaviour is to continue running the program as if nothing happened.

        Both goto Starts are redundant, as is the Start: label.

Re: Debugging Aid
by yodabjorn (Monk) on Apr 29, 2002 at 11:22 UTC
    Maybe a little "cleaner"?:

    sub Debug_aid
    {
       my ($out, $type) = @_ ; 
       my $timestamp =  scalar localtime();
       my %file = ( 'w' => ">$out",
                    'a' => ">>$out"
       );
    
       open (DEB, $file{$type}) or return 1 ;
       print DEB "timestamp -> $out\n\n";
       close (DEB) ;
       return 0 ;
    }
    

    Also I would suggest using Data::Dumper it is a life saver if you are ever needing to look at Structures. I almost always "use" it these days. anlother tip would be to accept another var called $debug then set an if on the print. This way you can set a cmd arg for debug messages or not..
    sub Debug_aid
    {
       my ($out, $type, $debug) = @_ ;
    .....
       print DEB "$dt -> $out\n\n" if $debug;
     
    Or be more eficient:
    sub Debug_aid
    {
       my ($out, $type, $debug) = @_ ;
       return(0) if (! $debug); 
    
    

    Always more than one way!.