Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: new file per line output

by Kenosis (Priest)
on Dec 31, 2013 at 19:07 UTC ( #1068815=note: print w/replies, xml ) Need Help??

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 }
  • Always: use strict; use warnings;
  • Used a single split
  • Used the three-argument open()
  • Used a here document for printing.
  • A close is absent above, since the currently-opened file will automatically close when a new handle is assigned to $fh

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 (Archbishop) 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.


      Thank you. Have updated the script.

Re^2: new file per line output
by AnomalousMonk (Chancellor) 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 (Prior) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068815]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-01-20 22:04 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.