kingwrcy has asked for the wisdom of the Perl Monks concerning the following question:

#$^I=""; #$^I=".bak"; sub replace(){ my $para = "$dir/$_[0]"; open MYFILE,"$para" or die "cannot open file $para $!\n"; while(<MYFILE>{ s/UI_EM/$newpar/g; print; } }
i just want to open a TXT file and replace some words,and then save it to the TXT file.i don't want any BAK files. But $^I does not work .whether it is "",or ".BAK" ! i am chinese guy. My english is not very good.if there anything wrong above,please tell me. thanks .

Replies are listed 'Best First'.
Re: $^I why this symbol does not work ?
by Corion (Patriarch) on Oct 01, 2008 at 06:24 UTC

    You don't tell us how it does not work. But I'm going to guess. You're running this on Windows. On Windows, it's not possible to recreate a file while still reading from it. That's why you need to set $^I. You can still erase the file afterwards.

    Also see the Sysadm::Install module, which has the pie subroutine, which does just what your replace subroutine does.

      to:Corion\
      Yes.my OS is windows XP.
      you mean :In windows XP, when a fle is opened,we cannot save/modify it at the sametime ?
      now i want to replace some words then save it ,how i should do?
      perl -p -i -e "s/<String to Find>/<String to replace>/g" <File Name>
      I try this ,an error occurs :Can't do inplace edit without backup.
      i don't want any bakup files .
      this is a shortcut,If i want to write a program,how i finish this job ?

        No. Under Windows, you cannot use -i, you have to use -i.bak or something like that, because you cannot "rewrite" a file on the fly, at least not with the simple approach that the -p switch does.

        You will need to create the backup file and then erase it afterwards.

        On windows system, you can use the following one liner.
        perl -pi.bak -e "s/<String to Find>/<String to replace>/;" <File Name>
        And ofcourse, it will create a backup of original file with .bak extension.
Re: $^I why this symbol does not work ?
by moritz (Cardinal) on Oct 01, 2008 at 06:16 UTC
    I guess that $^I only works if you use the magic ARGV file handle, so you could try something along these lines:
    sub replace { local $^I = ''; local @ARGV = ($para); while(<>) { s/UI_EM/$newpar/g; print; } }

    (Untested)

      my $new_par = "YI_OU"; open MYFILE,"c:/perl/test/1.txt" or die "cannot open file:$!"; local $^I = ''; local @ARGV = MYFILE; while(<>){ s/UI_EM/$new_par/g; print; }
      run it .an error occur:
      Bareword "MYFILE" not allowed while "strict subs" in use at C:\Perl\study\test.pl line 9.
      By the way could you tell me what is "magic ARGV file handle"?
      I am new perler.thanks.

        Put file names (not handles) in @ARGV:

        my $new_par = "YI_OU"; local $^I = ''; local @ARGV = 'c:/perl/test/1.txt'; while(<>) { s/UI_EM/$newpar/g; print; }
        local @ARGV = MYFILE;

        That's not the code I gave you.

        By the way could you tell me what is "magic ARGV file handle"?

        It's all in the docs.

Re: $^I why this symbol does not work ?
by andreas1234567 (Vicar) on Oct 01, 2008 at 07:23 UTC
    The FMTYEWTK About Mass Edits In Perl article (perl.com, by Geoff Broadwell October 14, 2004) is probably helpful to you.
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
      Thank you.It is helpful.But it's so long.My english is not very good...
      Study English first.
      thank you..
Re: $^I why this symbol does not work ?
by lamp (Chaplain) on Oct 01, 2008 at 06:22 UTC
    You can use the following perl one liner for replacing words from a file
    perl -p -i -e "s/<String to Find>/<String to replace>/g" <File Name>