为了比较,分别将两段ASCII文本(一个L =长,一个S =短)读入@arrayOne和@arrayTwo。以下子例程&analyse从smart.pl代码中获取了两个数组引用,但是在通过perl -c smart.pl检查时抛出错误。不幸的是,我不知道为什么:
68 sub analyse {
69 my $arraysize ; my $arrLref ; my $arrSref ; my $item_L ; my $item_S ; my $value ;
70
71 $arrSref = shift ; $arrLref = shift ;
72 $item_S = shift @{ $arrSref } ;
73 $item_L = shift @{ $arrLref } ;
74
75 $arraysize = $#{ $arrSref } ;
76 while ( $arraysize > 0 ) {
77 $value = ( $item_S cmp $item_L ) ;
78 given ( $value ) {
79 when ( -1 ) {
80 push ( @mergedArray , $item_S ) ;
81 $item_S = shift @{ $arrSref }
82 }
83 when ( 0 ) {
84 push ( @mergedArray , $item_L ) ;
85 $item_S = shift @{ $arrSref } ;
86 $item_L = shift @{ $arrLref }
87 }
88 when ( 1 ) {
89 push ( @mergedArray , $item_L ) ;
90 $item_L = shift @{ $arrLref }
91 }
92 default { &die }
93 }
94 }
95 }
使用以下语句中止编译:
$ perl -c smart.pl
syntax error at smart.pl line 78, near ") {"
syntax error at smart.pl line 83, near ") {"
syntax error at smart.pl line 88, near ") {"
Global symbol "$item_L" requires explicit package name at smart.pl line 89.
Global symbol "$item_L" requires explicit package name at smart.pl line 90.
Global symbol "$arrLref" requires explicit package name at smart.pl line 90.
syntax error at smart.pl line 91, near "}"
smart.pl had compilation errors.
也许其他人有线索?预先感谢–DrP-
最佳答案
根据Perl documentation,为了使用given
和when
,需要满足两个条件:
use feature "switch";
那应该可以解释您在第78、83和88行看到的内容。
关于在第89行和第90行上看到的警告,这些警告与
use strict;
的使用有关,可以在here中找到关于这些警告的出色解释。
关于perl - Perl脚本中的奇数子例程编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450274/