| 1 | 2 | ▶ | use v5.20; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 2 | ▶ | use experimental qw( signatures postderef lexical_subs ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use feature (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | package Marlin::Role::Antlers; | ||
| 8 | |||
| 9 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 10 | our $VERSION = '0.003001'; | ||
| 11 | |||
| 12 | 2 | ▶ | use Exporter::Tiny (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Marlin::Antlers (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Marlin::Util -lexical, qw( true false ); | ||
| 15 | use Role::Tiny (); | ||
| 16 | use Types::Common -lexical, -all; | ||
| 17 | |||
| 18 | our @ISA = qw( Marlin::Antlers ); | ||
| 19 | our @EXPORT = qw( has with requires before after around signature signature_for __FINALIZE__ ); | ||
| 20 | |||
| 21 | sub import { | ||
| 22 | my $me = shift; | ||
| 23 | my $globals = +{ @_ && ref($_[0]) eq q(HASH) ? shift->%* : () }; | ||
| 24 | $globals->{__role} = 1; | ||
| 25 | delete $globals->{constructor}; | ||
| 26 | delete $globals->{sloppy}; | ||
| 27 | unshift @_, $globals; | ||
| 28 | unshift @_, $me; | ||
| 29 | goto &Exporter::Tiny::import; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _generate_requires ( $me, $name, $value, $globals ) { | ||
| 33 | return sub ( @methods ) { | ||
| 34 | |||
| 35 | assert_Str $_ for @methods; | ||
| 36 | |||
| 37 | $globals->{MARLIN}{requires} //= []; | ||
| 38 | push $globals->{MARLIN}{requires}->@*, @methods; | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _generate_signature_for ( $me, $name, $value, $globals ) { | ||
| 43 | my $SUB; | ||
| 44 | $SUB = sub { | ||
| 45 | my ( $function, %opts ) = @_; | ||
| 46 | $opts{method} = 1 if !exists $opts{method}; | ||
| 47 | $opts{package} = $globals->{into} if !exists $opts{package}; | ||
| 48 | $opts{next} ||= delete $opts{goto_next} if exists $opts{goto_next}; | ||
| 49 | |||
| 50 | if ( is_ArrayRef $function ) { | ||
| 51 | return map { $SUB->( $_, %opts ) } $function->@*; | ||
| 52 | } | ||
| 53 | |||
| 54 | my $package = $opts{package}; | ||
| 55 | my $fullname = ( $function =~ /::/ ) ? $function : "$package\::$function"; | ||
| 56 | $opts{subname} ||= ( $function =~ /::(\w+)$/ ) ? $1 : $function; | ||
| 57 | $opts{next} ||= do { no strict 'refs'; exists(&$fullname) ? \&$fullname : undef; }; | ||
| 58 | if ( $opts{fallback} and not $opts{next} ) { | ||
| 59 | $opts{next} = ref( $opts{fallback} ) ? $opts{fallback} : sub {}; | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( $opts{next} ) { | ||
| 63 | @_ = ( $function, %opts ); | ||
| 64 | goto \&Type::Params::signature_for; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $wrapper = Type::Params::signature( %opts, next => true ); | ||
| 68 | push @{ $Role::Tiny::INFO{$globals->{into}}{modifiers} ||= [] }, | ||
| 69 | [ 'around', $function, $wrapper ]; | ||
| 70 | }; | ||
| 71 | return $SUB; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _for_cmm ( $me, $kind, $globals ) { | ||
| 75 | return sub ( @names ) { | ||
| 76 | |||
| 77 | my $coderef = pop @names; | ||
| 78 | assert_CodeRef $coderef; | ||
| 79 | |||
| 80 | push @{ $Role::Tiny::INFO{$globals->{into}}{modifiers} ||= [] }, | ||
| 81 | [ $kind, @names, $coderef ]; | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | __PACKAGE__ | ||
| 86 | __END__ | ||