|
Posted by Bill H on May 29, 2008, 6:42 pm
Please log in for more thread options
>
> > In article
>
> >> [snip]
> >> > 5.12: How can I open a filehandle to a string?
>
> >> This is interesting, but what real world examples would there be to
> >> doing this instead of just using string functions?
>
> > I usually need this where some module insists on having a filehandle to
> > send its data to, but I want it in a string without all the extra work
> > and left-over files. Now I can print to a string directly.
>
> I find this especially handy in test scripts. For example, here's an
> excerpt from a test script for a project I'm currently working on:
>
> my $s;
> close(STDOUT);
>
> my $id =3D $dataset->id;
>
> open(STDOUT, '>', $s);
> $dataset->print('xml');
> ok(index($s, "<d:id>$id</d:id>") !=3D -1, =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
"generated XML contains <d:id>$id</d:id>");
> ok(index($s, "<d:description xml:lang=3D'de'") !=3D -1, =A0 =A0 "generated=
XML contains german description");
>
> (in this case $dataset also has a method as_xml, which returns the XML
> representation as a string. So this test mainly serves to ensure that
> print really calls as_xml and prints the result to STDOUT. But it might
> actually be useful to turn them around: Put all the logic into print and
> then implement as_xml as a wrapper around print - this safes memory when
> you are only printing and allows pipeling the produces and the consumer
> of the XML file)
>
> =A0 =A0 =A0 =A0 hp
I like this idea Peter. I am starting to do a lot of xml processing
via perl and this may come in handy.
Thanks, Bill H
|