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.
use strict;
use warnings;
-use v5.8.0; # for open $fh, '>', \$scalar
use English;
use Dpkg;
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;
}
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)) {
$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;