所以,我已经习惯了perl -i来使用perl,就像我将sed和就地编辑一样.
perlvar中$^ I的文档:
$^I
The current value of the inplace-edit extension. Use undef to disable inplace editing.
好.所以这意味着我可能会在脚本中进行“就地”编辑?
我遇到麻烦的是这样的:
如果我跑:
perl -pi -e 's/^/fish/' test_file
然后解析它:
BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { s/^/fish/; } continue { die "-p destination: $!\n" unless print $_; }
现在 – 如果我想在脚本中使用$^ I,请说:
foreach my $file ( glob "*.csv" ) { #inplace edit these files - maybe using Text::CSV to manipulate? }
我如何“启用”这种情况发生?这是一个改变$_的问题(默认为s / something / somethingelse / do)并让perl隐式打印出来吗?或者还有其他事情发生了吗?
我的主要问题是 – 我可以执行应用CSV转换(或XML调整或类似)的“就地编辑”.
我很欣赏我可以打开单独的文件句柄,阅读/打印等.我想知道是否有另一种方式. (即使它只是在情境上有用).
解决方法
由-i命令行选项或通过设置$^启用的就地编辑行为仅适用于ARGV文件句柄.这意味着必须在命令行上命名文件,或者必须在程序中设置@ARGV
该程序将所有CSV文件中的所有小写字母更改为大写.请注意,我已将$^ I设置为非空字符串,建议您在测试时保留原始数据文件
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; } }