将数组传递给Perl sub时,“太多参数”

前端之家收集整理的这篇文章主要介绍了将数组传递给Perl sub时,“太多参数”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在perl中有一个函数
sub create_hash()
{
my @files = @_;

        foreach(@files){
         if(/.text/)
         {

         open($files_list{$_},">>$_") || die("This file will not open!");

         }
      }

}

我通过传递一个数组参数来调用这个函数,如下所示:

create_hash( @files2);

该数组中有大约38个值。
但是我收到编译错误

Too many arguments for main::create_hash at ....

我在这里做什么错?

我的perl版本是:

This is perl,v5.8.4 built for i86pc-solaris-64int
(with 36 registered patches,see perl -V for more detail)

解决方法

你的问题在这里:
sub create_hash()
{

()是一个prototype.在这种情况下,它表示create_hash不带参数。当你尝试传递一些,Perl抱怨。

应该看起来像

sub create_hash
{

一般来说,you should not use prototypes with Perl functions.他们不像大多数其他语言的原型。他们有使用,但这是Perl中相当高级的话题。

原文链接:https://www.f2er.com/Perl/172959.html

猜你在找的Perl相关文章