Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use of -i inplace option

Reply
Thread Tools

Use of -i inplace option

 
 
David Harmon
Guest
Posts: n/a
 
      10-03-2006
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;
}

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-03-2006
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

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      10-03-2006
On 3 Oct 2006 11:25:50 -0700 in comp.lang.perl.misc, "Paul Lalli"
<(E-Mail Removed)> wrote,
>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 (<>) { }


Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ann: InPlace, a new xslt-based CMS Oleg Paraschenko XML 0 10-04-2007 04:56 AM
gcc needs copy consturctor for inplace constructed object passed to func as reference, others not nutty C++ 6 06-15-2006 02:02 PM
asp 1.1 datagrid inplace editing, utf-8 code saved as "??" ? jason ASP .Net 0 11-09-2005 06:57 AM
DataGrid InPlace Editing msnews.microsoft.com ASP .Net 0 01-29-2005 10:11 AM
inplace edit error message news.verizon.net Perl 4 10-08-2003 09:43 PM



Advertisments