From: David Cantrell <[EMAIL PROTECTED]>
Sent: Thursday, May 24, 2001 1:57 PM

> On Thu, May 24, 2001 at 11:28:27AM +0100, Cross David - dcross wrote:
> 
> > package Tie::Hash::Test;
> > 
> > sub FETCH {
> >   print "wantarray is ", wantarray ? "true\n" : "false\n";
> >   return $_[0]->{$_[1]};
> > }
> > 
> > package main;
> > 
> > my %h;
> > tie %h, 'Tie::Hash::Test';
> > 
> > %h = (one => 1, two => 2);
> > 
> > my $scalar = $h{one};
> > my @array = $h{two};
> > 
> > This prints out "wantarray is false" on both accesses. To me, this
implies
> > that Perl is doing something strange behind the scenes and is forcing
the
> > FETCH call to always be in scalar context, when I'd expect the second
call
> > to be evaluated in list context.
> 
> FETCH will indeed be called in scalar context.  You can only store scalars
> as hash values, and so you will never want to get an array out of them.
> 
> Even if you do:
> 
> my @array=@h{@multiple_keys}
> 
> FETCH will still be called once for each key, in scalar context.

All true, and very valid. But you forget that we're talking about _tied_
hashes here. And this means that all bets about behaviour are off.

In this instance, I found the 'problem' when working on Tie::Hash::Regex. In
this module you can lookup a hash value using a regex. It's therefore very
likely that one regex would match a number of hash keys and in that case I'd
like to know if FETCH is called in list context so I can return _all_ of the
matching keys.

This is the first time that my imagination has come up with something that
you can't do in Perl and it's vaguely unsettling. I'm betting it's just an
oversight and if I mention it on p5p we'll get a fix pretty pronto :)

Dave...

-- 


The information contained in this communication is
confidential, is intended only for the use of the recipient
named above, and may be legally privileged. If the reader 
of this message is not the intended recipient, you are
hereby notified that any dissemination, distribution or
copying of this communication is strictly prohibited.  
If you have received this communication in error, please 
re-send this communication to the sender and delete the 
original message or any copy of it from your computer
system.

Reply via email to