http://www.perlmonks.org?node_id=1068815


in reply to new file per line output

You've received excellent scripting suggestions. Still, perhaps the following minor modifications of your script will be helpful:

use strict; use warnings; use DateTime; use POSIX qw(strftime); use autodie; open my $logFH, '<', 'second.txt'; while (<$logFH>) { my ( $streamname, $streamid, $timedate ) = split /[@\s]/; my $time_t = POSIX::strftime( "%Y-%m-%d %r", localtime($timedate) +); open my $fh, '>', "$streamid/$streamname"; print $fh <<END; <event> <stream-id>$streamid</stream-id> <event-name>$streamname</event-name> <primary-event> <delete-time>$time_t</delete-time> </primary-event> </event> END }

Edit: Added use autodie; to catch any silent close failures. Thank you, davido.

Replies are listed 'Best First'.
Re^2: new file per line output
by davido (Cardinal) on Dec 31, 2013 at 19:16 UTC

    I don't mind implicit closes of input filehandles, but without the autodie pragma, the implicit close within a loop of output files could permit silent failure.


    Dave

      Thank you. Have updated the script.

Re^2: new file per line output
by AnomalousMonk (Archbishop) on Dec 31, 2013 at 20:43 UTC
    open my $fh, '>', $streamid/$streamname;

    Open for writing a file that has a name that is the stringized quotient of $streamid divided by $streamname? Surely this is not what monteryjack intends!

      Appreciate you catching this. Accidentally removed the quotes when I updated the script. Fixed!

Re^2: new file per line output
by karlgoethebier (Abbot) on Jan 01, 2014 at 14:22 UTC

    Hi Kenosis, by the way another question: How did you format the here document? By hand?

    One reason why i avoid using SQl/XML here documents is that it's sometimes hard to format them.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Hi Karl and Happy New Year!

      Yes, by hand--only for readability, since it was just a few tags.