检查参数是文件名还是包含文件名列表的文件的最佳方法是什么?

Ore*_*ail 2 unix linux perl

我正在写一个Perl脚本,我想让用户输入一个文件或包含$ ARGV [0]中文件列表的文件.

我正在做的当前方式是检查文件名是否以@开头,如果是,则将该文件视为文件名列表.

这绝对不是理想的做法,因为我注意到@是bash中的一个特殊字符(顺便说一下它做了什么?我只看到它在bash中的$ @中使用过).

And*_*ich 5

您可以在命令行上指定其他参数来区别对待,例如

perl script.pl file
Run Code Online (Sandbox Code Playgroud)

用于阅读文件的内容,或

perl script.pl -l file
Run Code Online (Sandbox Code Playgroud)

用于从文件中读取文件列表.

您可以使用getopt模块来更轻松地解析输入参数.

  • 我推荐[Getopt :: Long](http://perldoc.perl.org/Getopt/Long.html)而不是[Getopt :: Std](http://perldoc.perl.org/Getopt/Std.html) .它几乎可以完成Getopt :: Std所做的一切,还有更多(特别是长选项名称,如`-list file`.) (4认同)
  • 绝对是个好主意.这就是`wget`所做的事情,例如``wget url`或`wget -i file-containing-list-of-urls`. (2认同)