如何在Perl中解析带引号和不带引号的参数? [英] How to parse quoted as well as unquoted arguments in Perl?
本文介绍了如何在Perl中解析带引号和不带引号的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的perl脚本正确地将两个用空格分隔的命令行参数解析为两个变量:
I want my perl script to correctly parse two command line arguments separated by a space into two variables:
$ cat 1.pl
print "arg1 = >$ARGV[0]<\n";
print "arg2 = >$ARGV[1]<\n";
$ perl 1.pl a b
arg1 = >a<
arg2 = >b<
$ perl 1.pl "a b"
arg1 = >a b<
arg2 = ><
$
是否有一种通用的处理方式,而不是试图检测报价是否
Is there a generic way of dealing with this rather than trying to detect whether quotes were used or not?
推荐答案
数据通过外壳传递给Perl。
The data is passed to Perl by the shell.
-
程序ab
将发送a
和b
-
程序'a b'
将发送ab
-
程序 ab
将发送ab
-
程序a\ b
将发送ab
program a b
will senda
andb
program 'a b'
will senda b
program "a b"
will senda b
program a\ b
will senda b
Perl拥有AFAIK,无法分辨出最后三个之间的差异。
Perl has, AFAIK, no way of telling the difference between any of the last three.
您可以分割
每个关于空格的参数,这将获得您所描述的效果……但这将意味着以不同于其他所有应用程序的方式工作。
You could split
every argument on spaces, and that would get the effect you are describing … but it would mean working in a different way to every other application out there.
这篇关于如何在Perl中解析带引号和不带引号的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文