hashref strange side effects

P

penguinista

# load a map into memory
sub readmap
{ my ($map, $file) = @_; # $map is a hashref here
my ($line, $type, $name);
return if eof $file;
$line = <$file>;
chomp $line;
if ($line =~ /\{\s*(\w+)\s+(\w+)/)
{ $type = $1; $name = $2;
if (!defined($map->{$name}))
{ $map->{$name} = {'parent'=>$map, 'name'=>$name, 'type'=>$type}; }
&readmap($map->{$name}, $file);
}
elsif ( $line =~ /(\w+)=(.+)/ )
{ $map->{$1} = $2; # this sets field value, then shoots off to garbage
}
elsif ($line =~ /\}/)
{ return; }
else {}; # error
}

It the above subroutine, the line $map->{$1} = $2 assigns key+value into
the hash as expected, then shoots off to a sub DESTROY {} in package
IO::Handle; before returning to my code after the call to the subroutine
that first calls recursive routine readmap. $1 = 'val', $2 = '"Login"'

running perl 5.8.5 in cygwin.

any ideas?
 
P

penguinista

penguinista said:
# load a map into memory
sub readmap
{ my ($map, $file) = @_; # $map is a hashref here
my ($line, $type, $name);
return if eof $file;
$line = <$file>;
chomp $line;
if ($line =~ /\{\s*(\w+)\s+(\w+)/)
{ $type = $1; $name = $2;
if (!defined($map->{$name}))
{ $map->{$name} = {'parent'=>$map, 'name'=>$name, 'type'=>$type}; }
&readmap($map->{$name}, $file);
}
elsif ( $line =~ /(\w+)=(.+)/ )
{ $map->{$1} = $2; # this sets field value, then shoots off to garbage
}
elsif ($line =~ /\}/)
{ return; }
else {}; # error
}

It the above subroutine, the line $map->{$1} = $2 assigns key+value into
the hash as expected, then shoots off to a sub DESTROY {} in package
IO::Handle; before returning to my code after the call to the subroutine
that first calls recursive routine readmap. $1 = 'val', $2 = '"Login"'

running perl 5.8.5 in cygwin.

any ideas?

Looks like I forgot the itteration loop. still doesn't quite explain
the debugger results.
 
A

Anno Siegel

penguinista said:
# load a map into memory
sub readmap
{ my ($map, $file) = @_; # $map is a hashref here
my ($line, $type, $name);
return if eof $file;
$line = <$file>;
chomp $line;
if ($line =~ /\{\s*(\w+)\s+(\w+)/)
{ $type = $1; $name = $2;
if (!defined($map->{$name}))
{ $map->{$name} = {'parent'=>$map, 'name'=>$name, 'type'=>$type}; }
&readmap($map->{$name}, $file);
}
elsif ( $line =~ /(\w+)=(.+)/ )
{ $map->{$1} = $2; # this sets field value, then shoots off to garbage
}
elsif ($line =~ /\}/)
{ return; }
else {}; # error
}

It the above subroutine, the line $map->{$1} = $2 assigns key+value into
the hash as expected, then shoots off to a sub DESTROY {} in package
IO::Handle; before returning to my code after the call to the subroutine
that first calls recursive routine readmap. $1 = 'val', $2 = '"Login"'

running perl 5.8.5 in cygwin.

any ideas?

Presumably the file handle in $file goes out of scope on return from
that call. The line you mention just happens to be the last line
executed in the sub. There's nothing mysterious about it. Why is
it bothering you?

Anno
 
P

penguinista

Anno said:
Presumably the file handle in $file goes out of scope on return from
that call. The line you mention just happens to be the last line
executed in the sub. There's nothing mysterious about it. Why is
it bothering you?

Anno

I'm used to the debugger showing the return from subroutine. I guess
it's a C vs. perl in debugger thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top