On 30 Apr 2007 17:37:44 -0700,
Monty <> wrote:
> Hope this is the right forum for this...
>
> I'm trying to learn sockets, and I'm studying some examples given
> vrious places on the web. So far I'm with the explanations, but one
> example leaves me perplexed and I'm hoping some one can shed light on
> it. I'm hoping to write a chat-like client-server program, and I
> found this explanatory code:
>
> ... create socket as before ...
> 11 use IO::Select;
> 12 $read_set = new IO::Select(); # create handle set for reading
[snip]
> 17 my ($rh_set) = IO::Select->select($read_set, undef, undef, 0);
> 18 # take all readable handles in turn
> 19 foreach $rh (@$rh_set) {
[snip]
> Regardless of your own opinion of this code, I'm wondering about the
> $read_set and $rh_set variables defined here. Are they, in the perl-
> ative or figurative sense, lists or arrays containing socket or file
> handles? I don't quite follow the perldoc on IO::Select to figure
> this out.
$read_set is an IO::Select object (see code line 12) and $rh_set is a
reference to an array of file handles that are ready to read. You use
$read_set by using the documented interface of IO::Select, and you use
$read_set by dereferencing it, and doing something with each file
handle.
A list, in Perl, is not something you can store in a variable. It's a
syntactic construct, or a runtime construct, but not a thing you can
store or manipulate. Also see the question 'What is the difference
between a list and an array?' in the Perl FAQ, section 4.
You say 'regardless of your own opinion of this code', but do you really
believe you can post code and not get comments?
The code you submitted is a bit of an odd circumvention of the neat OO
interface to select that IO::Select gives you. A better, and more
elegant example which does almost exactly the same as your code, can be
found at the end of the IO::Select man page. I'd use that instead as
your model of how to work with IO::Select, and I'd forget this one.
> Also, can sockets generated this way (using IO::Socket) be
> bidirectional?
I don't see why not.
Martien
--
|
Martien Verbruggen | I'm just very selective about what I accept
| as reality - Calvin
|