File Coverage

blib/lib/Devel/Eval.pm
Criterion Covered Total %
statement 25 37 67.5
branch 3 20 15.0
condition 0 3 0.0
subroutine 9 10 90.0
pod 1 3 33.3
total 38 73 52.0


line stmt bran cond sub pod time code
1             package Devel::Eval;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Devel::Eval - Allows you to debug string evals
8              
9             =head1 SYNOPSIS
10              
11             use Devel::Eval 'dval';
12            
13             dval "print 'Hello World!';";
14              
15             =head1 DESCRIPTION
16              
17             In the Perl debugger, code created via a string eval is effectively
18             invisible. You can run it, but the debugger will not be able to display
19             the code as it runs.
20              
21             For modules that make heavy use of code generation, this can make
22             debugging almost impossible.
23              
24             B provides an alternative to string eval that will
25             do a string-eval equivalent, except that it will run the code via a
26             temp file.
27              
28             Because the eval is being done though a physical file, the debugger will
29             be able to see this code and you can happily debug your generated code
30             as you do all the rest of your code.
31              
32             =head1 FUNCTIONS
33              
34             =cut
35              
36 2     2   30889 use 5.006;
  2         9  
  2         156  
37 2     2   14 use strict;
  2         6  
  2         73  
38 2     2   22 use Exporter ();
  2         5  
  2         63  
39 2     2   3493 use File::Temp ();
  2         67635  
  2         58  
40              
41 2     2   16 use vars qw{$VERSION @ISA @EXPORT $TRACE $UNLINK};
  2         4  
  2         217  
42             BEGIN {
43 2     2   5 $VERSION = '1.01';
44 2         36 @ISA = 'Exporter';
45 2         5 @EXPORT = 'dval';
46 2 50       9 $TRACE = '' unless defined $TRACE;
47 2 50       542 $UNLINK = 1 unless defined $UNLINK;
48             }
49              
50             =pod
51              
52             =head2 dval
53              
54             The C function takes a single parameter that should be the string
55             you want to eval, and executes it.
56              
57             Because this is intended for code generation testing, your code is
58             expected to be safe to run via a 'require' (that is, it should return
59             true).
60              
61             =cut
62              
63             sub dval ($) {
64 1 50   1 1 28 if ( $^V >= 5.008009 ) {
65 1         6 pval(@_);
66             } else {
67 0         0 fval(@_);
68             }
69             }
70              
71             sub pval ($) {
72 1     1 0 6 local $^P = $^P | 0x800;
73 1     1   80 eval $_[0];
  1         31  
74             }
75              
76             sub fval ($) {
77 0     0 0 0 my ($fh, $filename) = File::Temp::tempfile();
78 0 0       0 $fh->print("$_[0]") or die "print: $!";
79 0 0       0 close( $fh ) or die "close: $!";
80 0         0 my $message = "# do $filename\n";
81 0 0 0     0 if ( defined $TRACE and not ref $TRACE ) {
    0          
82 0 0       0 print STDOUT $message if $TRACE eq 'STDOUT';
83 0 0       0 print STDERR $message if $TRACE eq 'STDERR';
84             } elsif ( $TRACE ) {
85 0         0 $TRACE->print($message);
86             }
87 0         0 do $filename;
88 0 0       0 unlink $filename if $UNLINK;
89 0         0 return 1;
90             }
91              
92             1;
93              
94             =pod
95              
96             =head1 SUPPORT
97              
98             Bugs should be always be reported via the CPAN bug tracker at
99              
100             L
101              
102             For other issues, or commercial enhancement or support, contact the author.
103              
104             =head1 AUTHORS
105              
106             Adam Kennedy Ecpan@ali.asE
107              
108             =head1 COPYRIGHT
109              
110             Copyright 2009 Adam Kennedy.
111              
112             This program is free software; you can redistribute
113             it and/or modify it under the same terms as Perl itself.
114              
115             The full text of the license can be found in the
116             LICENSE file included with this module.
117              
118             =cut