【发布时间】:2014-07-15 23:26:22
【问题描述】:
作为我课程的一部分,我在过去几周第一次学习了 perl 编程语言。我一直在编写小函数并进行函数调用。我写了一个字符串匹配函数。
use strict;
use warnings;
sub find_multi_string {
my ($file, @strings) = @_;
my $fh;
open ($fh, "<$file");
#store the whole file in an array
my @array = <$fh>;
for my $string (@strings) {
if (grep /$string/, @array) {
next;
} else {
die "Cannot find $string in $file";
}
}
return 1;
}
find_multi_string('file name', 'string1','string2','string3','string4','string 5');
在上面的脚本中,我在函数调用中传递参数。该脚本有效。 但我想知道是否有办法在程序本身的数组中指定文件名和string1...string n,然后调用函数。
find_multi_string();
【问题讨论】:
-
为什么?你想达到什么目的?
-
使用autodie!
标签: perl function subroutine