Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: new file per line output

by Kenosis (Priest)
on Dec 31, 2013 at 19:07 UTC ( [id://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 (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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1068815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found