| 1 | package PAR::StrippedPARL::Base; | ||
| 2 | use 5.008001; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.975'; | ||
| 6 | |||
| 7 | use File::Temp (); | ||
| 8 | use File::Spec; | ||
| 9 | use Cwd; | ||
| 10 | use Config; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | PAR::StrippedPARL::Base - Base class for the PARL data packages | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | # Please use one of the siblings of this class instead. | ||
| 19 | use base 'PAR::StrippedPARL::Base'; | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This class is internal to PAR. Do not use it outside of PAR. | ||
| 24 | |||
| 25 | This class is basically just a container for a static binary PAR loader | ||
| 26 | which doesn't include the PAR code like the F<parl> or F<parl.exe> | ||
| 27 | you are used to. If you're really curious, I'll tell you it is | ||
| 28 | just a copy of the F<myldr/static> (or F<myldr/static.exe>) file. | ||
| 29 | |||
| 30 | The data is appended during the C<make> phase of the PAR build process. | ||
| 31 | |||
| 32 | If the binary data isn't appended during the build process, the two class | ||
| 33 | methods will return the empty list. | ||
| 34 | |||
| 35 | =head1 CLASS METHODS | ||
| 36 | |||
| 37 | =head2 write_parl | ||
| 38 | |||
| 39 | Takes a file name as argument. Writes the raw binary data in | ||
| 40 | the package to the specified file and embeds the core modules | ||
| 41 | to produce a complete PAR loader (I<parl>). | ||
| 42 | |||
| 43 | Returns true on success or the empty list on failure. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | sub write_parl { | ||
| 48 | my $class = shift; | ||
| 49 | my $file = shift; | ||
| 50 | if (not defined $file) { | ||
| 51 | warn "${class}->write_parl() needs a file name as argument"; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | # write out to a temporary file first | ||
| 56 | my ($fh, $tfile) = File::Temp::tempfile( | ||
| 57 | "parlXXXX", SUFFIX => $Config{_exe}||'', TMPDIR => 1, UNLINK => 1); | ||
| 58 | close $fh; | ||
| 59 | |||
| 60 | if (not $class->write_raw($tfile)) { | ||
| 61 | unlink($tfile); | ||
| 62 | warn "Could not write temporary parl (class $class) to file '$tfile'"; | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | chmod(0755, $tfile); | ||
| 66 | |||
| 67 | # Use this to generate a real parl | ||
| 68 | my @libs = (); | ||
| 69 | for my $ilib ( @INC ) { | ||
| 70 | next if ref $ilib; | ||
| 71 | $ilib =~ s/\\$/\\\\/; | ||
| 72 | push(@libs, qq(-I$ilib) ); | ||
| 73 | } | ||
| 74 | |||
| 75 | system(Cwd::abs_path($tfile), @libs, qw( -q -B ), "-O$file"); | ||
| 76 | unless ($? == 0) { | ||
| 77 | warn "Failed to execute temporary parl (class $class) '$tfile': \$?=$?"; | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 get_raw | ||
| 84 | |||
| 85 | Returns the binary data attached to the data package. | ||
| 86 | |||
| 87 | Returns the empty list on failure. | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub get_raw { | ||
| 92 | my $class = shift; | ||
| 93 | my $pos = $class->_data_pos(); | ||
| 94 | if (not defined $pos) { | ||
| 95 | warn "${class}->_data_pos() did not return the original tell() position of the DATA file handle"; | ||
| 96 | return(); | ||
| 97 | } | ||
| 98 | my $sym; | ||
| 99 | { | ||
| 100 | # Is there a better way to do this? | ||
| 101 | no strict 'refs'; | ||
| 102 | $sym = \*{"${class}::DATA"}; | ||
| 103 | } | ||
| 104 | seek $sym, $pos, 0 or die $!; | ||
| 105 | binmode $sym; | ||
| 106 | local $/ = undef; | ||
| 107 | my $data = <$sym>; | ||
| 108 | $data =~ s/^\s*//; | ||
| 109 | my $binary = unpack 'u', $data; | ||
| 110 | return() if not defined $binary or $binary !~ /\S/; | ||
| 111 | return $binary; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 write_raw | ||
| 116 | |||
| 117 | Takes a file name as argument and writes the binary data to the file. | ||
| 118 | |||
| 119 | Returns true on success and the empty list on failure. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub write_raw { | ||
| 124 | my ($class, $file) = @_; | ||
| 125 | if (not defined $file) { | ||
| 126 | warn "${class}->write_raw() needs a file name as argument"; | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | my $binary = $class->get_raw(); | ||
| 130 | if (not defined $binary) { | ||
| 131 | warn "${class}->get_raw() did not return the raw binary data for a PAR loader"; | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | my $fh; | ||
| 136 | (open $fh, '>:raw', $file) | ||
| 137 | && (print $fh $binary) | ||
| 138 | && (close $fh) | ||
| 139 | or die "Could not write file '$file': $!"; | ||
| 140 | |||
| 141 | return 1; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head1 SUBCLASSING | ||
| 145 | |||
| 146 | Subclasses need to implement the C<_data_pos> class method which returns | ||
| 147 | the value of C<tell DATA> as it was after the class was loaded. | ||
| 148 | |||
| 149 | =head1 AUTHORS | ||
| 150 | |||
| 151 | Steffen Mueller E<lt>smueller@cpan.orgE<gt>, | ||
| 152 | Audrey Tang E<lt>cpan@audreyt.orgE<gt> | ||
| 153 | |||
| 154 | =head1 COPYRIGHT AND LICENSE | ||
| 155 | |||
| 156 | Copyright 2006-2009 by Steffen Mueller E<lt>smueller@cpan.orgE<gt>. | ||
| 157 | |||
| 158 | This program is free software; you can redistribute it and/or | ||
| 159 | modify it under the same terms as Perl itself. | ||
| 160 | |||
| 161 | See F<LICENSE>. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | __DATA__ |