Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

tie-ing then tie-ing again

by eak (Monk)
on Apr 11, 2001 at 03:20 UTC ( #71544=perlquestion: print w/replies, xml ) Need Help??

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")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://71544]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-01-28 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?