In-place editing of multiple files in a directory using Perl's diamond and in-place edit operator

后端 未结 5 1411
挽巷
挽巷 2021-01-03 05:12

I am trying to in-place edit a bunch of text files using Perl\'s in-place edit operator $^I. I traverse through the directory using the diamond (<>) operator like this:

5条回答
  •  礼貌的吻别
    2021-01-03 05:54

    My usual approach for this is to process the arguments and save the files off in a temporary list, then stuff them back into @ARGV.

    my @files;
    foreach (@ARGV) {
        ... do something with each parm
        else {
            push @files,$_;
        }
    }
    @ARGV=@files;
    while (<>) {
        ...
    

    Usually, in the foreach (@ARGV) I will do something like

    if (-f) {
        push @files,$_;
        next;
    }
    

提交回复
热议问题