| 1 | package App::ForExample::ModuleEmbedCatalog; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | { | ||
| 7 | my $catalog; | ||
| 8 | sub extract { | ||
| 9 | my $self = shift; | ||
| 10 | my $module = shift; | ||
| 11 | my $end = shift; | ||
| 12 | |||
| 13 | $end = '__ASSET__' unless defined $end; | ||
| 14 | $end = qr/^\Q$end\E$/ unless ref $end eq 'Regexp'; | ||
| 15 | |||
| 16 | my $handle = "${module}::DATA"; | ||
| 17 | |||
| 18 | return $catalog ||= do { | ||
| 19 | my %catalog; | ||
| 20 | my ($path, $content); | ||
| 21 | while (<$handle>) { | ||
| 22 | if ( ! $path ) { | ||
| 23 | next unless m/\S/; | ||
| 24 | chomp; $path = $_; | ||
| 25 | $content = ''; | ||
| 26 | } | ||
| 27 | elsif ( $_ =~ $end ) { | ||
| 28 | my $__ = $content; | ||
| 29 | $catalog{$path} = \$__; | ||
| 30 | undef $path; | ||
| 31 | undef $content; | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | $content .= $_; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | \%catalog; | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | 1; |