Re: wantarray and Tied Hashed

2001-05-25 Thread Paul Makepeace

On Thu, May 24, 2001 at 04:35:52PM +0100, Cross David - dcross wrote:
   my @array = $h{two};

I bumped into this in 1997 and became convinced list contexts aren't
propagated to the effective sub call. If you look at the above line,
there's something very odd seeming about it anyway, and it's not
necessarily clear WTF is going on :-) At that time I didn't really have
resources/time to mention it to anyone and worked around it (them days of
riding back to Reading at 3am or sleeping on a couch ready for a 7am
start, etc -- and I were grateful!)

The source for what I was working on is locked on an NT box (boo!) at
work; I'll check there and see what I did (FWIW; probably not exciting).

Paul



Re: wantarray and Tied Hashed

2001-05-25 Thread Simon Cozens

On Thu, May 24, 2001 at 11:28:27AM +0100, Cross David - dcross wrote:
 my @array = $h{two};
  ^
In perl 5 at least, *this* is your scalar context.

-- 
Little else matters than to write good code.
-- Karl Lehenbauer



RE: wantarray and Tied Hashed

2001-05-25 Thread Cross David - dcross

From: Simon Cozens [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 25, 2001 12:02 PM

 On Thu, May 24, 2001 at 11:28:27AM +0100, Cross David - dcross wrote:
  my @array = $h{two};
   ^
 In perl 5 at least, *this* is your scalar context.

Good point. But even if I change it to:

my @array = @h{two};

FETCH doesn't get called in list context (instead it gets called once in
scalar context for each key in the list - in this case once). I can see why
Perl does this and I _almost_ agree that it's the right thing.

Anyway, as I said before, you can work around it with

my @array = tied(%h)-FETCH('two');

If anyone is interested, Tie::Hash::Regex is currently winging its way to
your favourite CPAN mirror.

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.



Re: wantarray and Tied Hashed

2001-05-25 Thread David Cantrell

On Fri, May 25, 2001 at 12:15:44PM +0100, Cross David - dcross wrote:

 Anyway, as I said before, you can work around it with
 
 my @array = tied(%h)-FETCH('two');
 
 If anyone is interested, Tie::Hash::Regex is currently winging its way to
 your favourite CPAN mirror.

I wonder, could you do some magic with the calling stack so that your
FETCH can Do The Right Thing?

-- 
David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/

  Rip, Mix, Burn, unless you're using our most advanced operating system
   in the world which we decided to release incomplete just for a laugh



Re: wantarray and Tied Hashed

2001-05-25 Thread David Cantrell

On Fri, May 25, 2001 at 12:36:59PM +0100, Cross David - dcross wrote:

 From: David Cantrell [EMAIL PROTECTED]
 
  I wonder, could you do some magic with the calling stack so that your
  FETCH can Do The Right Thing?
 
 Or, I could just accept that I'm a BAD MAN who is trying to PERVERT PERL in
 NASTY WAYS.

No, you're confusing yourself with Damian :-)

-- 
David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/

  Rip, Mix, Burn, unless you're using our most advanced operating system
   in the world which we decided to release incomplete just for a laugh



Re: wantarray and Tied Hashed

2001-05-25 Thread Piers Cawley

David Cantrell [EMAIL PROTECTED] writes:

 On Fri, May 25, 2001 at 12:36:59PM +0100, Cross David - dcross wrote:
 
  From: David Cantrell [EMAIL PROTECTED]
  
   I wonder, could you do some magic with the calling stack so that your
   FETCH can Do The Right Thing?
  
  Or, I could just accept that I'm a BAD MAN who is trying to PERVERT PERL in
  NASTY WAYS.
 
 No, you're confusing yourself with Damian :-)

[FX: points to Symbol::Approx::Sub]

Are you *quite* sure about that?

-- 
Piers Cawley
www.iterative-software.com




wantarray and Tied Hashed

2001-05-24 Thread Cross David - dcross

Apologies for dragging us off-topic again...

Am I missing something obvious here?

#!/usr/bin/perl -w

use strict;

package Tie::Hash::Test;
use Tie::Hash;
use vars qw(@ISA);

@ISA = 'Tie::StdHash';

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.

Any clues?

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.



RE: wantarray and Tied Hashed

2001-05-24 Thread Cross David - dcross


[snip]

An update -

Calling FETCH like this:

$scalar = tied(%h)-FETCH('one');
@array = tied(%h)-FETCH('two');

Does the 'right' thing. So it's certainly something in the tie interface.

Dave...
[contemplating searching the perl source code]

-- 


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.



Re: wantarray and Tied Hashed

2001-05-24 Thread David Cantrell

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.

-- 
David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/

  Rip, Mix, Burn, unless you're using our most advanced operating system
   in the world which we decided to release incomplete just for a laugh



Re: wantarray and Tied Hashed

2001-05-24 Thread David Cantrell

On Thu, May 24, 2001 at 12:28:39PM +0100, Cross David - dcross wrote:

 Calling FETCH like this:
 
 $scalar = tied(%h)-FETCH('one');
 @array = tied(%h)-FETCH('two');
 
 Does the 'right' thing. So it's certainly something in the tie interface.

Well yes, because you're just calling a plain ol' object method and so the
tie doesn't get a chance to work its magic.

-- 
David Cantrell | [EMAIL PROTECTED] | http://www.cantrell.org.uk/david/

  Rip, Mix, Burn, unless you're using our most advanced operating system
   in the world which we decided to release incomplete just for a laugh



RE: wantarray and Tied Hashed

2001-05-24 Thread Cross David - dcross

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.