From: David Leadbeater Date: Wed, 12 Jan 2011 18:10:40 +0000 (+0000) Subject: Make data actually work X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fdg%2Fdata;p=p5sagit%2FApp-FatPacker.git Make data actually work The main moves into a fatpacked file too, this is because 'tell DATA' can't work during BEGIN blocks, as perl hasn't seen the __DATA__ to create the filehandle. I'm not too keen on this, it probably needs something like extracting the pod from main so that perldoc fatpacked-file works correctly, it breaks current cpanm as that relies on 'caller' returning false in main, etc. --- diff --git a/lib/App/FatPacker.pm b/lib/App/FatPacker.pm index dc2ad35..14d4931 100644 --- a/lib/App/FatPacker.pm +++ b/lib/App/FatPacker.pm @@ -186,15 +186,14 @@ sub generate_file_hash_style { my $end = stripspace <<' END_END'; s/^ //mg for values %fatpacked; - unshift @INC, sub { - if (my $fat = $fatpacked{$_[1]}) { - open my $fh, '<', \$fat - or die "FatPacker error loading $_[1] (could be a perl installation issue?)"; - return $fh; - } - return - }; - + unshift @INC, sub { + if (my $fat = $fatpacked{$_[1]}) { + open my $fh, '<', \$fat + or die "FatPacker error loading $_[1] (could be a perl installation issue?)"; + return $fh; + } + return + }; } # END OF FATPACK CODE END_END my @segments = map { @@ -210,9 +209,15 @@ sub generate_file_hash_style { sub generate_file_data_style { my($files, $main) = @_; + { + open my $main_fh, '<', $main or die "Can't open '$main': $!"; + $files->{"fatpacked-main"} = join "", <$main_fh>; + } + my $start = stripspace <<' END_START'; # This chunk of stuff was generated by App::FatPacker. Do not edit. - my %fatpacked = ( + { + my %fatpacked = ( END_START my @segments = map { @@ -225,30 +230,29 @@ sub generate_file_data_style { my $fatpack_data; my $start_idx = 0; for my $segment (@segments) { - $fatpack_data .= " ".perlstring($segment->[0])." => [$start_idx, $segment->[1]],\n"; + $fatpack_data .= " ".perlstring($segment->[0])." => [$start_idx, $segment->[1]],\n"; $start_idx += $segment->[1]; } my $end = stripspace <<' END_END'; - ); - - my $data_pos = tell DATA; - unshift @INC, sub { - if(my $fat = $fatpacked{$_[1]}) { - seek DATA, $data_pos + $fat->[0], 0; - local $/ = \$fat->[1]; - open my $fh, '<', \scalar ; - return $fh; - } - }; + ); + my $data_pos = tell DATA; + unshift @INC, sub { + if(my $fat = $fatpacked{$_[1]}) { + seek DATA, $data_pos + $fat->[0], 0; + local $/ = \$fat->[1]; + open my $fh, '<', \scalar ; + return $fh; + } + }; + } # END OF FATPACK CODE - END_END - open my $main_fh, '<', $main or die "Can't open '$main': $!"; + do 'fatpacked-main' or die $@; + END_END print join "\n", $start, $fatpack_data, $end; - print join "", <$main_fh>, "\n"; print join "\n", "__DATA__", join "", map ${$_->[2]}, @segments; }