Of your 3 options, I would throw out option 2 as it places to much responsibility on your calling script IMO. As for the other options, its a matter of taste.

Option 1 is similar to the select-style of doing IO. The idea of select is that you have a tight loop in a single process, on each round of the loop you call select and get back information on which file handles are read for reading, which are ready for writing and which have errors and then do what is appropriate. It is basically the best way to do asyncronous non-blocking IO without spawning multiple processes. This technique tends to be more management intensive, but it's less resource intensive than threads or forking processes (aka - your 3rd option).

In order to do this in the most OO way, my suggestion would be to make run a class method, which references a package level array of all your connection instances. Or if that seems un-OO to you, you can make a factory class for your connections, which will retain a reference to all connections it dispenses and then have your factory "run" everything. With OO, as with perl, TIMTOWTDI.

Your 3rd option is the more common way to do about these things (at least in my experience). Apache works this way (yes I know I am greatly simplifying Apache), as do most web servers. The benefits are that you can code your connection objects in such a way that you need not worry about resource (net-connection) sharing and asyncronous behaviour. The forked-process/thread takes care of that for you. But, as I mentioned above, its much more resource intensive in terms of memory and CPU.

The good thing about this technique is that your connection objects can become simplier since they have no need to worry about hogging resources. You will still need to write some code to fork a process or spawn a thread for each one, but once that is done, the processes/threads take care of a lot of the "management" stuff that you would do by hand with option 1.

If you do go for option 3 (which I would recommend), you should look for some thread/fork managers on CPAN. Thread::Pool of Parallel::ForkManager come to mind.

-stvn

In reply to Re: OO theory question by stvn
in thread OO theory question by Forsaken

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.