in reply to Re^2: new file per line output
in thread new file per line output

I agree that it's simple thing to produce without using dedicated XML tools, but the character reservations that are involved with XML makes it potentially dangerous to do so. If you're willing to test, capture, and replace the characters (the minimum you should be doing is &, <, >, and %) in the data you're managing, that's fine, but the modules button all of that up for you nicely.

Here's how I'd do that. It's more code, granted, but it's always valid. Given the quality of some of the other tools that I've had to use that require XML, this will give me the best shot at not having to mess with it after I have it in production.

use strict; use warnings; use XML::Twig; my $stream_id = 'stream-id'; my $event_name = 'event-name'; my $time_t = 'time-t'; my $filename = 'foo.xml'; my $twig = XML::Twig->new(pretty_print => 'record'); $twig->parse('<event/>'); my $root = $twig->root(); $root->insert_new_elt('stream-id' => $stream_id); my $event_tag = $root->insert_new_elt('event-name' => $event_name); my $primary_event_tag = $event_tag->insert_new_elt('primary-event'); $primary_event_tag->insert_new_elt('delete-time' => $time_t); open(my $FH, '>', $filename); $twig->flush(\*$FH); close $FH;

Replies are listed 'Best First'.
Re^4: new file per line output
by davido (Cardinal) on Jan 02, 2014 at 16:27 UTC

    Thank you for taking the time to demonstrate that. It's a shame there's not a prettier way to do it, but I can appreciate that it is robust and won't try to cram a bunch of dangerous characters into an XML stream. I wouldn't have thought of going to XML::Twig as a producer before. And I can also see that, although producing such a simple stream seems simple, it's possible that untame data can turn it into a disaster.


    Dave