Skip to content

Commit a8b100c

Browse files
autarchoschwald
authored andcommitted
Set sub names when calling defer_sub
1 parent 1c9754b commit a8b100c

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

lib/MooseX/Types.pm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ This generates a coercion handler function, e.g. C<to_Int($value)>.
486486
=cut
487487

488488
sub coercion_export_generator {
489-
my ($class, $type, $full, $undef_msg) = @_;
490-
return defer_sub undef, sub {
489+
my ($class, $sub_name, $type, $full, $undef_msg) = @_;
490+
return defer_sub $sub_name, sub {
491491
my ($value) = @_;
492492

493493
# we need a type object
@@ -511,9 +511,9 @@ Generates a constraint check closure, e.g. C<is_Int($value)>.
511511
=cut
512512

513513
sub check_export_generator {
514-
my ($class, $type, $full, $undef_msg) = @_;
514+
my ($class, $sub_name, $type, $full, $undef_msg) = @_;
515515

516-
return defer_sub undef, sub {
516+
return defer_sub $sub_name, sub {
517517
my ($value) = @_;
518518

519519
# we need a type object

lib/MooseX/Types/Base.pm

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,18 @@ sub import {
5353
# determine the wrapper, -into is supported for compatibility reasons
5454
my $wrapper = $options->{ -wrapper } || 'MooseX::Types';
5555

56-
$args[0]->{into} = $options->{ -into }
57-
if exists $options->{ -into };
56+
# It's a little gross to calculate the calling package here when
57+
# Sub::Exporter is going to do it again, but we need to give Sub::Defer a
58+
# fully qualified name if we give it a name at all, and we want to give it
59+
# a name. Otherwise it guesses at the name and will use its caller, which
60+
# in this case ends up being MooseX::Types, which is wrong.
61+
my $into;
62+
if (exists $options->{ -into }) {
63+
$into = $args[0]->{into} = $options->{ -into }
64+
}
65+
else {
66+
$into = caller(($options->{into_level} || 0) + 1)
67+
}
5868

5969
my %ex_util;
6070

@@ -79,7 +89,7 @@ sub import {
7989
my $check_name = "is_${type_short}";
8090
push @{ $ex_spec{exports} },
8191
$check_name,
82-
sub { $wrapper->check_export_generator($type_short, $type_full, $undef_msg) };
92+
sub { $wrapper->check_export_generator("${into}::$check_name", $type_short, $type_full, $undef_msg) };
8393

8494
# only export coercion helper if full (for libraries) or coercion is defined
8595
next TYPE
@@ -89,7 +99,7 @@ sub import {
8999
my $coercion_name = "to_${type_short}";
90100
push @{ $ex_spec{exports} },
91101
$coercion_name,
92-
sub { $wrapper->coercion_export_generator($type_short, $type_full, $undef_msg) };
102+
sub { $wrapper->coercion_export_generator("${into}::$coercion_name", $type_short, $type_full, $undef_msg) };
93103
$ex_util{ $type_short }{to}++; # shortcut to remember this exists
94104
}
95105

t/27-sub-defer.t

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More 0.88;
5+
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
6+
7+
use Test::Fatal;
8+
use B::Deparse;
9+
use MooseX::Types::Moose qw( Int );
10+
use Sub::Defer qw( undefer_all );
11+
12+
like(
13+
B::Deparse->new->coderef2text( \&is_Int ),
14+
qr/package Sub::Defer/,
15+
'is_Int sub has not yet been undeferred'
16+
);
17+
is(
18+
exception { undefer_all() },
19+
undef,
20+
'Sub::Defer::undefer_all works with subs exported by MooseX::Types'
21+
);
22+
unlike(
23+
B::Deparse->new->coderef2text( \&is_Int ),
24+
qr/package Sub::Defer/,
25+
'is_Int sub is now undeferred'
26+
);
27+
28+
done_testing();

0 commit comments

Comments
 (0)