Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lazy load #7

Merged
merged 3 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"kfly8 <kfly@cpan.org>"
],
"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"
],
Expand Down Expand Up @@ -42,6 +42,7 @@
},
"runtime" : {
"requires" : {
"B::Hooks::EndOfScope" : "0.23",
"Function::Parameters" : "2.000003",
"Scalar::Util" : "0",
"Scope::Upper" : "0",
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
18 changes: 6 additions & 12 deletions lib/Function/Return.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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)};

Expand Down Expand Up @@ -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<Function::Return> at runtime, then you should call C<check_sub> specifically.

=head1 NOTE

=head2 enforce LIST to simplify
Expand Down
13 changes: 13 additions & 0 deletions t/10_case_lazy.t
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions t/lib/Sample.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Sample;

use Function::Return;
use Types::Standard -types;

sub invalid :Return(Str) {
return { };
}

1;