如何在另一个perl脚本中调用具有8个参数的子例程

时间:2013-11-21 10:28:16

标签: perl parameter-passing subroutine

我试图从我的perl脚本(sample1.pl)中调用另一个perl脚本(sample.pl)中有8个参数的子例程。 我收到这个错误。 错误显示为

"sum of 8 numbers is 0Illegal character in prototype for main::callng_prgm : $a,$b,$c,$d,$e,$f,$g,$h at D:/workspace/sdff/sample.pl line 4.
Use of uninitialized value $b in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $a in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $c in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $d in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $e in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $f in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $g in addition (+) at D:/workspace/sdff/sample.pl line 6.
Use of uninitialized value $h in addition (+) at D:/workspace/sdff/sample.pl line 6.
"
你能建议我怎么做吗

sample1.pl

require 'sample.pl';
use strict;
use warnings;
my $sa = main_prgm();
sub main_prgm {
    eval(callng_prgm(10,12,15,14,16,12,12,12));
}

sample.pl

use strict;
use warnings;
our ($a,$b,$c,$d,$e,$f,$g,$h);
sub callng_prgm ($a, $b, $c, $d, $e, $f, $g, $h) {
    my $z = ($a+$b+$c+$d+$e+$f+$g+$h);
    print "sum of 8 numbers is $z";
}
1;

2 个答案:

答案 0 :(得分:1)

只需定义您的子类:

sub callng_prgm
{
  my ($a,$b,$c,$d,$e,$f,$g,$h) = @_
  my $z= ($a+$b+$c+$d+$e+$f+$g+$h);
  print "sum of 8 numbers is $z";
}

答案 1 :(得分:1)

为什么要限制自己使用8个参数?这适用于任意数量的参数。

#!/usr/bin/perl

use strict;
use warnings;
sub callng_prgm
{
   my $z;
   foreach (@_) {$z += $_};
   print $z;
}
callng_prgm(10,12,15,14,16,12,12,12);