| 1 | 5 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 5 | ▶ | use experimental qw( signatures postderef lexical_subs ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use feature (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | package Marlin::Antlers; | ||||||||||
| 8 | |||||||||||
| 9 | our $AUTHORITY = 'cpan:TOBYINK'; | ||||||||||
| 10 | our $VERSION = '0.003001'; | ||||||||||
| 11 | |||||||||||
| 12 | 5 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.4
| |||||||||||
| 13 | 5 | my @funcs = qw( at_runtime after_runtime ); | |||||||||
| 14 | 5 | ▶ | if ( eval 'require B::Hooks::AtRuntime; B::Hooks::AtRuntime->VERSION( 8 ) ' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | B::Hooks::AtRuntime->import( @funcs ); | |||||||||
| 16 | } | ||||||||||
| 17 | else { | ||||||||||
| 18 | 5 | require B::Hooks::AtRuntime::OnlyCoreDependencies; | |||||||||
| 19 | 0 | B::Hooks::AtRuntime::OnlyCoreDependencies->import( @funcs ); | |||||||||
| 20 | } | ||||||||||
| 21 | }; | ||||||||||
| 22 | |||||||||||
| 23 | use Class::Method::Modifiers qw( install_modifier ); | ||||||||||
| 24 | use Exporter::Tiny; | ||||||||||
| 25 | use Import::Into; | ||||||||||
| 26 | use Marlin (); | ||||||||||
| 27 | use Marlin::Role (); | ||||||||||
| 28 | use Marlin::Util -lexical, qw( true false ); | ||||||||||
| 29 | use Types::Common -lexical, -all; | ||||||||||
| 30 | |||||||||||
| 31 | use namespace::autoclean; | ||||||||||
| 32 | |||||||||||
| 33 | our @ISA = qw( Exporter::Tiny ); | ||||||||||
| 34 | our @EXPORT = qw( has extends with before after around fresh signature signature_for __FINALIZE__ ); | ||||||||||
| 35 | |||||||||||
| 36 | my %PACKAGES; | ||||||||||
| 37 | |||||||||||
| 38 | sub _exporter_validate_opts ( $me, $globals ) { | ||||||||||
| 39 | |||||||||||
| 40 | my $pkg = $globals->{into}; | ||||||||||
| 41 | my $kind = $globals->{__role} ? 'Marlin::Role' : 'Marlin'; | ||||||||||
| 42 | if ( $PACKAGES{$pkg} ) { | ||||||||||
| 43 | Marlin::Util::_croak "$pkg already uses $kind\::Antlers"; | ||||||||||
| 44 | } | ||||||||||
| 45 | elsif ( my $meta = Marlin->find_meta($pkg) ) { | ||||||||||
| 46 | Marlin::Util::_croak "$pkg already uses " . ( $meta->inhaled_from // ref $meta ); | ||||||||||
| 47 | } | ||||||||||
| 48 | else { | ||||||||||
| 49 | $PACKAGES{$pkg} = $kind; | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | $globals->{installer} ||= $me->_exporter_lexical_installer( $globals ); | ||||||||||
| 53 | |||||||||||
| 54 | experimental->import::into( $pkg, qw( signatures postderef lexical_subs ) ); | ||||||||||
| 55 | feature->import::into( $pkg, qw( current_sub evalbytes fc say state unicode_eval unicode_strings ) ); | ||||||||||
| 56 | strict->import::into( $pkg ); | ||||||||||
| 57 | warnings->import::into( $pkg ); | ||||||||||
| 58 | Marlin::Util->import::into( $pkg, -lexical, -all ); | ||||||||||
| 59 | Types::Common->import::into( $pkg, -lexical, -all, qw( !signature !signature_for ) ); | ||||||||||
| 60 | namespace::autoclean->import::into( $pkg ); | ||||||||||
| 61 | |||||||||||
| 62 | my @plugins = do { | ||||||||||
| 63 | my $tmp = Exporter::Tiny::mkopt( | ||||||||||
| 64 | is_ArrayRef( $globals->{x} ) ? $globals->{x} : | ||||||||||
| 65 | is_Str( $globals->{x} ) ? [ $globals->{x} ] : | ||||||||||
| 66 | is_Defined( $globals->{x} ) ? Marlin::Util::_croak("Invalid value for 'x'") : [] | ||||||||||
| 67 | ); | ||||||||||
| 68 | $_->[0] =~ s/^:/Marlin::X::/s for $tmp->@*; | ||||||||||
| 69 | $tmp->@*; | ||||||||||
| 70 | }; | ||||||||||
| 71 | |||||||||||
| 72 | my $args = $globals->{MARLIN} = { | ||||||||||
| 73 | caller => $pkg, | ||||||||||
| 74 | this => $pkg, | ||||||||||
| 75 | parents => [], | ||||||||||
| 76 | roles => [], | ||||||||||
| 77 | attributes => [], | ||||||||||
| 78 | plugins => \@plugins, | ||||||||||
| 79 | delayed => [], | ||||||||||
| 80 | strict => !( $globals->{sloppy} ), | ||||||||||
| 81 | constructor => $globals->{constructor} // 'new', | ||||||||||
| 82 | modifiers => false, # export our own versions! | ||||||||||
| 83 | }; | ||||||||||
| 84 | my $mods = $globals->{MODIFY} = []; | ||||||||||
| 85 | |||||||||||
| 86 | my $finalize = $globals->{FINALIZE} = sub () { | ||||||||||
| 87 | state $finalized = 0; | ||||||||||
| 88 | return if $finalized++; | ||||||||||
| 89 | my $marlin = $kind->new( $args ); | ||||||||||
| 90 | $marlin->store_meta; | ||||||||||
| 91 | $marlin->do_setup; | ||||||||||
| 92 | install_modifier( $pkg, $_->@* ) for $mods->@*; | ||||||||||
| 93 | }; | ||||||||||
| 94 | |||||||||||
| 95 | &after_runtime( $finalize ); | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub _generate_has ( $me, $name, $value, $globals ) { | ||||||||||
| 99 | return sub ( $names, @spec ) { | ||||||||||
| 100 | |||||||||||
| 101 | is_ArrayRef $names | ||||||||||
| 102 | or $names = [ $names ]; | ||||||||||
| 103 | assert_Str $_ for $names->@*; | ||||||||||
| 104 | |||||||||||
| 105 | my $spec = ( @spec == 0 ) ? {} : ( @spec == 1 ) ? $spec[0] : { @spec }; | ||||||||||
| 106 | if ( is_Object $spec and $spec->DOES('Type::API::Constraint') ) { | ||||||||||
| 107 | my $tc = $spec; | ||||||||||
| 108 | $spec = { | ||||||||||
| 109 | isa => $tc, | ||||||||||
| 110 | coerce => !!( $tc->DOES('Type::API::Constraint::Coercible') and $tc->has_coercion ), | ||||||||||
| 111 | }; | ||||||||||
| 112 | } | ||||||||||
| 113 | elsif ( is_CodeRef $spec ) { | ||||||||||
| 114 | $spec = { | ||||||||||
| 115 | lazy => !!1, | ||||||||||
| 116 | builder => $spec, | ||||||||||
| 117 | }; | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | for my $name ( $names->@* ) { | ||||||||||
| 121 | my $default_init_arg = exists( $spec->{constant} ) ? undef : $name; | ||||||||||
| 122 | push $globals->{MARLIN}{attributes}->@*, { | ||||||||||
| 123 | is => 'ro', | ||||||||||
| 124 | init_arg => $default_init_arg, | ||||||||||
| 125 | $spec->%*, | ||||||||||
| 126 | slot => $name, | ||||||||||
| 127 | }; | ||||||||||
| 128 | } | ||||||||||
| 129 | }; | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | sub _generate_extends ( $me, $name, $value, $globals ) { | ||||||||||
| 133 | return sub ( @packages ) { | ||||||||||
| 134 | |||||||||||
| 135 | assert_Str $_ for @packages; | ||||||||||
| 136 | |||||||||||
| 137 | push $globals->{MARLIN}{parents}->@*, | ||||||||||
| 138 | map [ split /\s+/ ], | ||||||||||
| 139 | @packages; | ||||||||||
| 140 | }; | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | sub _generate_with ( $me, $name, $value, $globals ) { | ||||||||||
| 144 | return sub ( @packages ) { | ||||||||||
| 145 | |||||||||||
| 146 | assert_Str $_ for @packages; | ||||||||||
| 147 | |||||||||||
| 148 | push $globals->{MARLIN}{roles}->@*, | ||||||||||
| 149 | map [ split /\s+/ ], | ||||||||||
| 150 | @packages; | ||||||||||
| 151 | }; | ||||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | sub _generate_signature ( $me, $name, $value, $globals ) { | ||||||||||
| 155 | return sub { | ||||||||||
| 156 | my ( %opts ) = @_; | ||||||||||
| 157 | $opts{method} = 1 if !exists $opts{method}; | ||||||||||
| 158 | $opts{package} = $globals->{into} if !exists $opts{package}; | ||||||||||
| 159 | @_ = ( %opts ); | ||||||||||
| 160 | goto \&Type::Params::signature; | ||||||||||
| 161 | }; | ||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | sub _generate_signature_for ( $me, $name, $value, $globals ) { | ||||||||||
| 165 | return sub { | ||||||||||
| 166 | my ( $function, %opts ) = @_; | ||||||||||
| 167 | $opts{method} = 1 if !exists $opts{method}; | ||||||||||
| 168 | $opts{package} = $globals->{into} if !exists $opts{package}; | ||||||||||
| 169 | @_ = ( $function, %opts ); | ||||||||||
| 170 | goto \&Type::Params::signature_for; | ||||||||||
| 171 | }; | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | sub _generate_before ( $me, $name, $value, $globals ) { | ||||||||||
| 175 | return $me->_for_cmm( before => $globals ); | ||||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | sub _generate_after ( $me, $name, $value, $globals ) { | ||||||||||
| 179 | return $me->_for_cmm( after => $globals ); | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | sub _generate_around ( $me, $name, $value, $globals ) { | ||||||||||
| 183 | return $me->_for_cmm( around => $globals ); | ||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | sub _generate_fresh ( $me, $name, $value, $globals ) { | ||||||||||
| 187 | return $me->_for_cmm( fresh => $globals ); | ||||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | sub _for_cmm ( $me, $kind, $globals ) { | ||||||||||
| 191 | return sub ( @names ) { | ||||||||||
| 192 | |||||||||||
| 193 | my $coderef = pop @names; | ||||||||||
| 194 | assert_CodeRef $coderef; | ||||||||||
| 195 | |||||||||||
| 196 | push $globals->{MODIFY}->@*, [ $kind, @names, $coderef ]; | ||||||||||
| 197 | }; | ||||||||||
| 198 | } | ||||||||||
| 199 | |||||||||||
| 200 | sub _generate___FINALIZE__ ( $me, $name, $value, $globals ) { | ||||||||||
| 201 | return $globals->{FINALIZE}; | ||||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | __PACKAGE__ | ||||||||||
| 205 | __END__ | ||||||||||