Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Nonblocking IO Module

by perlknight (Pilgrim)
on Jul 16, 2002 at 19:21 UTC ( [id://182207]=perlquestion: print w/replies, xml ) Need Help??

perlknight has asked for the wisdom of the Perl Monks concerning the following question:

I need help with an example in "Network Programming With Perl" by Lincoln Stein. Here's a snipet of the code for the IO::SessionSet Module:
package IO::SessionSet use strict; use Carp; use IO::Handle; use IO::SessionData; use vars '$DEGUG'; $DEBUG = 0; sub new { my $pack = shift; my $listen = shift; sub new { my $self = bless { sessions => {}, readers => IO::Select->new(), writers => IO::Select->new(), }, $pack; if (defined($listen) and $listen->can('accept') { $self->{listen_socket} = $listen; $self->{readers}->add($listen); } return $self; }
I can't find anything in this example which has $listen->can('accept'), where is he pulling this method from? Thanks.

Replies are listed 'Best First'.
Re: Nonblocking IO Module
by theorbtwo (Prior) on Jul 16, 2002 at 19:36 UTC

    can is a method of UNIVERSAL, which means it can be called on any object. (All objects implicitly inherit from UNIVERSAL, even if they don't have any @ISA whatsoever.) It checks to see if there is an accept() method that could be called on $listen.


    Confession: It does an Immortal Body good.

Re: Nonblocking IO Module
by Rex(Wrecks) (Curate) on Jul 16, 2002 at 19:46 UTC
    $listen is an option, an arg you can pass the new() method. It is supposed to be a listen socket.

    From the book description of that API: "$set = IO::SessionSet->new([$listen])
    Creates a new IO::SessionSet. If a listen socket is provided in $listen, then the module automatically accepts incoming connections"

    Basically
    $set = IO::SessionSet->new() ;
    is the same as doing
    $set = IO::SessionSet->new() ; $set->add($listen);
    Does that make sense to you? Good, then what he is doing is calling the UNIVERSAL (as described above) to make sure that, if $listen exists, its a listening socket (although it can be other things too, see code on page 393 of the book) and if it is he calls his ->add method for you.

    Update: Sorry, I misread the question and just got to it in a more roundabout way :)

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://182207]
Approved by Rex(Wrecks)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found