V
vnick
Hi,
(before I start, sorry for the length of this posting)
I have this code (test case):
--------------------------------------------------------
#!/usr/local/bin/perl
use strict;
use vars qw ($d %spec %spec_cc);
# Nested Hash/Array hierarchy:
%spec = (
RIF_SOURCE_WIDTH => {
FMT => {genrule => "0x20", },
WIDTH => {genrule => "look_up(FMT)",
FMT => {
10 => "mul(ORIG_WIDTH,4)",
20 => "sub(ORIG_WIDTH,1)",
_OTHER_ => "sub(mul(int(div(ORIG_WIDTH,2)),2),1)",
_RANGE_ => [0,3, "and(ORIG_WIDTH,~3)"], #
},
},
},
);
######################################################################
# MAIN:
print ">>> " . $spec{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_} . "\n";
local $d = 0;
%spec_cc = ();
&struct_copy (\%spec, \%spec_cc);
print "### " . $spec_cc{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_} . "\n";
print "### " . $spec_cc{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_}[2] .
"\n";
######################################################################
sub struct_copy {
my ($oref, $cref) = @_;
my $objkey; # indexing var.
my $ret = 0; # Return value.
$d++;
if (ref($oref) eq "HASH") {
foreach $objkey (keys(%$oref)) {
printf "H-key($d): %-20s \t[$oref -> $cref]\n",$objkey;
if (ref($$oref{$objkey}) eq "HASH") {
$$cref{$objkey} = ({}); # Generate empty hash hierarchy.
&struct_copy ($$oref{$objkey},$$cref{$objkey} );
}
elsif (ref($$oref{$objkey}) eq "ARRAY") {
$$cref{$objkey} = ([]); # Generate empty array hierarchy.
&struct_copy ($$oref{$objkey},$$cref{$objkey} );
}
elsif (ref($$oref{$objkey}) eq "SCALAR" || ref($$oref{$objkey})
eq "") {
$$cref{$objkey} = $$oref{$objkey};
}
else {
warn "ERROR: Only deep hashes or arrays or a mix of them can be
copied\n";
$ret = 1;
}
}
}
elsif (ref($oref) eq "ARRAY") {
for ($objkey = 0; $objkey <= $#{@$oref}; $objkey++) {
printf "A_Ind($d): %-20s \t[$oref -> $cref]\n", $objkey;
if (ref($$oref[$objkey]) eq "HASH") {
$$cref[$objkey] = ({}); # Generate empty hash hierarchy.
&struct_copy ($$oref[$objkey],$$cref[$objkey] );
}
elsif (ref($$oref[$objkey]) eq "ARRAY") {
$$cref[$objkey] = ([]); # Generate empty array hierarchy.
&struct_copy ($$oref[$objkey],$$cref[$objkey] );
}
elsif (ref($$oref[$objkey]) eq "SCALAR" || ref($$oref[$objkey])
eq "") {
$$cref[$objkey] = $$oref[$objkey];
}
else {
warn "ERROR: Only deep hashes or arrays or a mix of them can be
copied\n";
$ret = 1;
}
}
return $ret;
}
else {
warn "ERROR: Object $$oref is neither array nor hash!\n";
$ret = 1;
}
$d--;
return $ret;
}
---------------------------------------------------------------
sub struct_copy is to copy any combination of Hashes and Arrays
(probably obvious to most readers
.
My problem is now that this piece of code works fine -
but only as long as I do not use the debugger!
Running the code without debugger gives:
----------------------------------------------------------------
H-key(1): RIF_SOURCE_WIDTH [HASH(0x4006bec0) -> HASH(0x4006bee4)]
H-key(2): WIDTH [HASH(0x4009b590) -> HASH(0x4009b5b4)]
H-key(3): FMT [HASH(0x4009b560) -> HASH(0x4009b818)]
H-key(4): _OTHER_ [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(4): _RANGE_ [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
A_Ind(5): 0 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
A_Ind(5): 1 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
A_Ind(5): 2 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
H-key(5): 10 [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(5): 20 [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(4): genrule [HASH(0x4009b560) -> HASH(0x4009b818)]
H-key(3): FMT [HASH(0x4009b590) -> HASH(0x4009b5b4)]
H-key(4): genrule [HASH(0x4003f6d0) -> HASH(0x40058f14)]
### ARRAY(0x400591a8)
### and(ORIG_WIDTH,~3)
--------------------------------------------------------------
After switching the debugger on:
--------------------------------------------------------------
Loading DB routines from perl5db.pl version 1.27
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main:
testtest.pl:7): %spec = (
main:
testtest.pl:8): RIF_SOURCE_WIDTH => {
H-key(1): RIF_SOURCE_WIDTH [HASH(0x402724d4) -> HASH(0x402724f8)]
H-key(2): WIDTH [HASH(0x4027662c) -> HASH(0x40337d04)]
H-key(3): FMT [HASH(0x4027605c) -> HASH(0x4031bc9c)]
H-key(4): _OTHER_ [HASH(0x40275e34) -> HASH(0x4005fdec)]
H-key(4): _RANGE_ [HASH(0x40275e34) -> HASH(0x4005fdec)]
Bizarre copy of ARRAY in leave at testtest.pl line 65.
at testtest.pl line 65
main::struct_copy('ARRAY(0x4003f5ec)', 'ARRAY(0x4006008c)')
called at testtest.pl line 53
main::struct_copy('HASH(0x40275e34)', 'HASH(0x4005fdec)')
called at testtest.pl line 49
main::struct_copy('HASH(0x4027605c)', 'HASH(0x4031bc9c)')
called at testtest.pl line 49
main::struct_copy('HASH(0x4027662c)', 'HASH(0x40337d04)')
called at testtest.pl line 49
main::struct_copy('HASH(0x402724d4)', 'HASH(0x402724f8)')
called at testtest.pl line 32
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.
DB<1>
---------------------------------------------------------------
So the question is why Perl (5.8.5) only reports this bizarre error
when I use the debugger?? Do I still have a bug in my code though it
works and I do not see where it could be or is that a debugger bug?
Any help appreciated - thanks!
Volker
(before I start, sorry for the length of this posting)
I have this code (test case):
--------------------------------------------------------
#!/usr/local/bin/perl
use strict;
use vars qw ($d %spec %spec_cc);
# Nested Hash/Array hierarchy:
%spec = (
RIF_SOURCE_WIDTH => {
FMT => {genrule => "0x20", },
WIDTH => {genrule => "look_up(FMT)",
FMT => {
10 => "mul(ORIG_WIDTH,4)",
20 => "sub(ORIG_WIDTH,1)",
_OTHER_ => "sub(mul(int(div(ORIG_WIDTH,2)),2),1)",
_RANGE_ => [0,3, "and(ORIG_WIDTH,~3)"], #
},
},
},
);
######################################################################
# MAIN:
print ">>> " . $spec{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_} . "\n";
local $d = 0;
%spec_cc = ();
&struct_copy (\%spec, \%spec_cc);
print "### " . $spec_cc{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_} . "\n";
print "### " . $spec_cc{RIF_SOURCE_WIDTH}{WIDTH}{FMT}{_RANGE_}[2] .
"\n";
######################################################################
sub struct_copy {
my ($oref, $cref) = @_;
my $objkey; # indexing var.
my $ret = 0; # Return value.
$d++;
if (ref($oref) eq "HASH") {
foreach $objkey (keys(%$oref)) {
printf "H-key($d): %-20s \t[$oref -> $cref]\n",$objkey;
if (ref($$oref{$objkey}) eq "HASH") {
$$cref{$objkey} = ({}); # Generate empty hash hierarchy.
&struct_copy ($$oref{$objkey},$$cref{$objkey} );
}
elsif (ref($$oref{$objkey}) eq "ARRAY") {
$$cref{$objkey} = ([]); # Generate empty array hierarchy.
&struct_copy ($$oref{$objkey},$$cref{$objkey} );
}
elsif (ref($$oref{$objkey}) eq "SCALAR" || ref($$oref{$objkey})
eq "") {
$$cref{$objkey} = $$oref{$objkey};
}
else {
warn "ERROR: Only deep hashes or arrays or a mix of them can be
copied\n";
$ret = 1;
}
}
}
elsif (ref($oref) eq "ARRAY") {
for ($objkey = 0; $objkey <= $#{@$oref}; $objkey++) {
printf "A_Ind($d): %-20s \t[$oref -> $cref]\n", $objkey;
if (ref($$oref[$objkey]) eq "HASH") {
$$cref[$objkey] = ({}); # Generate empty hash hierarchy.
&struct_copy ($$oref[$objkey],$$cref[$objkey] );
}
elsif (ref($$oref[$objkey]) eq "ARRAY") {
$$cref[$objkey] = ([]); # Generate empty array hierarchy.
&struct_copy ($$oref[$objkey],$$cref[$objkey] );
}
elsif (ref($$oref[$objkey]) eq "SCALAR" || ref($$oref[$objkey])
eq "") {
$$cref[$objkey] = $$oref[$objkey];
}
else {
warn "ERROR: Only deep hashes or arrays or a mix of them can be
copied\n";
$ret = 1;
}
}
return $ret;
}
else {
warn "ERROR: Object $$oref is neither array nor hash!\n";
$ret = 1;
}
$d--;
return $ret;
}
---------------------------------------------------------------
sub struct_copy is to copy any combination of Hashes and Arrays
(probably obvious to most readers
My problem is now that this piece of code works fine -
but only as long as I do not use the debugger!
Running the code without debugger gives:
----------------------------------------------------------------
H-key(1): RIF_SOURCE_WIDTH [HASH(0x4006bec0) -> HASH(0x4006bee4)]
H-key(2): WIDTH [HASH(0x4009b590) -> HASH(0x4009b5b4)]
H-key(3): FMT [HASH(0x4009b560) -> HASH(0x4009b818)]
H-key(4): _OTHER_ [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(4): _RANGE_ [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
A_Ind(5): 0 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
A_Ind(5): 1 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
A_Ind(5): 2 [ARRAY(0x4003f7fc) ->
ARRAY(0x400591a8)]
H-key(5): 10 [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(5): 20 [HASH(0x4005d5c8) -> HASH(0x40058f2c)]
H-key(4): genrule [HASH(0x4009b560) -> HASH(0x4009b818)]
H-key(3): FMT [HASH(0x4009b590) -> HASH(0x4009b5b4)]
H-key(4): genrule [HASH(0x4003f6d0) -> HASH(0x40058f14)]
### ARRAY(0x400591a8)
### and(ORIG_WIDTH,~3)
--------------------------------------------------------------
After switching the debugger on:
--------------------------------------------------------------
Loading DB routines from perl5db.pl version 1.27
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main:
main:
H-key(1): RIF_SOURCE_WIDTH [HASH(0x402724d4) -> HASH(0x402724f8)]
H-key(2): WIDTH [HASH(0x4027662c) -> HASH(0x40337d04)]
H-key(3): FMT [HASH(0x4027605c) -> HASH(0x4031bc9c)]
H-key(4): _OTHER_ [HASH(0x40275e34) -> HASH(0x4005fdec)]
H-key(4): _RANGE_ [HASH(0x40275e34) -> HASH(0x4005fdec)]
Bizarre copy of ARRAY in leave at testtest.pl line 65.
at testtest.pl line 65
main::struct_copy('ARRAY(0x4003f5ec)', 'ARRAY(0x4006008c)')
called at testtest.pl line 53
main::struct_copy('HASH(0x40275e34)', 'HASH(0x4005fdec)')
called at testtest.pl line 49
main::struct_copy('HASH(0x4027605c)', 'HASH(0x4031bc9c)')
called at testtest.pl line 49
main::struct_copy('HASH(0x4027662c)', 'HASH(0x40337d04)')
called at testtest.pl line 49
main::struct_copy('HASH(0x402724d4)', 'HASH(0x402724f8)')
called at testtest.pl line 32
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.
DB<1>
---------------------------------------------------------------
So the question is why Perl (5.8.5) only reports this bizarre error
when I use the debugger?? Do I still have a bug in my code though it
works and I do not see where it could be or is that a debugger bug?
Any help appreciated - thanks!
Volker