in reply to Re: Perl OO - Class Data
in thread Perl OO - Class Data

In general, you can't use objects as keys to a hash.

Just to expand on that a little...

If you attempt to use an object as a hash key, Perl will convert the reference to a text string and use that as the actual hash key. While this is quite handy for generating unique IDs, it also means that the keys of the hash are no longer usable as objects. (This applies to all references, actually, not just objects.)

So using keys %ds to retrieve your User objects can't work. However, if you construct %ds with $ds{$objref} = $objref (the posted code doesn't put any data into %ds, so I don't know whether that's how you're doing it or not), then you should be able to retrieve all your objects using values %ds in your foreach.

But it also looks like your test sub, in addition to only looking for Users on the opposing team, is also going against every one of them rather than a random one. If you want randomness, then you'll probably want to do something like

my @objlist = values %ds; my $randobj = $objlist[rand @objlist];

Replies are listed 'Best First'.
Re^3: Perl OO - Class Data
by yoda54 (Monk) on Jun 26, 2006 at 19:14 UTC
    That cleared up a lot of confusion for me. Thanks!!!