File Coverage

blib/lib/Language/Befunge/Debug.pm
Criterion Covered Total %
statement 41 41 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 3 3 100.0
total 65 65 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Language::Befunge.
3             # Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.
4             #
5             # This program is free software; you can redistribute it and/or modify
6             # it under the same terms as Perl itself.
7             #
8             #
9              
10             package Language::Befunge::Debug;
11              
12 69     69   2337 use 5.010;
  69         246  
  69         4166  
13 69     69   376 use strict;
  69         127  
  69         2703  
14 69     69   351 use warnings;
  69         298  
  69         4043  
15              
16 69     69   369 use base qw{ Exporter };
  69         137  
  69         40327  
17             our @EXPORT = qw{ debug };
18              
19              
20             # -- public subs
21              
22 8774     8774 1 19134 sub debug {}
23              
24             my %redef;
25             sub enable {
26 1     1 1 1136 %redef = ( debug => sub { warn @_; } );
  1     1   1557  
27 1         4 _redef();
28             }
29              
30             sub disable {
31 1     1 1 805 %redef = ( debug => sub {} );
  1     1   1267  
32 1         3 _redef();
33             }
34              
35              
36             # -- private subs
37              
38             #
39             # _redef()
40             #
41             # recursively walk the symbol table, and replace subs named after %redef
42             # keys with the matching value of %redef.
43             #
44             # this is not really clean, but since the sub debug() is exported in
45             # other modules, replacing the sub in *this* module is not enough: other
46             # modules still refer to their local copy.
47             #
48             # also, calling sub with full name Language::Befunge::Debug::debug() has
49             # performance issues (10%-15%) compared to using an exported sub...
50             #
51             my %orig; # original subs
52             sub _redef {
53 410     410   573 my $parent = shift;
54 410 100       750 if ( not defined $parent ) {
55 2         4 $parent = '::';
56 2         8 foreach my $sub ( keys %redef ) {
57 2         9 $orig{ $sub } = \&$sub;
58             }
59             }
60 69     69   1199 no strict 'refs';
  69         163  
  69         4027  
61 69     69   450 no warnings 'redefine';
  69         134  
  69         20925  
62 410         441 foreach my $ns ( grep /^\w+::/, keys %{$parent} ) {
  410         6502  
63 410         752 $ns = $parent . $ns;
64 410 100       1361 _redef($ns) unless $ns eq '::main::';
65 410         1400 foreach my $sub (keys %redef) {
66             next # before replacing, check that...
67 410         2144 unless exists ${$ns}{$sub} # named sub exist...
  60         1044  
68 410 100 100     438 && \&{ ${$ns}{$sub} } == $orig{$sub}; # ... and refer to the one we want to replace
  60         59  
69 4         10 *{$ns . $sub} = $redef{$sub};
  4         24  
70             }
71             }
72             }
73              
74             1;
75              
76             __END__