From ebe27f2ad5e1438324c6f87021aa24b3bce8f9df Mon Sep 17 00:00:00 2001 From: Frank Lichtenheld Date: Sun, 13 Jan 2008 18:07:15 +0100 Subject: [PATCH] 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. --- ChangeLog | 9 +++++++++ scripts/Dpkg/Changelog.pm | 8 +------- scripts/Dpkg/Fields.pm | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index 87ba103d..4d1028d0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2008-01-13 Frank Lichtenheld + * 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. diff --git a/scripts/Dpkg/Changelog.pm b/scripts/Dpkg/Changelog.pm index e186871f..de59c667 100644 --- a/scripts/Dpkg/Changelog.pm +++ b/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; } diff --git a/scripts/Dpkg/Fields.pm b/scripts/Dpkg/Fields.pm index 809ca664..c640bb58 100644 --- a/scripts/Dpkg/Fields.pm +++ b/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; -- 2.39.5