Subject: | Patch proposal for string output |
I have attached a little patch that incorporates the functionality provided by XML::Writer::String to XML::Writer. With that patch, the OUTPUT of XML::Writer can now be piped to a scalar variable instead of a filehandle.
E.g.:
my $xml;
my $writer = new XML::Writer( OUTPUT => \$xml );
# do a lot of stuff here
print $xml;
So there we go. Please feel to use and abuse as you see fit. :)
diff -c XML-Writer-0.520/Changes XML-Writer-0.521/Changes
*** XML-Writer-0.520/Changes 2004-09-01 11:23:29.000000000 -0400
--- XML-Writer-0.521/Changes 2004-11-23 20:34:04.000000000 -0500
***************
*** 1,5 ****
--- 1,9 ----
Revision history for Perl extension XML::Writer.
+ 0.521 Tue Nov 23 20:32:47 EST 2004 <yanick@babyl.dyndns.org>
+ - Added the possibility to output to a string instead than
+ to a filehandle
+
0.520 Wed Sep 1 16:18:46 BST 2004 <joe@kafsemo.org>
- Fixed bug with forced declaration of the default namespace (#7266)
- Removed dead code. Added copyright notices to pod.
Only in XML-Writer-0.521: Makefile.old
diff -c XML-Writer-0.520/Makefile.PL XML-Writer-0.521/Makefile.PL
*** XML-Writer-0.520/Makefile.PL 2004-09-01 11:23:29.000000000 -0400
--- XML-Writer-0.521/Makefile.PL 2004-11-23 16:30:07.000000000 -0500
***************
*** 7,13 ****
# the contents of the Makefile that is written.
WriteMakefile(
'NAME' => 'XML::Writer',
! 'VERSION' => '0.520',
# A manually-created META.yml has all the other metadata;
# we don't want it overwritten
--- 7,13 ----
# the contents of the Makefile that is written.
WriteMakefile(
'NAME' => 'XML::Writer',
! 'VERSION' => '0.521',
# A manually-created META.yml has all the other metadata;
# we don't want it overwritten
diff -c XML-Writer-0.520/MANIFEST XML-Writer-0.521/MANIFEST
*** XML-Writer-0.520/MANIFEST 2004-05-25 09:28:53.000000000 -0400
--- XML-Writer-0.521/MANIFEST 2004-11-23 16:30:22.000000000 -0500
***************
*** 5,8 ****
--- 5,9 ----
META.yml
Writer.pm
t/01_main.t
+ t/02_string.t
TODO
Common subdirectories: XML-Writer-0.520/t and XML-Writer-0.521/t
diff -c XML-Writer-0.520/Writer.pm XML-Writer-0.521/Writer.pm
*** XML-Writer-0.520/Writer.pm 2004-09-01 11:16:08.000000000 -0400
--- XML-Writer-0.521/Writer.pm 2004-11-23 20:39:28.000000000 -0500
***************
*** 15,21 ****
use vars qw($VERSION);
use Carp;
use IO::Handle;
! $VERSION = "0.520";
--- 15,21 ----
use vars qw($VERSION);
use Carp;
use IO::Handle;
! $VERSION = "0.521";
***************
*** 426,434 ****
$self->{'SETOUTPUT'} = sub {
my $newOutput = $_[0];
# If there is no OUTPUT parameter,
# use standard output
! $output = $newOutput || \*STDOUT;
};
$self->{'SETDATAMODE'} = sub {
--- 426,443 ----
$self->{'SETOUTPUT'} = sub {
my $newOutput = $_[0];
+
+ if( $newOutput ) {
+ $output = ref $newOutput eq 'SCALAR' ?
+ XML::Writer::String->new( $newOutput )
+ : $newOutput;
+ }
+ else {
# If there is no OUTPUT parameter,
# use standard output
! $output = \*STDOUT;
! }
! return $output;
};
$self->{'SETDATAMODE'} = sub {
***************
*** 1051,1056 ****
--- 1060,1091 ----
return &{$self->{FORCENSDECL}};
}
+ package XML::Writer::String;
+
+ # internal class enabling the possiblity to write the xml
+ # to a string instead than to a filehandle
+ #
+ # heavily inspired by Simon Olivier's XML::Writer::String
+
+ sub new {
+ my $class = shift;
+ my $scalar_ref = shift;
+ my $self = bless $scalar_ref, $class;
+ $self->value(@_) if @_;
+ return $self;
+ }
+
+ sub print {
+ my $self = shift;
+ $$self .= join '', @_;
+ return scalar(@_);
+ }
+
+ sub value {
+ my $self = shift;
+ @_ ? $$self = join('', @_) : $$self;
+ }
+
1;
__END__
***************
*** 1120,1127 ****
=item OUTPUT
An object blessed into IO::Handle or one of its subclasses (such as
! IO::File); if this parameter is not present, the module will write to
! standard output.
=item NAMESPACES
--- 1155,1162 ----
=item OUTPUT
An object blessed into IO::Handle or one of its subclasses (such as
! IO::File), or a reference to a string; if this parameter is not present,
! the module will write to standard output.
=item NAMESPACES