The following commit has been merged in the master branch:
commit 5d1795408484778f7978062bb472c97b45a5cd10
Author: Raphaël Hertzog <[email protected]>
Date:   Sat Feb 20 23:50:06 2010 +0100

    Dpkg::Interface::Storable: load() and save() accept "-" for standard 
input/output
    
    Simplify code in Dpkg::Control::Info thanks to this.

diff --git a/scripts/Dpkg/Control/Info.pm b/scripts/Dpkg/Control/Info.pm
index 3f3ef26..7ea87a0 100644
--- a/scripts/Dpkg/Control/Info.pm
+++ b/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)
 
diff --git a/scripts/Dpkg/Interface/Storable.pm 
b/scripts/Dpkg/Interface/Storable.pm
index e4a5c45..8474fa3 100644
--- a/scripts/Dpkg/Interface/Storable.pm
+++ b/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"

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to