--- a/bin/catter Thu Jul 28 10:03:15 2011 +0200
+++ b/bin/catter Thu Jul 28 11:12:38 2011 +0200
@@ -8,7 +8,6 @@
# cat DATA/$f || zcat DATA/$f.gz
# done
-
use 5.010;
use strict;
use warnings;
@@ -22,7 +21,7 @@
use constant KiB => 1024;
use constant MiB => 1024 * KiB;
use constant GiB => 1024 * MiB;
-use constant ME => basename $0;
+use constant ME => basename $0;
sub find_data_dir;
@@ -30,59 +29,67 @@
Getopt::Long::Configure(qw(Bundling));
GetOptions(
- "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) },
- "m|man" => sub { pod2usage(-verbose => 2, -exit => 0,
- -noperldoc => system("perldoc -V 1>/dev/null
- 2>&1")) },
- ) and @ARGV == 2 or pod2usage;
+ "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) },
+ "m|man" => sub {
+ pod2usage(
+ -verbose => 2,
+ -exit => 0,
+ -noperldoc => system(
+ "perldoc -V 1>/dev/null
+ 2>&1"
+ )
+ );
+ },
+ )
+ and @ARGV == 2
+ or pod2usage;
- my $idx = shift;
- my $dst = shift;
+ my $idx = shift;
+ my $dst = shift;
my $blocksize = undef;
- my $data = find_data_dir($idx);
+ my $data = find_data_dir($idx);
open(my $fh => $idx);
{ local $/ = ""; $_ = <$fh>; }
- /^format:\s*1$/m or die ME.": expected index format 1\n";
- ($blocksize) = /^blocksize:\s*(\d+)/m or die ME.": no blocksize found\n";
-
+ /^format:\s*1$/m or die ME . ": expected index format 1\n";
+ ($blocksize) = /^blocksize:\s*(\d+)/m or die ME . ": no blocksize found\n";
my $out;
- if ($dst eq "-") { open($out => ">&STDOUT") }
- else { open($out => ">", $dst) };
+ if ($dst eq "-") { open($out => ">&STDOUT") }
+ else { open($out => ">", $dst) }
while (<$fh>) {
- next if /^#/;
- my ($blk, $hash, $path) = split;
- my ($in, $buffer);
+ next if /^#/;
+ my ($blk, $hash, $path) = split;
+ my ($in, $buffer);
- if (-f "$data/$path") {
- open($in => "$data/$path");
- binmode($in);
- local $/ = \$blocksize;
- $buffer = <$in>;
- }
- elsif (-f "$data/$path.gz") {
- open($in => "$data/$path.gz");
- binmode($in);
- gunzip($in => \$buffer)
- or die $GunzipError;
- }
- else {
- die ME.": Can't open $data/$path: $!\n";
- }
- print {$out} $buffer;
- close($in);
+ if (-f "$data/$path") {
+ open($in => "$data/$path");
+ binmode($in);
+ local $/ = \$blocksize;
+ $buffer = <$in>;
+ }
+ elsif (-f "$data/$path.gz") {
+ open($in => "$data/$path.gz");
+ binmode($in);
+ gunzip($in => \$buffer)
+ or die $GunzipError;
+ }
+ else {
+ die ME . ": Can't open $data/$path: $!\n";
+ }
+ print {$out} $buffer;
+ close($in);
}
close($out);
close($fh);
}
sub find_data_dir {
- for (my $dir = shift; $dir ne "/"; $dir = abs_path("$dir/..")) {
- return "$dir/data" if -d "$dir/data" and -d "$dir/idx";
+ for (my $dir = shift ; $dir ne "/" ; $dir = abs_path("$dir/..")) {
+ return "$dir/data" if -d "$dir/data" and -d "$dir/idx";
}
- die ME.": no data directory found!\n";
+ die ME . ": no data directory found!\n";
}
__END__