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.
| [reply] [d/l] [select] |
|
|
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 ?
| [reply] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
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. | [reply] [d/l] |
|
|
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) | [reply] [d/l] [select] |
|
|
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. | [reply] [d/l] |
|
|
my $new_par = "YI_OU";
local $^I = '';
local @ARGV = 'c:/perl/test/1.txt';
while(<>) {
s/UI_EM/$newpar/g;
print;
}
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
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]
| [reply] |
|
|
Thank you.It is helpful.But it's so long.My english is not very good...
Study English first.
thank you..
| [reply] |
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>
| [reply] [d/l] |