Procházet zdrojové kódy

Dpkg::Fields::Object: Support filehandles and strings for output and dump

* scripts/Dpkg/Fields.pm (dump): Allow to omit the
filehandle argument. If the function is called in
non-void context, also remove the printed string
to the caller. Together this avoids having to fiddle
with filehandles if the caller doesn't want to.
(output): Likewise.
* scripts/Dpkg/Changelog.pm (data2rfc822): Simplify
using this new behaviour.
Frank Lichtenheld před 18 roky
rodič
revize
ebe27f2ad5
3 změnil soubory, kde provedl 22 přidání a 9 odebrání
  1. 9 0
      ChangeLog
  2. 1 7
      scripts/Dpkg/Changelog.pm
  3. 12 2
      scripts/Dpkg/Fields.pm

+ 9 - 0
ChangeLog

@@ -1,5 +1,14 @@
 2008-01-13  Frank Lichtenheld  <djpig@debian.org>
 
+	* scripts/Dpkg/Fields.pm (dump): Allow to omit the
+	filehandle argument. If the function is called in
+	non-void context, also remove the printed string
+	to the caller. Together this avoids having to fiddle
+	with filehandles if the caller doesn't want to.
+	(output): Likewise.
+	* scripts/Dpkg/Changelog.pm (data2rfc822): Simplify
+	using this new behaviour.
+
 	* scripts/t/600_Dpkg_Changelog.t: Add a new changelog
 	'fields' that tests the handling of the different fields
 	in the dpkg format.

+ 1 - 7
scripts/Dpkg/Changelog.pm

@@ -35,7 +35,6 @@ package Dpkg::Changelog;
 use strict;
 use warnings;
 
-use v5.8.0; # for open $fh, '>', \$scalar
 use English;
 
 use Dpkg;
@@ -665,12 +664,7 @@ sub data2rfc822 {
 
 	return join "\n", @rfc822;
     } else {
-	my $rfc822_str = "";
-
-	open my $fh, '>', \$rfc822_str
-	    or warning("couldn't open filehandle for string");
-	$data->output($fh);
-	close $fh;
+	my $rfc822_str = $data->output;
 
 	return $rfc822_str;
     }

+ 12 - 2
scripts/Dpkg/Fields.pm

@@ -158,15 +158,19 @@ sub NEXTKEY {
 
 sub dump {
     my ($self, $fh) = @_;
+    my $str = "";
     foreach (@{$self->[1]}) {
 	if (exists $self->[0]->{$_}) {
-	    print $fh "$_: " . $self->[0]->{$_} . "\n";
+	    print $fh "$_: " . $self->[0]->{$_} . "\n" if $fh;
+	    $str .= "$_: " . $self->[0]->{$_} . "\n" if defined wantarray;
 	}
     }
+    return $str;
 }
 
 sub output {
     my ($self, $fh, $substvars) = @_;
+    my $str = "";
 
     # Add substvars to refer to other fields
     if (defined($substvars)) {
@@ -194,8 +198,14 @@ sub output {
            $v =~ s/\s*,\s*$//;
         }
         $v =~ s/\$\{\}/\$/g;
-        print $fh "$f: $v\n" || syserr(_g("write error on control data"));
+	if ($fh) {
+	    print $fh "$f: $v\n" || syserr(_g("write error on control data"));
+	}
+	if (defined wantarray) {
+	    $str .= "$f: $v\n";
+	}
     }
+    return $str;
 }
 
 1;