]>
Dogcows Code - chaz/p5-Return-Type-Lexical/blob - lib/Return/Type/Lexical.pm
8da614a016af249c35d1c7308343b08cddf56485
1 package Return
::Type
::Lexical
;
2 # ABSTRACT: Same thing as Return::Type, but lexical
8 use parent
'Return::Type';
10 our $VERSION = '999.999'; # VERSION
13 my ($class, %args) = @_;
14 $^H{'Return::Type::Lexical/in_effect'} = exists $args{check
} && !$args{check
} ? 0 : 1;
18 $^H{'Return::Type::Lexical/in_effect'} = 0;
23 $level = 0 if !defined $level;
24 my $hinthash = (caller($level))[10];
25 my $in_effect = $hinthash->{'Return::Type::Lexical/in_effect'};
26 return !defined $in_effect || $in_effect;
29 # XXX This is kind of janky. It relies upon Return::Type using Attribute::Handlers, and it assumes
30 # some internal Attribute::Handlers behavior. If it proves to be too fragile, we may need to copy
31 # the Return::Type code to here. Or make Return::Type lexical if that can be done without breaking
32 # backward-compatibility.
35 $handler = $UNIVERSAL::{ReturnType
};
36 delete $UNIVERSAL::{ReturnType
};
37 delete $UNIVERSAL::{_ATTR_CODE_ReturnType
};
39 sub UNIVERSAL
::ReturnType
:ATTR
(CODE
,BEGIN) {
40 my $in_effect = _in_effect
(4);
41 return if !$in_effect;
43 return $handler->(@_);
51 use Return::Type::Lexical;
52 use Types::Standard qw(Int);
54 sub foo :ReturnType(Int) { return "not an int" }
57 no Return::Type::Lexical;
58 sub bar :ReturnType(Int) { return "not an int" }
61 my $foo = foo(); # throws an error
62 my $bar = bar(); # returns "not an int"
64 # Can also be used with Devel::StrictMode to only perform
65 # type checks in strict mode:
67 use Devel::StrictMode;
68 use Return::Type::Lexical check => STRICT;
72 This module works just like L<Return::Type>, but type-checking can be enabled and disabled within
75 There is no runtime penalty when type-checking is disabled.
79 The C<check> attribute can be used to set whether or not types are checked.
This page took 0.033503 seconds and 4 git commands to generate.