| 1 | package MOP4Import::Opts; | ||
| 2 | use strict; | ||
| 3 | use warnings qw(FATAL all NONFATAL misc); | ||
| 4 | use Carp; | ||
| 5 | use Exporter qw/import/; | ||
| 6 | use overload '""' => 'as_string'; | ||
| 7 | |||
| 8 | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||
| 9 | BEGIN { | ||
| 10 | print STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2 | ||
| 11 | } | ||
| 12 | |||
| 13 | use MOP4Import::Util qw/globref/; | ||
| 14 | |||
| 15 | use fields | ||
| 16 | ( | ||
| 17 | # caller() of import in usual case. | ||
| 18 | 'callpack' | ||
| 19 | |||
| 20 | # Where to export. Always defined. | ||
| 21 | , 'destpkg' | ||
| 22 | |||
| 23 | # What to define. Optional. | ||
| 24 | , 'objpkg' | ||
| 25 | |||
| 26 | # What to inherit. Optional. | ||
| 27 | , 'basepkg' | ||
| 28 | |||
| 29 | # Used in MOP4Import::Types::Extend and MOP4Import::Declare::Type | ||
| 30 | , 'extending' | ||
| 31 | |||
| 32 | # original caller info. This may be empty for faked m4i_opts() | ||
| 33 | , 'caller' | ||
| 34 | |||
| 35 | # closures which are called at the each end of MOP4Import handling. | ||
| 36 | , 'delayed_tasks' | ||
| 37 | |||
| 38 | # default value for json_type. 'string' if not specified | ||
| 39 | , 'default_json_type' | ||
| 40 | |||
| 41 | # Cache to store and keep heavy computation results between pragmas. | ||
| 42 | , 'stash' | ||
| 43 | |||
| 44 | , qw/filename line/ | ||
| 45 | ); | ||
| 46 | |||
| 47 | use MOP4Import::Util; | ||
| 48 | |||
| 49 | #======================================== | ||
| 50 | |||
| 51 | sub Opts () {__PACKAGE__} | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my ($pack, %opts) = @_; | ||
| 55 | |||
| 56 | my Opts $opts = fields::new($pack); | ||
| 57 | |||
| 58 | if (my $caller = delete $opts{caller}) { | ||
| 59 | $opts->{caller} = $caller; | ||
| 60 | ($opts->{callpack}, $opts->{filename}, $opts->{line}) | ||
| 61 | = ref $caller ? @$caller : ($caller, '', ''); | ||
| 62 | } | ||
| 63 | |||
| 64 | $opts->{delayed_tasks} = []; | ||
| 65 | |||
| 66 | $opts->{$_} = $opts{$_} for keys %opts; | ||
| 67 | |||
| 68 | $opts->{objpkg} = $opts->{destpkg} = $opts->{callpack}; | ||
| 69 | |||
| 70 | $opts; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub take_hash_maybe { | ||
| 74 | (my Opts $opts, my $list) = @_; | ||
| 75 | |||
| 76 | return $opts unless @$list and ref $list->[0] eq 'HASH'; | ||
| 77 | |||
| 78 | my $o = shift @$list; | ||
| 79 | |||
| 80 | $opts->{$_} = $o->{$_} for keys %$o; | ||
| 81 | |||
| 82 | $opts; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Should I use Clone::clone? | ||
| 86 | sub clone { | ||
| 87 | (my Opts $old) = @_; | ||
| 88 | my Opts $new = fields::new(ref $old); | ||
| 89 | %$new = %$old; | ||
| 90 | $new; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub with_destpkg { my Opts $new = clone($_[0]); $new->{destpkg} = $_[1]; $new } | ||
| 94 | sub with_objpkg { my Opts $new = clone($_[0]); $new->{objpkg} = $_[1]; $new } | ||
| 95 | sub with_basepkg { my Opts $new = clone($_[0]); $new->{basepkg} = $_[1]; $new } | ||
| 96 | |||
| 97 | # XXX: Not extensible. but how? | ||
| 98 | sub m4i_opts { | ||
| 99 | my ($arg) = @_; | ||
| 100 | if (not ref $arg) { | ||
| 101 | # Fake Opts from string. | ||
| 102 | Opts->new(caller => $arg); | ||
| 103 | |||
| 104 | } elsif (UNIVERSAL::isa($arg, Opts)) { | ||
| 105 | # Pass through. | ||
| 106 | $arg | ||
| 107 | |||
| 108 | } elsif (ref $arg eq 'ARRAY') { | ||
| 109 | # Shorthand of MOP4Import::Opts->new(caller => [caller]). | ||
| 110 | Opts->new(caller => $arg); | ||
| 111 | |||
| 112 | } elsif (ref $arg eq 'HASH' and m4i_opts_alike($arg)) { | ||
| 113 | # pass-thru when m4i_opts-compatible plain HASH is given. | ||
| 114 | $arg; | ||
| 115 | |||
| 116 | } else { | ||
| 117 | Carp::croak("Unknown argument! ". MOP4Import::Util::terse_dump($arg)); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub m4i_opts_alike { | ||
| 122 | my ($arg) = @_; | ||
| 123 | not grep {not exists $arg->{$_}} | ||
| 124 | qw(caller callpack filename line) | ||
| 125 | } | ||
| 126 | |||
| 127 | sub m4i_args { | ||
| 128 | ($_[0], m4i_opts($_[1]), @_[2..$#_]); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub m4i_fake_opts { | ||
| 132 | my ($fakedCallpack) = @_; | ||
| 133 | (undef, my (@callerTail)) = caller; | ||
| 134 | Opts->new(caller => [$fakedCallpack, @callerTail]); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub as_string { | ||
| 138 | (my Opts $opts) = @_; | ||
| 139 | $opts->{callpack}; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Provide field getters. | ||
| 143 | foreach my $field (keys our %FIELDS) { | ||
| 144 | *{globref(Opts, $field)} = sub {shift->$field()}; | ||
| 145 | } | ||
| 146 | |||
| 147 | our @EXPORT = qw/ | ||
| 148 | Opts | ||
| 149 | m4i_args | ||
| 150 | /; | ||
| 151 | our @EXPORT_OK = (@EXPORT, MOP4Import::Util::function_names | ||
| 152 | (matching => qr/^(with_|m4i_)/)); | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | =head1 NAME | ||
| 157 | |||
| 158 | MOP4Import::Opts - Object to encapsulate caller() record | ||
| 159 | |||
| 160 | =head1 SYNOPSIS | ||
| 161 | |||
| 162 | # To import the type 'Opts' and m4i api functions. | ||
| 163 | use MOP4Import::Opts; | ||
| 164 | |||
| 165 | # To create an instance of MOP4Import::Opts. | ||
| 166 | sub import { | ||
| 167 | ... | ||
| 168 | my Opts $opts = m4i_opts([caller]); | ||
| 169 | ... | ||
| 170 | } | ||
| 171 | |||
| 172 | # To extract MOP4Import::Opts safely from pragma args. | ||
| 173 | sub declare_foo { | ||
| 174 | (my $myPack, my Opts $opts, my (@args)) = m4i_args(@_); | ||
| 175 | ... | ||
| 176 | } | ||
| 177 | |||
| 178 | =head1 DESCRIPTION | ||
| 179 | |||
| 180 | This hash object encapsulates L<caller()|perlfunc/caller> info | ||
| 181 | and other parameters for L<MOP4Import::Declare> family. | ||
| 182 | |||
| 183 | =head1 ATTRIBUTES | ||
| 184 | |||
| 185 | =over 4 | ||
| 186 | |||
| 187 | =item callpack | ||
| 188 | |||
| 189 | L<scalar caller()|perlfunc/caller> of import in usual case. | ||
| 190 | |||
| 191 | =item destpkg | ||
| 192 | |||
| 193 | Where to export. Always defined. | ||
| 194 | |||
| 195 | =item objpkg | ||
| 196 | |||
| 197 | What to define. Optional. | ||
| 198 | |||
| 199 | =item basepkg | ||
| 200 | |||
| 201 | What to inherit. Optional. | ||
| 202 | |||
| 203 | =item extending | ||
| 204 | |||
| 205 | Used in MOP4Import::Types::Extend and MOP4Import::Declare::Type | ||
| 206 | |||
| 207 | =item caller | ||
| 208 | |||
| 209 | Original L<caller()|perlfunc/caller> info. | ||
| 210 | This may be empty for faked m4i_opts(). | ||
| 211 | |||
| 212 | =item filename | ||
| 213 | |||
| 214 | =item line | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head1 FUNCTIONS | ||
| 219 | |||
| 220 | =head2 m4i_args | ||
| 221 | |||
| 222 | This function converts C<$_[1]> by L<m4i_opts> and returns whole C<@_>. | ||
| 223 | |||
| 224 | (my $myPack, my Opts $opts, my (@args)) = m4i_args(@_); | ||
| 225 | |||
| 226 | =head2 m4i_opts | ||
| 227 | |||
| 228 | my Opts $opts = m4i_opts([caller]); | ||
| 229 | |||
| 230 | my Opts $opts = m4i_opts(scalar caller); # string is ok too. | ||
| 231 | |||
| 232 | =head1 METHODS | ||
| 233 | |||
| 234 | =head2 as_string | ||
| 235 | |||
| 236 | $opts->as_string; | ||
| 237 | |||
| 238 | "$opts"; # Same as above. | ||
| 239 | |||
| 240 | =head1 AUTHOR | ||
| 241 | |||
| 242 | Kobayashi, Hiroaki E<lt>hkoba@cpan.orgE<gt> | ||
| 243 | |||
| 244 | =head1 LICENSE | ||
| 245 | |||
| 246 | This library is free software; you can redistribute it and/or modify | ||
| 247 | it under the same terms as Perl itself. | ||
| 248 | |||
| 249 | =cut | ||
| 250 |