| 1 | package | ||
| 2 | XS::Install::FrozenShit::Typemaps::Cmd; | ||
| 3 | use 5.006001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | our $VERSION = '3.57'; | ||
| 7 | |||
| 8 | use XS::Install::FrozenShit::Typemaps; | ||
| 9 | |||
| 10 | require Exporter; | ||
| 11 | |||
| 12 | our @ISA = qw(Exporter); | ||
| 13 | our @EXPORT = qw(embeddable_typemap); | ||
| 14 | our %EXPORT_TAGS = (all => \@EXPORT); | ||
| 15 | |||
| 16 | sub embeddable_typemap { | ||
| 17 | my @tms = @_; | ||
| 18 | |||
| 19 | # Get typemap objects | ||
| 20 | my @tm_objs = map [$_, _intuit_typemap_source($_)], @tms; | ||
| 21 | |||
| 22 | # merge or short-circuit | ||
| 23 | my $final_tm; | ||
| 24 | if (@tm_objs == 1) { | ||
| 25 | # just one, merge would be pointless | ||
| 26 | $final_tm = shift(@tm_objs)->[1]; | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | # multiple, need merge | ||
| 30 | $final_tm = XS::Install::FrozenShit::Typemaps->new; | ||
| 31 | foreach my $other_tm (@tm_objs) { | ||
| 32 | my ($tm_ident, $tm_obj) = @$other_tm; | ||
| 33 | eval { | ||
| 34 | $final_tm->merge(typemap => $tm_obj); | ||
| 35 | 1 | ||
| 36 | } or do { | ||
| 37 | my $err = $@ || 'Zombie error'; | ||
| 38 | die "Failed to merge typ"; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | # stringify for embedding | ||
| 44 | return $final_tm->as_embedded_typemap(); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _load_module { | ||
| 48 | my $name = shift; | ||
| 49 | return eval "require $name; 1"; | ||
| 50 | } | ||
| 51 | |||
| 52 | SCOPE: { | ||
| 53 | my %sources = ( | ||
| 54 | module => sub { | ||
| 55 | my $ident = shift; | ||
| 56 | my $tm; | ||
| 57 | if (/::/) { # looks like FQ module name, try that first | ||
| 58 | foreach my $module ($ident, "XS::Install::FrozenShit::Typemaps::$ident") { | ||
| 59 | if (_load_module($module)) { | ||
| 60 | eval { $tm = $module->new } | ||
| 61 | and return $tm; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | foreach my $module ("XS::Install::FrozenShit::Typemaps::$ident", "$ident") { | ||
| 67 | if (_load_module($module)) { | ||
| 68 | eval { $tm = $module->new } | ||
| 69 | and return $tm; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return(); | ||
| 74 | }, | ||
| 75 | file => sub { | ||
| 76 | my $ident = shift; | ||
| 77 | return unless -e $ident and -r _; | ||
| 78 | return XS::Install::FrozenShit::Typemaps->new(file => $ident); | ||
| 79 | }, | ||
| 80 | ); | ||
| 81 | # Try to find typemap either from module or file | ||
| 82 | sub _intuit_typemap_source { | ||
| 83 | my $identifier = shift; | ||
| 84 | |||
| 85 | my @locate_attempts; | ||
| 86 | if ($identifier =~ /::/ || $identifier !~ /[^\w_]/) { | ||
| 87 | @locate_attempts = qw(module file); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | @locate_attempts = qw(file module); | ||
| 91 | } | ||
| 92 | |||
| 93 | foreach my $source (@locate_attempts) { | ||
| 94 | my $tm = $sources{$source}->($identifier); | ||
| 95 | return $tm if defined $tm; | ||
| 96 | } | ||
| 97 | |||
| 98 | die "Unable to find typemap for '$identifier': " | ||
| 99 | . "Tried to load both as file or module and failed.\n"; | ||
| 100 | } | ||
| 101 | } # end SCOPE | ||
| 102 | |||
| 103 | =head1 NAME | ||
| 104 | |||
| 105 | XS::Install::FrozenShit::Typemaps::Cmd - Quick commands for handling typemaps | ||
| 106 | |||
| 107 | =head1 SYNOPSIS | ||
| 108 | |||
| 109 | From XS: | ||
| 110 | |||
| 111 | INCLUDE_COMMAND: $^X -MXS::Install::FrozenShit::Typemaps::Cmd \ | ||
| 112 | -e "print embeddable_typemap(q{Excommunicated})" | ||
| 113 | |||
| 114 | Loads C<XS::Install::FrozenShit::Typemaps::Excommunicated>, instantiates an object, | ||
| 115 | and dumps it as an embeddable typemap for use directly in your XS file. | ||
| 116 | |||
| 117 | =head1 DESCRIPTION | ||
| 118 | |||
| 119 | This is a helper module for L<XS::Install::FrozenShit::Typemaps> for quick | ||
| 120 | one-liners, specifically for inclusion of shared typemaps | ||
| 121 | that live on CPAN into an XS file (see SYNOPSIS). | ||
| 122 | |||
| 123 | For this reason, the following functions are exported by default: | ||
| 124 | |||
| 125 | =head1 EXPORTED FUNCTIONS | ||
| 126 | |||
| 127 | =head2 embeddable_typemap | ||
| 128 | |||
| 129 | Given a list of identifiers, C<embeddable_typemap> | ||
| 130 | tries to load typemaps from a file of the given name(s), | ||
| 131 | or from a module that is an C<XS::Install::FrozenShit::Typemaps> subclass. | ||
| 132 | |||
| 133 | Returns a string representation of the merged typemaps that can | ||
| 134 | be included verbatim into XS. Example: | ||
| 135 | |||
| 136 | print embeddable_typemap( | ||
| 137 | "Excommunicated", "XS::Install::FrozenShit::Typemaps::Basic", "./typemap" | ||
| 138 | ); | ||
| 139 | |||
| 140 | This will try to load a module C<XS::Install::FrozenShit::Typemaps::Excommunicated> | ||
| 141 | and use it as an C<XS::Install::FrozenShit::Typemaps> subclass. If that fails, it'll | ||
| 142 | try loading C<Excommunicated> as a module, if that fails, it'll try to | ||
| 143 | read a file called F<Excommunicated>. It'll work similarly for the | ||
| 144 | second argument, but the third will be loaded as a file first. | ||
| 145 | |||
| 146 | After loading all typemap files or modules, it will merge them in the | ||
| 147 | specified order and dump the result as an embeddable typemap. | ||
| 148 | |||
| 149 | =head1 SEE ALSO | ||
| 150 | |||
| 151 | L<XS::Install::FrozenShit::Typemaps> | ||
| 152 | |||
| 153 | L<perlxs> | ||
| 154 | |||
| 155 | =head1 AUTHOR | ||
| 156 | |||
| 157 | Steffen Mueller C<<smueller@cpan.org>> | ||
| 158 | |||
| 159 | =head1 COPYRIGHT & LICENSE | ||
| 160 | |||
| 161 | Copyright 2012 Steffen Mueller | ||
| 162 | |||
| 163 | This program is free software; you can redistribute it and/or | ||
| 164 | modify it under the same terms as Perl itself. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | 1; | ||
| 169 |