Devel::Eval - Allows you to debug string evals


Devel-Eval documentation Contained in the Devel-Eval distribution.

Index


Code Index:

NAME

Top

Devel::Eval - Allows you to debug string evals

SYNOPSIS

Top

  use Devel::Eval 'dval';

  dval "print 'Hello World!';";

DESCRIPTION

Top

In the Perl debugger, code created via a string eval is effectively invisible. You can run it, but the debugger will not be able to display the code as it runs.

For modules that make heavy use of code generation, this can make debugging almost impossible.

Devel::Eval provides an alternative to string eval that will do a string-eval equivalent, except that it will run the code via a temp file.

Because the eval is being done though a physical file, the debugger will be able to see this code and you can happily debug your generated code as you do all the rest of your code.

FUNCTIONS

Top

dval

The dval function takes a single parameter that should be the string you want to eval, and executes it.

Because this is intended for code generation testing, your code is expected to be safe to run via a 'require' (that is, it should return true).

SUPPORT

Top

Bugs should be always be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-Eval

For other issues, or commercial enhancement or support, contact the author.

AUTHORS

Top

Adam Kennedy <cpan@ali.as>

COPYRIGHT

Top


Devel-Eval documentation Contained in the Devel-Eval distribution.
package Devel::Eval;

use 5.006;
use strict;
use Exporter   ();
use File::Temp ();

use vars qw{$VERSION @ISA @EXPORT $TRACE $UNLINK};
BEGIN {
	$VERSION = '1.01';
	@ISA     = 'Exporter';
	@EXPORT  = 'dval';
	$TRACE   = '' unless defined $TRACE;
	$UNLINK  = 1  unless defined $UNLINK;
}

sub dval ($) {
	if ( $^V >= 5.008009 ) {
		pval(@_);
	} else {
		fval(@_);
	}
}

sub pval ($) {
	local $^P = $^P | 0x800;
	eval $_[0];
}

sub fval ($) {
	my ($fh, $filename) = File::Temp::tempfile();
	$fh->print("$_[0]") or die "print: $!";
	close( $fh )        or die "close: $!";
	my $message = "# do $filename\n";
	if ( defined $TRACE and not ref $TRACE ) {
		print STDOUT $message if $TRACE eq 'STDOUT';
		print STDERR $message if $TRACE eq 'STDERR';
	} elsif ( $TRACE ) {
		$TRACE->print($message);
	}
	do $filename;
	unlink $filename if $UNLINK;
	return 1;
}

1;