| 1 | package MOP4Import::Declare::Type; | ||||||||||||||
| 2 | 22 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 22 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 22 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 22 | ▶ | use MOP4Import::Declare -as_base, qw/Opts m4i_args m4i_opts/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 22 | ▶ | use MOP4Import::Pairs -as_base; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 22 | ▶ | use MOP4Import::Util qw/symtab globref ensure_symbol_has_array/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 22 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | sub default_exports { | ||||||||||||||
| 14 | 0 | ▶ | (my $myPack) = @_; | ||||||||||||
|
Subroutine
default_exports: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 15 | 0 | my $symtab = symtab($myPack); | |||||||||||||
| 16 | my $exportSym = $symtab->{EXPORT} | ||||||||||||||
| 17 | 0 | ▶ | or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | 0 | my $exportArray = *{$exportSym}{ARRAY} | |||||||||||||
| 19 | 0 | ▶ | or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 0 | @$exportArray | |||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | sub declare_type { | ||||||||||||||
| 24 | 49 | ▶ | (my $myPack, my Opts $opts, my ($name, @spec)) = m4i_args(@_); | ||||||||||||
|
Subroutine
declare_type: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 25 | |||||||||||||||
| 26 | 49 | ▶ | if ($opts->{extending}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 27 | 27 | ▶ | my $sub = $opts->{destpkg}->can($name) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | or croak "Can't find base class $name in parents of $opts->{destpkg}"; | ||||||||||||||
| 29 | 27 | unshift @spec, [fileless_base => $sub->()]; | |||||||||||||
| 30 | } elsif ($opts->{basepkg}) { | ||||||||||||||
| 31 | 3 | unshift @spec, [fileless_base => $opts->{basepkg}]; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | $myPack->declare___inner_class_in($opts | ||||||||||||||
| 35 | 49 | , $opts->{destpkg}, $name, @spec); | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | sub declare_extends { | ||||||||||||||
| 39 | 0 | ▶ | (my $myPack, my Opts $opts, my ($baseClassName)) = m4i_args(@_); | ||||||||||||
|
Subroutine
declare_extends: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | my $baseClass = do { | |||||||||||||
| 42 | 0 | ▶ | if (my $sub = $opts->{destpkg}->can($baseClassName)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 43 | 0 | $sub->() | |||||||||||||
| 44 | } elsif (MOP4Import::Util::maybe_fields_hash($baseClassName)) { | ||||||||||||||
| 45 | 0 | $baseClassName | |||||||||||||
| 46 | } else { | ||||||||||||||
| 47 | 0 | Carp::croak "Can't find base class $baseClassName in (parents of) $opts->{destpkg}"; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | }; | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 0 | $myPack->declare_fileless_base($opts, $baseClass); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | # | ||||||||||||||
| 55 | # Create a new class $extended, deriving from $callpack->SUPER::$extended, | ||||||||||||||
| 56 | # in $callpack. | ||||||||||||||
| 57 | # | ||||||||||||||
| 58 | sub declare_extend { | ||||||||||||||
| 59 | 0 | ▶ | (my $myPack, my Opts $opts, my ($extended, @spec)) = m4i_args(@_); | ||||||||||||
|
Subroutine
declare_extend: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | my $sub = $opts->{destpkg}->can($extended) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | or croak "Can't find base class $extended in parents of $opts->{destpkg}"; | ||||||||||||||
| 63 | |||||||||||||||
| 64 | $myPack->declare___inner_class_in($opts | ||||||||||||||
| 65 | 0 | , $opts->{destpkg}, $extended | |||||||||||||
| 66 | , [fileless_base => $sub->()] | ||||||||||||||
| 67 | , @spec); | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | sub declare___inner_class_in { | ||||||||||||||
| 71 | 49 | ▶ | (my $myPack, my Opts $opts, my ($destpkg, $name, @spec)) = m4i_args(@_); | ||||||||||||
|
Subroutine
declare___inner_class_in: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||
| 72 | |||||||||||||||
| 73 | 49 | my $innerClass = join("::", $destpkg, $name); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 49 | $myPack->declare_alias($opts, $name, $innerClass); | |||||||||||||
| 76 | |||||||||||||||
| 77 | 49 | ▶ | if (@spec) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | 49 | $myPack->dispatch_declare($opts->with_objpkg($innerClass) | |||||||||||||
| 79 | , @spec); | ||||||||||||||
| 80 | } else { | ||||||||||||||
| 81 | # Note: To make sure %FIELDS is defined. Without this we get: | ||||||||||||||
| 82 | # No such class Foo at (eval 45) line 1, near "(my Foo" | ||||||||||||||
| 83 | # | ||||||||||||||
| 84 | 0 | $myPack->declare_fields($opts->with_objpkg($innerClass)); | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | |||||||||||||||
| 87 | 49 | my $export = ensure_symbol_has_array(globref($destpkg, 'EXPORT')); | |||||||||||||
| 88 | 49 | ▶ | unless (grep {$_ eq $name} @$export) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | 49 | push @$export, $name; | |||||||||||||
| 90 | 49 | print STDERR " type $name is added to default exports of $destpkg\n" if DEBUG; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | sub declare_subtypes { | ||||||||||||||
| 95 | 0 | ▶ | (my $myPack, my Opts $opts, my @specs) = m4i_args(@_); | ||||||||||||
|
Subroutine
declare_subtypes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 96 | |||||||||||||||
| 97 | $myPack->dispatch_pairs_as_declare( | ||||||||||||||
| 98 | 0 | type => $opts->with_basepkg($opts->{objpkg}), | |||||||||||||
| 99 | @specs | ||||||||||||||
| 100 | ); | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 22 | 1; | |||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 NAME | ||||||||||||||
| 106 | |||||||||||||||
| 107 | MOP4Import::Declare::Type - inner-type related pragmas | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 SYNOPSIS | ||||||||||||||
| 110 | |||||||||||||||
| 111 | package MyApp; | ||||||||||||||
| 112 | use MOP4Import::Types | ||||||||||||||
| 113 | BaseUser => [ | ||||||||||||||
| 114 | [fields => qw/sessid/], | ||||||||||||||
| 115 | [subtypes => | ||||||||||||||
| 116 | Guest => [], | ||||||||||||||
| 117 | RegisteredUser => [[fields => qw/uid registered_at/]], | ||||||||||||||
| 118 | ] | ||||||||||||||
| 119 | ], | ||||||||||||||
| 120 | AdminUser => [ | ||||||||||||||
| 121 | [extends => 'RegisteredUser'], | ||||||||||||||
| 122 | [fields => qw/impersonation/], | ||||||||||||||
| 123 | ], | ||||||||||||||
| 124 | ; | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||||||
| 127 | |||||||||||||||
| 128 | This module implements inner-type related pragmas. | ||||||||||||||
| 129 | Usually used via L<MOP4Import::Types>. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 PRAGMAS | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =head2 [subtypes => ($typename => [@spec])...] | ||||||||||||||
| 134 | X<subtypes> | ||||||||||||||
| 135 | |||||||||||||||
| 136 | Declare multiple inner-types which are subtypes of currently-defining inner-type by C<< $typename => [@spec] >> pair list. | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head2 [extends => $typename] | ||||||||||||||
| 139 | X<extends> | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Set superclass for currently-defining inner-type to C<< $typename >>. | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =head2 (Internal) [type => $typename, @spec] | ||||||||||||||
| 144 | X<type> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | Declare inner-type C<$typename> based on given C<@spec>. | ||||||||||||||
| 147 | |||||||||||||||
| 148 | =head2 (Not recommended) [extend => $typename, @spec] | ||||||||||||||
| 149 | X<extend> | ||||||||||||||
| 150 | |||||||||||||||
| 151 | Declare extended version of C<$typename>, which is inherited from parent class, | ||||||||||||||
| 152 | based on given C<@spec>. | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =head1 AUTHOR | ||||||||||||||
| 155 | |||||||||||||||
| 156 | Kobayashi, Hiroaki E<lt>hkoba@cpan.orgE<gt> | ||||||||||||||
| 157 | |||||||||||||||
| 158 | =head1 LICENSE | ||||||||||||||
| 159 | |||||||||||||||
| 160 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 161 | it under the same terms as Perl itself. | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =cut | ||||||||||||||