Use of -i inplace option

Discussion in 'Perl Misc' started by David Harmon, Oct 3, 2006.

  1. David Harmon

    David Harmon Guest

    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;
    }
     
    David Harmon, Oct 3, 2006
    #1
    1. Advertising

  2. David Harmon

    Paul Lalli Guest

    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
     
    Paul Lalli, Oct 3, 2006
    #2
    1. Advertising

  3. David Harmon

    David Harmon Guest

    On 3 Oct 2006 11:25:50 -0700 in comp.lang.perl.misc, "Paul Lalli"
    <> 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.
     
    David Harmon, Oct 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.verizon.net

    inplace edit error message

    news.verizon.net, Sep 29, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,615
    Jim Gibson
    Oct 8, 2003
  2. msnews.microsoft.com

    DataGrid InPlace Editing

    msnews.microsoft.com, Jan 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    msnews.microsoft.com
    Jan 29, 2005
  3. jason
    Replies:
    0
    Views:
    425
    jason
    Nov 9, 2005
  4. nutty
    Replies:
    6
    Views:
    399
    Tom Widmer
    Jun 15, 2006
  5. Oleg Paraschenko

    Ann: InPlace, a new xslt-based CMS

    Oleg Paraschenko, Oct 4, 2007, in forum: XML
    Replies:
    0
    Views:
    451
    Oleg Paraschenko
    Oct 4, 2007
Loading...

Share This Page