File Coverage

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


line stmt bran cond sub pod time code
1             #
2             # This file is part of Language-Befunge
3             #
4             # This software is copyright (c) 2003 by Jerome Quelin.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9 68     68   1329 use 5.010;
  68         148  
10 68     68   220 use strict;
  68         158  
  68         1164  
11 68     68   208 use warnings;
  68         79  
  68         2887  
12              
13             package Language::Befunge::Debug;
14             # ABSTRACT: optimized debug solution for language::befunge
15             $Language::Befunge::Debug::VERSION = '5.000';
16 68     68   242 use base qw{ Exporter };
  68         120  
  68         14278  
17             our @EXPORT = qw{ debug };
18              
19              
20             # -- public subs
21              
22       8819 1   sub debug {}
23              
24             my %redef;
25             sub enable {
26 1     1 1 628 %redef = ( debug => sub { warn @_; } );
  1     1   1073  
27 1         3 _redef();
28             }
29              
30             sub disable {
31 1     1 1 686 %redef = ( debug => sub {} );
        1      
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 424     424   286 my $parent = shift;
54 424 100       461 if ( not defined $parent ) {
55 2         3 $parent = '::';
56 2         6 foreach my $sub ( keys %redef ) {
57 2         7 $orig{ $sub } = \&$sub;
58             }
59             }
60 68     68   268 no strict 'refs';
  68         82  
  68         1945  
61 68     68   209 no warnings 'redefine';
  68         82  
  68         10414  
62 424         244 foreach my $ns ( grep /^\w+::/, keys %{$parent} ) {
  424         3279  
63 424         480 $ns = $parent . $ns;
64 424 100       698 _redef($ns) unless $ns eq '::main::';
65 424         669 foreach my $sub (keys %redef) {
66             next # before replacing, check that...
67 424         949 unless exists ${$ns}{$sub} # named sub exist...
68 424 100 100     233 && \&{ ${$ns}{$sub} } == $orig{$sub}; # ... and refer to the one we want to replace
  62         35  
  62         414  
69 4         6 *{$ns . $sub} = $redef{$sub};
  4         16  
70             }
71             }
72             }
73              
74             1;
75              
76             __END__