首页 > 解决方案 > Perl variables access problem with recursive call sub

问题描述

I need to convert the database to SQL. I write it on perl. It's here:

P.S. Simplifyed

#!/usr/bin/perl
use utf8;
use strict;
use warnings;

my @tables = (
    ['clients', ['id'], 'id', '', 
      [['accounts', ['id'], '', 'id_client', 0]]
    ]
);

sub ProcessTables {
    my ($tbl, $id_value) = @_;
    my ($i, $i2, $sth);
    my @arr;
    my $arr_count = $#$tbl;
    for ( $i=0; $i<=$arr_count; $i++ ) {
        if ( $tbl->[$i]->[4] != 0) {
            @arr = @{$tbl->[$i]->[4]};
            ProcessTables( \@arr, 'blablalba' );
        };
    }
};

ProcessTables(\@tables);

When I get into the procedure a second time (after the line @arr = ....) by the passed parameters and access to them, there are no problems. Everything is smooth and beautiful. Everything changes radically on the line "if ( $tbl->[$i]->[4] != 0)". After that, it seems that I did not go here. All variables refer to the data that I saw when I first entered this procedure. Before the call "if," all links pointed to an array with data "accounts". After the call, they began to point to the "clients" again. Please tell me what is the matter? Why it happens?

P.S. I debug it under linux. This is output:

  DB<2>
main::ProcessTables(TestRecurce2.pl:19):
19:         my $arr_count = $#$tbl;
  DB<2>
main::ProcessTables(TestRecurce2.pl:20):
20:         for ( $i=0; $i<=$arr_count; $i++ ) {
  DB<2>
main::ProcessTables(TestRecurce2.pl:21):
21:             if ( $tbl->[$i]->[4] != 0) {
  DB<2> p $tbl
ARRAY(0x5593be5baba0)
  DB<3> p  $tbl->[$i]
ARRAY(0x5593be5fbf78)
  DB<4> p $tbl->[$i]->[4]
0
  DB<5> n
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.

标签: perl

解决方案


推荐阅读