1.4 子程序返回值 子程序可以向其他编程语言一样使用 return 语句来返回函数值。 如果没有使用 return 语句,则子程序的最后一行语句将作为返回值。 #!/usr/bin/perl # 方法定义 sub add_a_b{ # 不使用 return $_[0]+$_[1]; # 使用 return # return $_[0]+$_[1]; } print add_a_b(1,
print"Array 1: @{$array_ref1}\n"; print"Array 2: @{$array_ref2}\n"; print"Hash 1: ".join(", ",%{$hash_ref1})."\n"; print"Hash 2: ".join(", ",%{$hash_ref2})."\n"; 以上代码中 return_multiple_data 子程序返回了四个数据结构的引用。 在调用程序中,我们通过解引用这些...
return ($folder_name,$path_name); } # endof subroutine get_path_dir_name my $file = $ARGV[0]; # 命令行中第一个参数为$ARGV[0],下标从0开始,$0表示脚本名 my ($folder_name,$path_name) = &get_path_dir_name($file); print " (\$folder_name,\$path_name) = ($folder_name,$path...
子程序可以向其他编程语言一样使用 return 语句来返回函数值。 如果没有使用 return 语句,则子程序的最后一行语句将作为返回值。 实例 #!/usr/bin/perl # 方法定义 sub add_a_b{ # 不使用 return $_[0]+$_[1]; # 使用 return # return $_[0]+$_[1]; } print add_a_b(1, 2) 以上程序执行...
@_ 是函数传参时放置参数的数组,可以从中取实参;$_ 是默认参数的意思,指的是在不指定的情况下,程序处理的上一个变量;shift 是将数组的第一个元素$array[0] 移走, 并将这个元素回传(return) (堆栈一节有详细讲解)。 shift函数是取数组的第一个元素,缺省就取@_的第一个函数,这句一般用在程序的开头,用...
# doing this in an array context, the # catalog tag will be returned as a (type, catalog, id) # triplet, where each member of the triplet will behave as # an integer or a string, depending on context. # If instead this next line provided a scalar context, e.g. ...
例如 - @array = (1, 2, 'Hello'); @array = qw/This is an array/; 123 第二行使用qw //运算符,它返回一个字符串列表,用空格分隔分隔的字符串。 在这个例子中,这导致了一个四元素阵列; 第一个元素是'this',last(第四个)是'array'。 这意味着您可以使用以下不同的行 -...
return @array; in an action that is forwarded to is going to return a scalar, i.e. how many items are in that array, which is probably not what you want. If you need to return an array then return a reference to it, or stash it like so:...
With a second non-null argument returns an absolute path. Double-byte characters will not be translated. "Cygwin::mount_table()" Returns an array of [mnt_dir, mnt_fsname, mnt_type, mnt_opts]. perl -e 'for $i (Cygwin::mount_table) {print join(" ",@$i),"\n";}' /bin c:\...
return $this; # Return the reference to the hash., #从此函数返回后$this引用被销毁,但保存了对该hash表的引用 } ###array to save sub new { my $class = shift; #获取要求的类名,允许类被继承,以类名作为第一个参数 my @para = @_;#实例hash table my $this...