2012-03-25 75 views
3

我想创建具有类似内置推式函数功能的子例程mypush,但下面的代码无法正常工作。perl函数原型

@planets = ('mercury', 'venus', 'earth', 'mars'); 
    myPush(@planets,"Test"); 

    sub myPush (\@@) { 
     my $ref = shift; 
     my @bal = @_; 
     print "\@bal : @bal\nRef : @{$ref}\n"; 
     #... 
    } 

回答

11

在这一行:

myPush(@planets,"Test"); 

的Perl目前还没有看到样机,所以它不能使用它。 (如果你打开警告,你总是应该,你会得到一个消息,main::myPush() called too early to check prototype

您可以之前创建的子程序你使用它:

sub myPush (\@@) { 
     my $ref = shift; 
     my @bal = @_; 
     print "\@bal : @bal\nRef : @{$ref}\n"; 
     #... 
    } 

    @planets = ('mercury', 'venus', 'earth', 'mars'); 
    myPush(@planets,"Test"); 
至少

要不然与它的原型预声明它:

sub myPush (\@@); 

    @planets = ('mercury', 'venus', 'earth', 'mars'); 
    myPush(@planets,"Test"); 

    sub myPush (\@@) { 
     my $ref = shift; 
     my @bal = @_; 
     print "\@bal : @bal\nRef : @{$ref}\n"; 
     #... 
    } 
0

如果您确定的功能和自己的名字,你可以把一个符号调用之前:

@planets = ('mercury', 'venus', 'earth', 'mars'); 
&myPush(@planets,"Test"); 

sub myPush (\@@) { 
    my $ref = shift; 
    my @bal = @_; 
    print "\@bal : @bal\nRef : @{$ref}\n"; 
    #... 
}