开发者

Perl Inline Replacement in a Perl Script

开发者 https://www.devze.com 2023-01-31 10:49 出处:网络
I am trying to write a 开发者_高级运维perl script with inline replacement. I have found this script example to do it, but not sure how to use it as im not sure how to specify the file and handler?

I am trying to write a 开发者_高级运维perl script with inline replacement. I have found this script example to do it, but not sure how to use it as im not sure how to specify the file and handler?

$^I = '.bak'  # Call for in-place editing; make backups with a .bak suffix

while (<>) 
{
  s/foo/bar/
  print;
}


The <> is a special file handle.

The special file handle: <>

Like the default variable, the special file handle -- written as <> is a short cut in the language added to make programs easier to write. The special file handle treats all command line arguments as file names, and opens each in turn. If there are no command line arguments then it opens and reads from STDIN. As per the UNIX convention, if "-" is given as a command line argument it opens STDIN in place of a file.

Source

Setting the $^I variable enables in-place editing - in other words the file is modified directly.

To use this script you can for example do this:

perl yourscript.pl somefile.txt
0

精彩评论

暂无评论...
验证码 换一张
取 消