File Coverage

blib/lib/Parse/Trace.pm
Criterion Covered Total %
statement 18 61 29.5
branch 0 18 0.0
condition 0 3 0.0
subroutine 6 11 54.5
pod 0 5 0.0
total 24 98 24.4


line stmt bran cond sub pod time code
1             # Copyright (c) Philippe Verdret, 1995-1997
2             require 5.000;
3 10     10   59 use strict;
  10         14  
  10         684  
4            
5             package Parse::Trace;
6             $Parse::Trace::VERSION = '2.21';
7 10     10   55 use Carp;
  10         23  
  10         2586  
8             #use vars qw($indent);
9             $Trace::indent = 0;
10            
11             # doesn't work with my Perl current version
12             #use FileHandle;
13             my $TRACE = \*STDERR; # Default
14            
15             my %cache = ();
16 0 0   0 0   sub name { $cache{$_[0]} or ($cache{$_[0]} = $_[0]->findName) }
17 0     0 0   sub inpkg { 'main' } # no better definition at the present time
18            
19             sub findName { # Try to find the "name" of self
20             # assume $self is put in a scalar variable
21 0     0 0   my $self = shift;
22 0           my $pkg = $self->inpkg;
23 0           my $symbol;
24             my $value;
25 10     10   256 no strict qw(refs);
  10         23  
  10         1639  
26 0           local $^W = 0;
27 0           map {
28 0           ($symbol = ${"${pkg}::"}{$_}) =~ s/[*]//;
  0            
29 0 0         if (defined($value = ${$symbol})) {
  0            
30 0 0         return $symbol if ($value eq $self);
31             }
32 0           } grep {! /\W/} keys %{"${$pkg}::"};
  0            
  0            
33 10     10   55 use strict qw(refs);
  10         17  
  10         2327  
34 0           return undef;
35             }
36             sub context {
37 0     0 0   my $self = shift;
38 0           my $ref = ref($self);
39 0           my $name = '';
40 0           $name = $self->name;
41 0 0         if (not $name) {
42 0           $name = $self->Parse::Trace::name;
43             }
44 0 0         my $sign = defined $name ? "[$name|$ref]" : "[$ref]";
45 0           print $TRACE " " x $Trace::indent, "$sign @_\n";
46             }
47            
48             sub trace {
49 0     0 0   my $self = shift;
50 0   0       my $class = (ref $self or $self);
51             # state switch
52 10     10   91 no strict qw(refs);
  10         16  
  10         1606  
53            
54 0           ${"${class}::trace"} = not ${"${class}::trace"};
  0            
  0            
55 0 0         if (${"${class}::trace"}) {
  0            
56 0           my $file = $class;
57 0           $file =~ s!::!/!g;
58 0           eval { # Load specialized methods
59             # die() is trapped by $Parse::Template::SIG{__DIE__}
60             #local $SIG{__DIE__} = sub {};
61             #require "${file}-t.pm";
62 0           do "${file}-t.pm"; # do esn't raised an exception
63             };
64 0           print STDERR "Trace is ON in class $class\n";
65             } else {
66 0           print STDERR "Trace is OFF in class $class\n";
67             }
68 10     10   55 use strict qw(refs);
  10         15  
  10         1651  
69             # output
70 0 0         if (@_) {
71 0 0         if (ref $_[0]) {
72 0           $TRACE = $_[0];
73             } else {
74             # $TRACE = new FileHandle("> $_[0]");
75 0 0         unless ($TRACE) {
76 0           croak qq^unable to open "$_[0]"^;
77             } else {
78 0           print STDERR "Trace put in $_[0]\n";
79             }
80             }
81             }
82             }
83            
84             1;
85             __END__