Просмотр исходного кода

Dpkg::Interface::Storable: load() and save() accept "-" for standard input/output

Simplify code in Dpkg::Control::Info thanks to this.
Raphaël Hertzog лет назад: 16
Родитель
Сommit
5d17954084
2 измененных файлов с 30 добавлено и 16 удалено
  1. 3 6
      scripts/Dpkg/Control/Info.pm
  2. 27 10
      scripts/Dpkg/Interface/Storable.pm

+ 3 - 6
scripts/Dpkg/Control/Info.pm

@@ -56,11 +56,7 @@ sub new {
     };
     bless $self, $class;
     if ($arg) {
-        if ($arg eq "-") {
-            $self->parse(\*STDIN, _g("<standard input>"));
-        } else {
-            $self->load($arg);
-        }
+	$self->load($arg);
     } else {
 	$self->load("debian/control");
     }
@@ -81,7 +77,8 @@ sub reset {
 
 =item $c->load($file)
 
-Load the content of $file. Exits in case of errors.
+Load the content of $file. Exits in case of errors. If file is "-", it
+loads from the standard input.
 
 =item $c->parse($fh, $description)
 

+ 27 - 10
scripts/Dpkg/Interface/Storable.pm

@@ -64,7 +64,8 @@ and it writes the same string to $fh (if it's defined).
 
 Initialize the object with the data stored in the file. The file can be
 compressed, it will be uncompressed on the fly by using a
-Dpkg::Compression::FileHandle object.
+Dpkg::Compression::FileHandle object. If $filename is "-", then the
+standard input is read (no compression is allowed in that case).
 
 =cut
 
@@ -73,10 +74,18 @@ sub load {
     unless ($self->can("parse")) {
 	internerr("%s cannot be loaded, it lacks the parse method", ref($self));
     }
-    my $cf = Dpkg::Compression::FileHandle->new();
-    open($cf, "<", $file) || syserr(_g("cannot read %s"), $file);
-    my $res = $self->parse($cf, $file, @options);
-    close($cf) || syserr(_g("cannot close %s"), $file);
+    my ($desc, $fh) = ($file, undef);
+    if ($file eq "-") {
+	$fh = \*STDIN;
+	$desc = _g("<standard input>");
+    } else {
+	$fh = Dpkg::Compression::FileHandle->new();
+	open($fh, "<", $file) || syserr(_g("cannot read %s"), $file);
+    }
+    my $res = $self->parse($fh, $desc, @options);
+    if ($file ne "-") {
+	close($fh) || syserr(_g("cannot close %s"), $file);
+    }
     return $res;
 }
 
@@ -84,7 +93,8 @@ sub load {
 
 Store the object in the file. If the filename ends with a known
 compression extension, it will be compressed on the fly by using a
-Dpkg::Compression::FileHandle object.
+Dpkg::Compression::FileHandle object. If $filename is "-", then the
+standard output is used (data are written uncompressed in that case).
 
 =cut
 
@@ -93,10 +103,17 @@ sub save {
     unless ($self->can("output")) {
 	internerr("%s cannot be saved, it lacks the output method", ref($self));
     }
-    my $cf = Dpkg::Compression::FileHandle->new();
-    open($cf, ">", $file) || syserr(_g("cannot write %s"), $file);
-    $self->output($cf, @options);
-    close($cf) || syserr(_g("cannot close %s"), $file);
+    my $fh;
+    if ($file eq "-") {
+	$fh = \*STDOUT;
+    } else {
+	$fh = Dpkg::Compression::FileHandle->new();
+	open($fh, ">", $file) || syserr(_g("cannot write %s"), $file);
+    }
+    $self->output($fh, @options);
+    if ($file ne "-") {
+	close($fh) || syserr(_g("cannot close %s"), $file);
+    }
 }
 
 =item "$obj"