English 中文(简体)
为什么我的休养职能永远不会结束?
原标题:Why does my Perl recursive function never end?

我试图写以下休养职能。 问题在于它永远不会结束,我无法理解为什么:

    sub do_smth(@first, @second){
    my @tmp_first = @first;
    $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @second);
    }
    my @tmp_second = @second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@first, @tmp_second);
    }

}
最佳回答

该法典甚至没有编纂。 如果没有警告和严格限制,你就会发现这些错误:

Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 5, near "$tmp_first)"
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 10, near "$tmp_second)"
Execution aborted due to compilation errors.

• 发出警告和严格警告:

Illegal character in prototype for main::do_smth : @first,@second at so.pl line 4.
Global symbol "@first" requires explicit package name at so.pl line 5.
Global symbol "$tmp" requires explicit package name at so.pl line 6.
Global symbol "$tmp_first" requires explicit package name at so.pl line 6.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 6, near "$tmp_first)"
Global symbol "@second" requires explicit package name at so.pl line 8.
Global symbol "@second" requires explicit package name at so.pl line 10.
Global symbol "$tmp" requires explicit package name at so.pl line 11.
Global symbol "$tmp_second" requires explicit package name at so.pl line 11.
Type of arg 1 to shift must be array (not scalar dereference) at so.pl line 11, near "$tmp_second)"
Global symbol "@first" requires explicit package name at so.pl line 13.
Execution aborted due to compilation errors.

页: 1

use warnings;
use strict;

sub do_smth (@@);  # predeclaration needed since the prototyped sub
                     # is called recursively
sub do_smth (@@) {
    my ($first, $second) = @_;
    my @tmp_first = @$first;
    my $tmp = shift(@tmp_first);
    if (@tmp_first > 0){
        do_smth(@tmp_first, @$second);
    }
    my @tmp_second = @$second;
    $tmp = shift(@tmp_second);
    if (@tmp_second > 0){
        do_smth(@$first, @tmp_second);
    }
}
问题回答

页: 1

更进一步。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签