Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello monks. I was using perl to translate files from one format to another with something like the following: perl -p translation.pl file.in > file.out when I got lazy and decided that I should be able to just pass a list of input files, and let perl append .out (or replace the extension) to create an output file for each input file.

There are obviously more than one way to do it and I settled for:

for my $filename (@ARGV) { open my $input, '<', $filename or die "Couldn't open $filename: $!"; open my $output, '>', $filename.'.new' or die "Couldn't open $filena +me: $!"; while (<$input>) { say $output translate($_); } }

Now I'm wondering, is there some simpler way to do it? This looks like a pretty common task, and it is very close to what -i does, except the two files are swapped: I want the output in the file with the added extension and the original unchanged. There doesn't seem to be an option for it, so do you know of some hack (bring out your secret operators, I'm asking out of curiosity so I want even the bad answers :D ) or some module that would let me write my code without having to write the explicit while(<>) loop on the data?

NB: yup I already just asked that in the CB and got some answers. Like choroba's proposition to call the translation.pl file inside a shell loop to handle the output files.

Edit: perl -pE "open STDOUT, '>>', qq<$ARGV.txt>; s/(.*)/\U$1/;" FILES works once :P (inspired by choroba's proposition)


In reply to One to one file output idiom by Eily

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 22:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found