且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Perl 在脚本中进行就地编辑(而不是单行)

更新时间:2023-12-04 12:30:28

-i 命令行选项或通过设置 $^ 启用的就地编辑行为I 仅适用于 ARGV 文件句柄.这意味着文件必须在命令行中命名或 @ARGV 必须在程序中设置

The edit-in-place behaviour that is enabled by the -i command-line option or by setting $^I works only on the ARGV file handle. That means the files must either be named on the command line or @ARGV must be set up within the program

此程序会将所有 CSV 文件中的所有小写字母更改为大写字母.请注意,我已将 $^I 设置为非空字符串,建议您在测试时这样做,以便保留原始数据文件

This program will change all lower-case letters to upper-case in all CSV files. Note that I have set $^I to a non-null string, which is advisable while you are testing so that your original data files are retained

use strict;
use warnings;

our $^I = '.bak';

while ( my $file = glob '*.csv' ) {

  print "Processing $file\n";

  our @ARGV = ($file);

  while ( <ARGV> ) {
     tr/a-z/A-Z/;
     print;
  }
}