I'm trying to have a program handle multiple sockets in the same process. So for my convenience I use IO::Select, which is a beaut little module that handles sockets for me. I add sockets to be watched with $s->add($socket_handle). Then I wait and call  @ready = $s->can_read(0); which returns only the socket handles that have data waiting (so I don't block on reads - very important when I'm handling multiple sockets).

But of course I want to do different things with the data based on what socket it came from. I can't use a reference as a hash key unless I use Tie::RefHash, which is not a standard module. So far I have managed to only use standard modules, and just to prove a point I would like to continue (and because my program is more portable if I stick to core modules). What's the best way to deal with it. I could create a hash and then check every key value to see if it has been returned, but that's kludgy and I'm not even sure it works:

{ 1 => $connection_handle_1, 2 => $connection_handle_2, 3 => $connection_handle_3 }

when I would rather have:

{ $connection_handle_1 => 1, $connection_handle_2 => 2, $connection_handle_3 => 3, }

The other option is to create an object for each socket (or rather, have the object create the socket) and then poll each object like $_->do_connection foreach @object. This doesn't sit very well with me because I'm wasting some of the power of IO::Select, but it's the solution I'm running with right now. Could someone set me straight please?

____________________
Jeremy
I didn't believe in evil until I dated it.


In reply to Non blocking multiple socket handles as hash keys by jepri

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.