This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Making adding binary files possible
[perl5.git] / pack.pl
... / ...
CommitLineData
1#!perl
2use strict;
3use Getopt::Std;
4
5my $opts = {};
6getopts('ushvD', $opts );
7
8die usage() if $opts->{h};
9
10my $file = shift or die "Need file\n". usage();
11my $outfile = shift || '';
12my $mode = (stat($file))[2] & 07777;
13
14open my $fh, $file or die "Could not open input file $file: $!";
15my $str = do { local $/; <$fh> };
16
17### unpack?
18my $outstr;
19if( $opts->{u} ) {
20 if( !$outfile ) {
21 $outfile = $file;
22 $outfile =~ s/\.packed$//;
23 }
24
25 $outstr = unpack 'u', $str;
26
27} else {
28 $outfile ||= $file . '.packed';
29
30 $outstr = pack 'u', $str;
31}
32
33### output the file
34if( $opts->{'s'} ) {
35 print STDOUT $outstr;
36} else {
37 print "Writing $file into $outfile\n" if $opts->{'v'};
38 open my $outfh, ">$outfile"
39 or die "Could not open $outfile for writing: $!";
40 print $outfh $outstr;
41 close $outfh;
42
43 chmod $mode, $outfile;
44}
45
46### delete source file?
47if( $opts->{'D'} and $file ne $outfile ) {
48 1 while unlink $file;
49}
50
51sub usage {
52 return qq[
53Usage: $0 [-v] [-s] [-D] SOURCE [OUTPUT_FILE]
54 $0 [-v] [-s] [-D] -u SOURCE [OUTPUT_FILE]
55 $0 -h
56
57 uuencodes a file, either to a target file or STDOUT.
58 If no output file is provided, it outputs to SOURCE.packed
59
60Options:
61 -v Run verbosely
62 -s Output to STDOUT rather than OUTPUT_FILE
63 -h Display this help message
64 -u Unpack rather than pack
65 -D Delete source file after encoding/decoding
66
67]
68}