Skip to content

Commit c1d415b

Browse files
committed
Attribute-Handlers: rework eval code
This makes the logic a bit simpler, and makes it easier to debug as well. Reducing the amount of code that needs to be inside the eval makes it easier to debug what is going on, especially from an internals point of view (eg with -Dl enabled).
1 parent f33868e commit c1d415b

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

dist/Attribute-Handlers/lib/Attribute/Handlers.pm

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use Carp;
44
use warnings;
55
use strict;
66
our $AUTOLOAD;
7-
our $VERSION = '1.02'; # remember to update version in POD!
7+
our $VERSION = '1.03'; # remember to update version in POD!
88
# $DB::single=1;
9-
9+
my $debug= $ENV{DEBUG_ATTRIBUTE_HANDLERS} || 0;
1010
my %symcache;
1111
sub findsym {
1212
my ($pkg, $ref, $type) = @_;
@@ -241,20 +241,33 @@ sub _apply_handler_AH_ {
241241
my ($declaration, $phase) = @_;
242242
my ($pkg, $ref, $attr, $data, $raw, $handlerphase, $filename, $linenum) = @$declaration;
243243
return unless $handlerphase->{$phase};
244-
# print STDERR "Handling $attr on $ref in $phase with [$data]\n";
244+
print STDERR "Handling $attr on $ref in $phase with [$data]\n"
245+
if $debug;
245246
my $type = ref $ref;
246247
my $handler = "_ATTR_${type}_${attr}";
247248
my $sym = findsym($pkg, $ref);
248249
$sym ||= $type eq 'CODE' ? 'ANON' : 'LEXICAL';
249250
no warnings;
250251
if (!$raw && defined($data)) {
251252
if ($data ne '') {
252-
my $evaled = eval("package $pkg; no warnings; no strict;
253-
local \$SIG{__WARN__}=sub{die}; [$data]");
254-
$data = $evaled unless $@;
253+
# keeping the minimum amount of code inside the eval string
254+
# makes debugging perl internals issues with this logic easier.
255+
my $code= "package $pkg; my \$ref= [$data]; \$data= \$ref; 1";
256+
print STDERR "Evaling: '$code'\n"
257+
if $debug;
258+
local $SIG{__WARN__} = sub{ die };
259+
no strict;
260+
no warnings;
261+
# Note we do not need
262+
eval($code) or do {
263+
print STDERR "Eval failed: $@"
264+
if $debug;
265+
};
255266
}
256267
else { $data = undef }
257268
}
269+
270+
# now call the handler with the $data decoded (maybe)
258271
$pkg->$handler($sym,
259272
(ref $sym eq 'GLOB' ? *{$sym}{ref $ref}||$ref : $ref),
260273
$attr,
@@ -300,7 +313,7 @@ Attribute::Handlers - Simpler definition of attribute handlers
300313
301314
=head1 VERSION
302315
303-
This document describes version 1.02 of Attribute::Handlers.
316+
This document describes version 1.03 of Attribute::Handlers.
304317
305318
=head1 SYNOPSIS
306319

0 commit comments

Comments
 (0)