| 1 | package MOP4Import::Pairs; | ||
| 2 | use 5.010; | ||
| 3 | use strict; | ||
| 4 | use warnings qw(FATAL all NONFATAL misc); | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use MOP4Import::Declare -as_base, qw/Opts m4i_opts m4i_args/; | ||
| 8 | use MOP4Import::Util qw/terse_dump/; | ||
| 9 | |||
| 10 | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||
| 11 | |||
| 12 | sub dispatch_pairs_as_declare { | ||
| 13 | (my $myPack, my $pragma, my Opts $opts, my (@pairs)) = @_; | ||
| 14 | |||
| 15 | # | ||
| 16 | # Process leading non-pair pragmas. (ARRAY and -pragma) | ||
| 17 | # | ||
| 18 | while (@pairs) { | ||
| 19 | if (ref $pairs[0] eq 'CODE') { | ||
| 20 | (shift @pairs)->($myPack, $opts); | ||
| 21 | } elsif ($pairs[0] =~ /^-([A-Za-z]\w*)$/) { | ||
| 22 | shift @pairs; | ||
| 23 | $myPack->dispatch_declare_pragma($opts, $1); | ||
| 24 | } else { | ||
| 25 | last; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | unless (@pairs % 2 == 0) { | ||
| 30 | croak "Odd number of arguments!: ".terse_dump(\@pairs); | ||
| 31 | } | ||
| 32 | |||
| 33 | my $sub = $myPack->can("declare_$pragma") | ||
| 34 | or croak "Unknown declare pragma: $pragma"; | ||
| 35 | |||
| 36 | while (my ($name, $speclist) = splice @pairs, 0, 2) { | ||
| 37 | |||
| 38 | if (defined $name) { | ||
| 39 | print STDERR " dispatching 'declare_$pragma' for pair(" | ||
| 40 | , terse_dump($name, $speclist) | ||
| 41 | , $myPack->m4i_file_line_of($opts), "\n" if DEBUG; | ||
| 42 | |||
| 43 | $sub->($myPack, $opts, $name, @$speclist); | ||
| 44 | } else { | ||
| 45 | 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 | $myPack->dispatch_declare($opts, @$speclist); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | 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 |