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];

In reply to Re^2: Perl OO - Class Data by dsheroh
in thread Perl OO - Class Data by yoda54

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.