perl中的函数原型

mad*_*dia 3 perl subroutine subroutine-prototypes

我正在尝试使用内置推送功能的类似功能创建子程序mypush,但下面的代码无法正常工作.

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

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

rua*_*akh 11

在这一行:

    myPush(@planets,"Test");
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

或者至少用它的原型预先声明它:

    sub myPush (\@@);

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

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