diff --git a/META.json b/META.json index 35cc75b..c96423f 100644 --- a/META.json +++ b/META.json @@ -4,7 +4,7 @@ "kfly8 " ], "dynamic_config" : 0, - "generated_by" : "Minilla/v3.1.2, CPAN::Meta::Converter version 2.150010", + "generated_by" : "Minilla/v3.1.4, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], @@ -42,6 +42,7 @@ }, "runtime" : { "requires" : { + "B::Hooks::EndOfScope" : "0.23", "Function::Parameters" : "2.000003", "Scalar::Util" : "0", "Scope::Upper" : "0", diff --git a/cpanfile b/cpanfile index cfb9ea1..1caff78 100644 --- a/cpanfile +++ b/cpanfile @@ -5,6 +5,7 @@ requires 'Sub::Info'; requires 'Scalar::Util'; requires 'Scope::Upper'; requires 'Function::Parameters', '2.000003'; +requires 'B::Hooks::EndOfScope', '0.23'; on 'test' => sub { requires 'Test::More', '0.98'; diff --git a/lib/Function/Return.pm b/lib/Function/Return.pm index 7061021..50afa49 100644 --- a/lib/Function/Return.pm +++ b/lib/Function/Return.pm @@ -10,6 +10,7 @@ use Sub::Util (); use Sub::Info (); use Scalar::Util (); use Scope::Upper (); +use B::Hooks::EndOfScope; use Function::Parameters; our $DEFAULT_ATTR_NAME = 'Return'; @@ -28,6 +29,10 @@ sub import { no strict qw(refs); *{"${pkg}::FETCH_CODE_ATTRIBUTES"} = \&_FETCH_CODE_ATTRIBUTES; *{"${pkg}::MODIFY_CODE_ATTRIBUTES"} = \&_MODIFY_CODE_ATTRIBUTES; + + on_scope_end { + _check_sub(); + } } my %ATTR; @@ -170,14 +175,7 @@ sub _register_return_info { $metadata{$key} = $info; } -{ - no warnings 'void'; # To avoid warnings 'Too late to run CHECK block' - sub CHECK { - check_sub(); - } -} - -sub check_sub { +sub _check_sub { for my $decl (@DECLARATIONS) { my ($pkg, $sub, $types) = @$decl{qw(pkg sub types)}; @@ -306,10 +304,6 @@ This interface is for power-user. Rather than using the C<< :Return >> attribute my $wrapped = Function::Return->wrap_sub($orig, [Str]); $wrapped->(); -=head3 Function::Return->check_sub() - -Generaly, it's unnecessary to call this method. If you loaded C at runtime, then you should call C specifically. - =head1 NOTE =head2 enforce LIST to simplify diff --git a/t/10_case_lazy.t b/t/10_case_lazy.t new file mode 100644 index 0000000..f11307b --- /dev/null +++ b/t/10_case_lazy.t @@ -0,0 +1,13 @@ +use strict; +use warnings; +use Test::More; +use Test::Fatal; + +use lib 't/lib'; + +# lazy load +require Sample; + +like exception { Sample::invalid() }, qr!^Invalid return in fun invalid: return!; + +done_testing; diff --git a/t/lib/Sample.pm b/t/lib/Sample.pm new file mode 100644 index 0000000..ffa14ca --- /dev/null +++ b/t/lib/Sample.pm @@ -0,0 +1,10 @@ +package Sample; + +use Function::Return; +use Types::Standard -types; + +sub invalid :Return(Str) { + return { }; +} + +1;