将数组传递给Perl sub时"参数太多"?

Vij*_*jay 21 perl

我在perl中有一个函数

sub create_hash()
{
my @files = @_;

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

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

         }
      }

}
Run Code Online (Sandbox Code Playgroud)

我通过传递如下所示的数组参数来调用此函数:

create_hash( @files2);
Run Code Online (Sandbox Code Playgroud)

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

Too many arguments for main::create_hash at ....
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?

我的perl版本是:

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

cjm*_*cjm 65

你的问题就在这里:

sub create_hash()
{
Run Code Online (Sandbox Code Playgroud)

()是一个原型.在这种情况下,它表示不create_hash带参数.当你试图传递一些时,Perl抱怨道.

应该是这样的

sub create_hash
{
Run Code Online (Sandbox Code Playgroud)

通常,您不应该将原型与Perl函数一起使用.它们不像大多数其他语言的原型.它们确实有用,但这在Perl中是一个相当高级的主题.

  • 上帝保佑 .打破了我的脑袋:) (2认同)