是否可以在Perl中解析ARGV的1美元? 我试图从ARGV解析传入$ 1,但它将其视为文字字符串,而不是1美元。
这只是一个试图展示概念证明的简单脚本。
#!/usr/bin/perl
my $from = '^(.*/)([^/]*)$';
my $dir = "/var/foo/baz";
$dir =~ /$from/;
# This works and prints the expected output I would like
print "Dir $1\n";
print "File $2\n";
# This is printing a literal '$1' and '$2'
print "Dir $ARGV[0]\n";
print "File $ARGV[1]\n";
这就是我正在运行的和我的输出:
$ ./test.pl '$1' '$2'
Dir /var/foo/
File baz
Dir $1
File $2
答案 0 :(得分:1)
您想使用String::Substitution。
$ perl -E'
use String::Substitution qw( interpolate_match_vars last_match_vars );
my ($str, $pat, $x_template, $y_template) = @ARGV;
$str =~ $pat
or die("Didn'\''t match\n");
my $x = interpolate_match_vars($x_template, last_match_vars());
my $y = interpolate_match_vars($y_template, last_match_vars());
say "x: $x";
say "y: $y";
' \
abcdef '(.)c(.)' '$1' '$2'
x: b
y: d