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

前端之家收集整理的这篇文章主要介绍了将数组传递给Perl sub时,“太多参数”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在perl中有一个函数
  1. sub create_hash()
  2. {
  3. my @files = @_;
  4.  
  5. foreach(@files){
  6. if(/.text/)
  7. {
  8.  
  9. open($files_list{$_},">>$_") || die("This file will not open!");
  10.  
  11. }
  12. }
  13.  
  14. }

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

  1. create_hash( @files2);

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

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

我在这里做什么错?

我的perl版本是:

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

解决方法

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

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

应该看起来像

  1. sub create_hash
  2. {

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

猜你在找的Perl相关文章