perl解析命令行参数

use*_*275 -4 perl concatenation command-line-arguments

我需要编写一个perl程序来接受可以连接的命令行参数,例如

myPerl.pl -l -c -d    same as    myPerl.pl  -lcd
Run Code Online (Sandbox Code Playgroud)

唯一的限制是我不能使用诸如"Getopts"之类的模块构建,任何人都知道我可以做的任何其他事情来轻松实现这一点.

run*_*rig 10

看看捆绑下的Getopt :: Long.

更新:哦,没有模块.这是功课.那么,从Getopt :: Long复制代码.

  • +1"从Getopt :: Long复制代码" (3认同)

tch*_*ist 5

没有模块? 真? 这就像这样 Perl4样式.:)

好的,所以我们总是在perl4中做到这一点:

ARG: while (@ARGV && $ARGV[0] =~ s/^-(?=.)//) {
OPT:    for (shift @ARGV) {

            m/^$/        && do {                                 next ARG; };
            m/^-$/       && do {                                 last ARG; };

            s/^0//       && do { $nullpaths++;                   redo OPT; };
            s/^f//       && do { $force++;                       redo OPT; };
            s/^l//       && do { $reslinking++;                  redo OPT; };
            s/^I//       && do { $inspect++;                     redo OPT; };
            s/^i//       && do { $careful++;                     redo OPT; };
            s/^v//       && do { $verbose++;                     redo OPT; };
            s/^V//       && do { $verbose += 2;                  redo OPT; };  # like two -v's
            s/^m//       && do { $renaming++;                    redo OPT; };
            s/^n//       && do { $nonono++;                      redo OPT; };
            s/^N//       && do { $nonono += 2;                   redo OPT; };  # like two -n's
            s/^q//       && do { $quiet++;                       redo OPT; };

            s/^F(.*)//s  && do { push @flist, $1 || shift @ARGV; redo OPT; };

            &usage("Unknown option: $_");
        }
    }
Run Code Online (Sandbox Code Playgroud)

那不是那么膨胀吗?:)

同样的方法今天仍然有效,但它可能会让你谈到.