| 1 | package Data::Embed::OneFileAsModule; | ||
| 2 | |||
| 3 | use Exporter qw< import >; | ||
| 4 | @EXPORT_OK = (qw< generate_module_from_file >); | ||
| 5 | @EXPORT = (); | ||
| 6 | %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use Log::Log4perl::Tiny qw< :easy :dead_if_first >; | ||
| 10 | |||
| 11 | our $VERSION = '0.32'; # make indexer happy | ||
| 12 | |||
| 13 | { | ||
| 14 | no strict 'refs'; | ||
| 15 | #__TEMPLATE_BEGIN__ | ||
| 16 | { | ||
| 17 | my $data = \*{__PACKAGE__ . '::DATA'}; | ||
| 18 | my $position = undef; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Carp; | ||
| 23 | use English qw< -no_match_vars >; | ||
| 24 | use Fcntl qw< :seek >; | ||
| 25 | |||
| 26 | sub get_fh { | ||
| 27 | $position = tell $data unless defined $position; | ||
| 28 | open my $fh, '<&', $data | ||
| 29 | or croak __PACKAGE__ . "::dup() for DATA: $OS_ERROR"; | ||
| 30 | seek $fh, $position, SEEK_SET; | ||
| 31 | return $fh; | ||
| 32 | } ## end sub get_fh | ||
| 33 | |||
| 34 | sub get_data { | ||
| 35 | my $fh = get_fh(); | ||
| 36 | binmode $fh; | ||
| 37 | |||
| 38 | # ensure we slurp all, whatever the context | ||
| 39 | local $/ = wantarray() ? $/ : undef; | ||
| 40 | return <$fh>; | ||
| 41 | } ## end sub get_data | ||
| 42 | } | ||
| 43 | #__TEMPLATE_END__ | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _get_output_fh { | ||
| 47 | my $output = shift; | ||
| 48 | my $binmode = shift || ':raw'; | ||
| 49 | |||
| 50 | # if no output is defined, we will return a scalar with data | ||
| 51 | if (!defined $output) { | ||
| 52 | my $buffer = ''; | ||
| 53 | open my $fh, '>', \$buffer | ||
| 54 | or LOGCROAK "open() on (scalar ref): $OS_ERROR"; | ||
| 55 | binmode $fh, $binmode; | ||
| 56 | return ($fh, \$buffer); | ||
| 57 | } ## end if (!defined $output) | ||
| 58 | |||
| 59 | # if filename is '-', use standard output | ||
| 60 | if ($output eq '-') { | ||
| 61 | open my $fh, '>&', \*STDOUT # dup()-ing | ||
| 62 | or LOGCROAK "dup(): $OS_ERROR"; | ||
| 63 | binmode $fh, $binmode; | ||
| 64 | return $fh; | ||
| 65 | } ## end if ($output eq '-') | ||
| 66 | |||
| 67 | my $ro = ref $output; | ||
| 68 | if (!$ro) { # output is a simple filename | ||
| 69 | open my $fh, '>', $output | ||
| 70 | or LOGCROAK "open('$output'): $OS_ERROR"; | ||
| 71 | binmode $fh, $binmode; | ||
| 72 | return $fh; | ||
| 73 | } ## end if (!$ro) | ||
| 74 | |||
| 75 | # so we have a reference here. | ||
| 76 | # if not a reference to a SCALAR, assume it's | ||
| 77 | # something that supports the filehandle interface | ||
| 78 | return $output if $ro ne 'SCALAR'; | ||
| 79 | |||
| 80 | # otherwise, open a handle to write in the scalar | ||
| 81 | open my $fh, '>', $output | ||
| 82 | or LOGCROAK "open('$output') (scalar ref): $OS_ERROR"; | ||
| 83 | binmode $fh, $binmode; | ||
| 84 | return $fh; | ||
| 85 | } ## end sub _get_output_fh | ||
| 86 | |||
| 87 | sub _get_input_fh { | ||
| 88 | my $args = shift; | ||
| 89 | |||
| 90 | return $args->{fh} if exists $args->{fh}; | ||
| 91 | |||
| 92 | if (defined $args->{filename}) { | ||
| 93 | open my $fh, '<', $args->{filename} | ||
| 94 | or LOGCROAK "open('$args->{filename}'): $OS_ERROR"; | ||
| 95 | binmode $fh; | ||
| 96 | return $fh; | ||
| 97 | } ## end if (defined $args->{filename...}) | ||
| 98 | |||
| 99 | if (defined $args->{dataref}) { | ||
| 100 | open my $fh, '<', $args->{dataref} | ||
| 101 | or LOGCROAK "open('$args->{dataref}') (scalar ref): $OS_ERROR"; | ||
| 102 | binmode $fh; | ||
| 103 | return $fh; | ||
| 104 | } ## end if (defined $args->{dataref...}) | ||
| 105 | |||
| 106 | if (defined $args->{data}) { | ||
| 107 | open my $fh, '<', \$args->{data} | ||
| 108 | or LOGCROAK "open() (scalar ref): $OS_ERROR"; | ||
| 109 | binmode $fh; | ||
| 110 | return $fh; | ||
| 111 | } ## end if (defined $args->{data...}) | ||
| 112 | |||
| 113 | LOGCROAK "no input source defined"; | ||
| 114 | return; # unreached | ||
| 115 | } ## end sub _get_input_fh | ||
| 116 | |||
| 117 | sub generate_module_from_file { | ||
| 118 | my %args = (scalar(@_) && ref($_[0])) ? %{$_[0]} : @_; | ||
| 119 | |||
| 120 | LOGCROAK 'no package name set' | ||
| 121 | unless defined $args{package}; | ||
| 122 | LOGCROAK "unsupported package name '$args{module}'" | ||
| 123 | unless $args{package} =~ m{\A (?: \w+) (:: \w+)* \z}mxs; | ||
| 124 | |||
| 125 | my $template_fh = get_fh(); | ||
| 126 | binmode $template_fh; | ||
| 127 | seek $template_fh, 0, SEEK_SET; | ||
| 128 | |||
| 129 | my $in_fh = _get_input_fh(\%args); | ||
| 130 | |||
| 131 | ($args{output} = 'lib/' . $args{package} . '.pm') =~ s{::}{/}gmxs | ||
| 132 | if $args{output_from_package}; | ||
| 133 | my ($out_fh, $outref) = _get_output_fh($args{output}, $args{binmode}); | ||
| 134 | |||
| 135 | # package name | ||
| 136 | print {$out_fh} "package $args{package};\n"; | ||
| 137 | |||
| 138 | # package code | ||
| 139 | my $seen_start; | ||
| 140 | INPUT: | ||
| 141 | while (<$template_fh>) { | ||
| 142 | if (!$seen_start) { | ||
| 143 | $seen_start = m{\A \#__TEMPLATE_BEGIN__ \s*\z}mxs; | ||
| 144 | next INPUT; | ||
| 145 | } | ||
| 146 | last INPUT if m{\A \#__TEMPLATE_END__ \s*\z}mxs; | ||
| 147 | print {$out_fh} $_; | ||
| 148 | } ## end INPUT: while (<$template_fh>) | ||
| 149 | |||
| 150 | # package code ending | ||
| 151 | print {$out_fh} "\n1;\n__DATA__\n"; | ||
| 152 | |||
| 153 | # file contents | ||
| 154 | while (!eof $in_fh) { | ||
| 155 | defined(my $nread = read $in_fh, my $buffer, 4096) | ||
| 156 | or LOGCROAK "read(): $OS_ERROR"; | ||
| 157 | last unless $nread; # paranoid | ||
| 158 | print {$out_fh} $buffer; | ||
| 159 | } ## end while (!eof $in_fh) | ||
| 160 | |||
| 161 | return $$outref if $outref; | ||
| 162 | return; | ||
| 163 | } ## end sub generate_module_from_file | ||
| 164 | |||
| 165 | 1; |