开发者

How to cancel output if $^I=".bak" already exists?

In Perl, I'm looping over files using the common pattern:

$^I=".bak"; 

while(<>) {
  s/pattern/replacement/g;
  print;
}

where $^I will create backups开发者_StackOverflow社区. However, I want to test if the backup file exists and cancel the script if it does, so the back up isn't overwritten. It has to be explicitly removed.

The problem is that outside the while loop I can't do this

if (-e "$ARGV.bak") {
  # print warning and exit
}

because $ARGV isn't set until <>, and inside the while loop $ARGV has already been replaced.

So am I missing something or do I have to do this a different way?

Thanks!


Check @ARGV (which is what null <> reads its list of file names from) before entering the while loop that uses it. e.g.

die "Backup file already exists.\n" if -e $ARGV[0] . $^I;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜