Re: Use of -i inplace option



David Harmon wrote:
I try to modify a file in place with

perl -i.bak myfunc.pl data.txt

but instead of modifying in place, I get output to the screen
and a zero-length data.txt. The original data.txt is renamed
to data.txt.bak as expected.

What should my command line be?

I'm using perl v5.8.8 built for MSWin32-x86-multi-thread
Binary build 819 [267479] provided by ActiveState
Windows XP.

Simplified myfunc.pl is:
for (<>) {
print;
}

It has nothing to do with your command line and everything to do with
your code. The special magic of the $^I variable (or -i command line
switch) apply only to files read via the
while (<>) { }
construct. Your for(<>) {} construct doesn't work with it. My *guess*
as to the reason for this is that because for reads all lines from the
file at once, by the time it starts actually processing the file, the
magic of $^I has already been expended, and STDOUT has been reselected.
That is, $^I only changes the default output filehandle while <ARGV>
is still being read. As soon as it's exhausted, STDOUT becomes the
default filehandle again. Because you didn't have any print lines
while the new file was select()'ed, no text was written to that file.

On a related note, NEVER use the for (<>) {} construct. Ever. You are
attempting to process a file line-by-line, which is good, but you're
reading all the lines into memory at once, and keeping them there for
the duration of the loop.
for my $line (<$fh>) { }
is simply this in disguise:
my @all_lines = <$fh>;
for my $line (@all_lines) { }

Whereas the while(<>) {} construct reads one line, processes it,
discards it, and reads the next line.

Paul Lalli

.



Relevant Pages

  • Re: first in last out
    ... reverses even big files in a smart way, but to the shell's stdout. ... For some reason ... perl -i.bak -ne ... ... the ARGVOUT) filehandle has been closed and the default output filehandle is ...
    (comp.unix.shell)
  • Re: first in last out
    ... reverses even big files in a smart way, but to the shell's stdout. ... For some reason ... perl -i.bak -ne ... ... the ARGVOUT) filehandle has been closed and the default output filehandle is ...
    (comp.unix.shell)
  • Re: Wait for background processes to complete
    ... To be able to execute commands in the background and wait for their ... The documentation I am referring to is http://perldoc.perl.org/. ... You can run a command in the background with: ... There is a general problem with perl documentation: ...
    (comp.lang.perl.misc)
  • Re: Perl For Amateur Computer Programmers
    ... >professional computer programmers could use with the same ease as Basic. ... >Perl For Amateur Computer Programmers ... Also, taking into account that you're appealing to "scientists", it ... Also, as a side note, you seem to use the noun "command" in a naive ...
    (comp.lang.perl.misc)
  • Obtaining complete Unix command line that evoked script as string
    ... If there is a more appropriate list for this, let me know; the other perl lists I've seen seem to specialised for this. ... Note this is not just the arguments of the call to the script, but everything including pipes and redirects, etc., e.g. ... Ideally the perl interpreter would grab the complete command line as its evoked and I'd access this via a variable. ...
    (perl.beginners)