eak has asked for the wisdom of the Perl Monks concerning the following question:

I am having some problems with tie and DBI. I have boiled the problem down to this short example. Basically, when you run the code below, you get the following error:
Can't use an undefined value as an ARRAY reference at ./example.pl lin +e 37.

But if you comment out the line
$hash{blah} = 1;

Everything works as expected. FETCH returns an anonymous array and foreach properly iterates through it printing out the following:
$user = { 'val' => 1 };

Can anyone explain why when you tie a hash, then tie another inside a FETCH call, you see these problems? thanks very much, --eric
#!/usr/bin/perl -w package MyTie1; sub TIEHASH { bless {}, shift; } sub STORE { my $self = shift; my ($key, $value) = @_; $self->{$key} = $value; } package MyTie2; sub TIEHASH { bless {}, shift; } sub FETCH { my $self = shift; my $key = shift; my %hash; tie %hash, 'MyTie1'; $hash{blah} = 1; return [{val => 1}]; } package main; my %hash; tie %hash, 'MyTie2'; use Data::Dumper; foreach my $user (@{$hash{one}}){ print Data::Dumper->Dump([$user],['user']); }

Replies are listed 'Best First'.
(tye)Re: tie-ing then tie-ing again
by tye (Sage) on Apr 11, 2001 at 19:37 UTC

    Looks like a bug in Perl to me. You can't used a tied variable inside of another tied variable's FETCH. I did some experimenting and came up with the following case that also illustrates the problem but with fewer moving parts inside the FETCH call:

    #!/usr/bin/perl -w package MyTie1; sub TIEHASH { bless {}, shift; } sub FETCH { my $self = shift; my $key = shift; return [{foo=>'bar'}]; } package MyTie2; sub TIEHASH { my $self= {}; my %hash; $self->{hash}= \%hash; tie %hash, 'MyTie1'; bless $self, shift; } sub FETCH { my $self = shift; my $key = shift; return [{val => $self->{hash}->{key}}]; } package main; my %hash; tie %hash, 'MyTie2'; use Data::Dumper; foreach my $user (@{$hash{one}}){ print Data::Dumper->Dump([$user],['user']); } __END__ Can't use an undefined value as an ARRAY reference at refetch.pl line +33.
    I recommend you report this using the perlbug script that comes with Perl.

            - tye (but my friends call me "Tye")