| 1 | package MOP4Import::Pairs; | ||||||||||||||
| 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_opts m4i_args/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 22 | ▶ | use MOP4Import::Util qw/terse_dump/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 22 | ▶ | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | sub dispatch_pairs_as_declare { | ||||||||||||||
| 13 | 69 | ▶ | (my $myPack, my $pragma, my Opts $opts, my (@pairs)) = @_; | ||||||||||||
|
Subroutine
dispatch_pairs_as_declare: called CC=8 SCAR=22.2
Pod
documented
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | # | ||||||||||||||
| 16 | # Process leading non-pair pragmas. (ARRAY and -pragma) | ||||||||||||||
| 17 | # | ||||||||||||||
| 18 | 69 | ▶ | while (@pairs) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 68 | ▶ | if (ref $pairs[0] eq 'CODE') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 20 | 0 | (shift @pairs)->($myPack, $opts); | |||||||||||||
| 21 | } elsif ($pairs[0] =~ /^-([A-Za-z]\w*)$/) { | ||||||||||||||
| 22 | 20 | shift @pairs; | |||||||||||||
| 23 | 20 | $myPack->dispatch_declare_pragma($opts, $1); | |||||||||||||
| 24 | } else { | ||||||||||||||
| 25 | 48 | last; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | } | ||||||||||||||
| 28 | |||||||||||||||
| 29 | 69 | ▶ | unless (@pairs % 2 == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 0 | croak "Odd number of arguments!: ".terse_dump(\@pairs); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | 69 | ▶ | my $sub = $myPack->can("declare_$pragma") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | or croak "Unknown declare pragma: $pragma"; | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 69 | ▶ | while (my ($name, $speclist) = splice @pairs, 0, 2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | 49 | ▶ | if (defined $name) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 49 | print STDERR " dispatching 'declare_$pragma' for pair(" | |||||||||||||
| 40 | , terse_dump($name, $speclist) | ||||||||||||||
| 41 | , $myPack->m4i_file_line_of($opts), "\n" if DEBUG; | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 49 | $sub->($myPack, $opts, $name, @$speclist); | |||||||||||||
| 44 | } else { | ||||||||||||||
| 45 | 0 | print STDERR " fallback to dispatch_declare for pair of undef => " | |||||||||||||
| 46 | , terse_dump($speclist) | ||||||||||||||
| 47 | , $myPack->m4i_file_line_of($opts), "\n" if DEBUG; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | $myPack->dispatch_declare($opts, @$speclist); | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 22 | 1; | |||||||||||||
| 55 | |||||||||||||||
| 56 | =head1 NAME | ||||||||||||||
| 57 | |||||||||||||||
| 58 | MOP4Import::Pairs - pragma dispatcher for name => [@pragma]... style protocol | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||||||
| 61 | |||||||||||||||
| 62 | package YourExporter; | ||||||||||||||
| 63 | use MOP4Import::Pairs -as_base, qw/Opts m4i_opts/; | ||||||||||||||
| 64 | |||||||||||||||
| 65 | sub import { | ||||||||||||||
| 66 | my $myPack = shift; | ||||||||||||||
| 67 | |||||||||||||||
| 68 | my Opts $opts = m4i_opts([caller]); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | # Invoke declare_foobar for each $name => $pragmas pairs. | ||||||||||||||
| 71 | $myPack->dispatch_pairs_as_declare(foobar => $opts, @_); | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | sub declare_foobar { | ||||||||||||||
| 75 | (my $myPack, my Opts $opts, my ($name, @pragmas)) = m4i_args(@_); | ||||||||||||||
| 76 | ... | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | #----------------------------------- | ||||||||||||||
| 80 | # Then you can use above module in other script like following | ||||||||||||||
| 81 | #----------------------------------- | ||||||||||||||
| 82 | |||||||||||||||
| 83 | use YourExporter ( | ||||||||||||||
| 84 | xxx => [1..3], | ||||||||||||||
| 85 | yyy => [4..6] | ||||||||||||||
| 86 | ); | ||||||||||||||
| 87 | |||||||||||||||
| 88 | # Above is equivalent of following: | ||||||||||||||
| 89 | |||||||||||||||
| 90 | BEGIN { | ||||||||||||||
| 91 | YourExporter->declare_foobar(__PACKAGE__, xxx, 1..3); | ||||||||||||||
| 92 | YourExporter->declare_foobar(__PACKAGE__, yyy, 4..6); | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||||||
| 96 | |||||||||||||||
| 97 | This module provides C<< name => [@pragma_list] >> style dispatcher of | ||||||||||||||
| 98 | L<MOP4Import::Declare> pragmas. Mainly used from L<MOP4Import::Types>. | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head2 "MetaObject Protocol for Import" in this module | ||||||||||||||
| 101 | |||||||||||||||
| 102 | This module does not provide "import" itself. | ||||||||||||||
| 103 | Instead, this provides a helper method C<dispatch_pairs_as_declare> | ||||||||||||||
| 104 | to implement "import". | ||||||||||||||
| 105 | |||||||||||||||
| 106 | =head1 METHODS | ||||||||||||||
| 107 | |||||||||||||||
| 108 | =head2 dispatch_pairs_as_declare($pragma, $opts, @name_pragmalist_pairs) | ||||||||||||||
| 109 | X<dispatch_pairs_as_declare> | ||||||||||||||
| 110 | |||||||||||||||
| 111 | This basically does following: | ||||||||||||||
| 112 | |||||||||||||||
| 113 | sub dispatch_pairs_as_declare { | ||||||||||||||
| 114 | (my $myPack, my $pragma, my Opts $opts, my (@pairs)) = @_; | ||||||||||||||
| 115 | |||||||||||||||
| 116 | my $method = "declare_$pragma"; | ||||||||||||||
| 117 | |||||||||||||||
| 118 | while (my ($typename, $pragma_list) = splice @pairs, 0, 2) { | ||||||||||||||
| 119 | |||||||||||||||
| 120 | $myPack->$method($opts, $typename, @$pragma_list); | ||||||||||||||
| 121 | |||||||||||||||
| 122 | } | ||||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||
| 126 | |||||||||||||||
| 127 | Kobayashi, Hiroaki E<lt>hkoba@cpan.orgE<gt> | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head1 LICENSE | ||||||||||||||
| 130 | |||||||||||||||
| 131 | This library is free software; you can redistribute it and/or modify | ||||||||||||||
| 132 | it under the same terms as Perl itself. | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =cut | ||||||||||||||