I'm been experimenting with the command line switch -i.
I was confused with results at first, but I think I'm starting to figure it out.
I have a file "testfile" that contains the text "testdata".
When I run
ls testfile | perl -lpi -e 's/e/*/'
I get the output "t*stfil*" and the file contents are unchanged.
However, if I run
ls testfile | xargs perl -lpi -e 's/e/*/'
There is no output to STDOUT, and the contents of the textfile are changed as
expected, now reading "t*stdata".
It took me a while to figure out what was happening, but it seems that files
are only edited in place when the filenames are passed as arguments, not as STDIN.
This was confirmed when I found
merlyn's post
xargs functionality for inplace editing which uses
find ... -print | perl -pi.bak -e 'BEGIN { chomp(@ARGV = <STDIN>) } s/foo/bar/g'
However,
perlrun -i "specifies that files processed by the <> construct
are to be edited in-place"
The code explaination also reads
LINE: while (<>) {
...
}
I thought that meant it processes <STDIN>, not @ARGV, am I reading perlrun wrong or should
the code example read ...?
LINE: while (@ARGV) {
...
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.