6

在 perl 子例程定义中声明数组类型参数时遇到编译错误。我的完整代码如下:

use Data::Dumper;
use Win32;
use Win32::Service;
use strict;
use warnings;
my @Services = qw(NNMAction RpcEptMapper smstsmgr SNMPTRAP);
my $server   = 'nnmi.hclt.corp.hcl.in';
ServiceStatus($server , @Services);

sub ServiceStatus ($serverName,@serverServices)
{       my %statcodeHash = (     '1' => 'stopped',
                             '2' => 'start pending',
                             '3' => 'stop pending',
                             '4' => 'running',
                             '5' => 'continue pending',
                             '6' => 'pause pending',
                             '7' => 'paused'            );

  foreach my $serv (@serverServices)
  {     my %status;
     my $ret = Win32::Service::GetStatus($serverName , $serv , \%status);
     if ($ret)
     {       print "success \t$statcodeHash{$status{CurrentState}} \t$serv\n";
     }
     else
     {       print Win32::FormatMessage(Win32::GetLastError()), "\n";
     }
   }
 }   

编译错误

>perl -w perl_RemoteServiceStatus.pl
Prototype after '@' for main::ServiceStatus : $serverName,@serverServices at per
l_RemoteServiceStatus.pl line 21.
Illegal character in prototype for main::ServiceStatus : $serverName,@serverServ
ices at perl_RemoteServiceStatus.pl line 21.
main::ServiceStatus() called too early to check prototype at perl_RemoteServiceS
tatus.pl line 16.
Global symbol "@serverServices" requires explicit package name at perl_RemoteSer
viceStatus.pl line 31.
Global symbol "$serverName" requires explicit package name at perl_RemoteService
Status.pl line 33.
Execution of perl_RemoteServiceStatus.pl aborted due to compilation errors.

请帮我调试这些代码。我相信这对某些人来说是小菜一碟。

4

4 回答 4

9

这很简单:如果你不知道原型是如何工作的,就不要使用它们。要让您的代码运行,请从以下位置更改子例程声明:

sub ServiceStatus ($serverName,@serverServices)
{ #...

到:

sub ServiceStatus {
    my ($serverName, @serverServices) = @_;

编辑:如果您需要将多个数组/哈希传递给子例程,或者应该在其他一些值之前传入数组/哈希,则必须通过引用传递它:

sub complex_params {
    my ($array1, $scalar, $hash, $array2) = @_;

    # dereference
    my @a1 = @$array1;
    my @a2 = @$array2;
    my %h  = %$hash;

    #...
}

# reference
complex_params(\@some_array, $some_scalar, \%some_hash, \@other_array);
于 2012-12-20T09:49:14.083 回答
6

Perl 原型不是用于命名参数,甚至不是为它们赋予类型,它们是用于创建评估上下文。您需要像这样修改子例程:

sub ServiceStatus ($@){
  my ($serverName,@serverServices) = @_;   
  # ...
}

或完全摆脱原型:

sub ServiceStatus {
  my ($serverName,@serverServices) = @_;   
  # ...
}
于 2012-12-20T09:49:10.497 回答
4
sub ServiceStatus
{
    my ($serverName,@serverServices) = @_; # Declare variables and populate from @_, the parameter list.
    ...

}
于 2012-12-20T09:49:01.313 回答
3

你在做什么?

第一的!不要尝试使用原型:

sub ServiceStatus($@){

}

让我们看看,你想要什么:

将数组或哈希传递给函数是一个非常古老的技巧:

sub ServiceStatus{
my ($firstArgument, $refToSecondArgumentWhichIsArray) = @_;

#return undef unless defined($firstArgument&&$refToSecondArgumentWhichIsArray);
...
}

这个怎么用?

ServiceStatus($serverName, \@serverServices);

以及如何处理 refs?

$refToArray->[0]; <- first element of array you pass
@{$refToArray}    <- array you pass to function
于 2012-12-20T09:54:33.507 回答