How to use select (select(2)) in Perl?

K

Kenny McCormack

How do you do the equivalent of the following (abbreviated) C in Perl:

while (1) {
select(maxfd,&iofds,0,0,0);
if (FD_ISSET(fd1,&iofds))
... copy data from fd1 to fd2;
if (FD_ISSET(fd2,&iofds))
... copy data from fd2 to fd1;
}

(with, of course, all the appropriate error checking and so on...)

I'm comfortable with this idiom in C, but all I have to go on for Perl is
"man perlfunc" and the "Programming Perl" book. Both contain about the
same information, which is pretty good about how to build up the bits and
call the function, but contain nothing about how to unpack the result.

Can anyone help me with this?
 
B

Ben Morrow

How do you do the equivalent of the following (abbreviated) C in Perl:

while (1) {
select(maxfd,&iofds,0,0,0);
if (FD_ISSET(fd1,&iofds))
... copy data from fd1 to fd2;
if (FD_ISSET(fd2,&iofds))
... copy data from fd2 to fd1;
}

(with, of course, all the appropriate error checking and so on...)

I'm comfortable with this idiom in C, but all I have to go on for Perl is
"man perlfunc" and the "Programming Perl" book. Both contain about the
same information, which is pretty good about how to build up the bits and
call the function, but contain nothing about how to unpack the result.

Can anyone help me with this?

You want to use the IO::Select module. Read 'perldoc IO::Select'.

Ben
 
K

Kenny McCormack

You want to use the IO::Select module. Read 'perldoc IO::Select'.

Thanks, but no thanks. I don't use modules.

I just need to know the equivalent of FD_ISSET().
 
U

Uri Guttman

KM> Thanks, but no thanks. I don't use modules.

and why not? are you such a perl guru that you can replace cpan with 3
lines of code? do you use c libraries?

KM> I just need to know the equivalent of FD_ISSET().

rtfm. hmm, perl might even have a similar function. but you will have to
write a c program to extract, format and print out the docs since that
might use a perl module.

uri
 
K

Kenny McCormack

That would be the vec() function, as mentioned in perldoc -f select

There are even examples.

As I said, the documentation for select() shows how to use vec() as an
lvalue to build up the bits objects for the call, but they don't show how
to parse what comes back. It seems to assume you already know how to do it
from C.

I'm guessing that you use vec() as an rvalue to test the bits, after the
call. Is that correct?

(And, please, no snotty comments about using modules and other silliness)
 
U

Uri Guttman

KM> I'm guessing that you use vec() as an rvalue to test the bits, after the
KM> call. Is that correct?

KM> (And, please, no snotty comments about using modules and other
KM> silliness)

use a module. you haven't explained your perfectly good reason to not
use one. does your homework disallow them? is your religion against
them? do your free perl web host not allow them? are you a wheel
reinventor?

and have you even rtfm'ed the docs on vec which select referred to?

so no more snotty questions from you as you don't answer any questions
asked of you.

i pick out my noise at you!

snottingly yours,

uri
 
S

Sam Holden

As I said, the documentation for select() shows how to use vec() as an
lvalue to build up the bits objects for the call, but they don't show how
to parse what comes back. It seems to assume you already know how to do it
from C.

I'm guessing that you use vec() as an rvalue to test the bits, after the
call. Is that correct?

Why not RTFM and see.
 
K

Kenny McCormack

Why not RTFM and see.

As I said, the documentation for select() shows how to use vec() as an
lvalue to build up the bits objects for the call, but they don't show how
to parse what comes back. It seems to assume you already know how to do it
from C.
 
S

Sherm Pendley

Thanks, but no thanks. I don't use modules.

Um... why not?

Honestly - I don't intend to criticize. I'm simply having trouble with the
concept. To me, Perl as a language is not all that remarkable; most of its
value comes from its extensive library of modules.

Do you mean "won't" or "can't?" That is, do you avoid them for your own
reasons, or is there some technical restriction that prevents you from
using them? If it's the latter, someone here could probably help you find
a way around it.

For example, if you're on a shared server, you can install a module into
your user space without needing root access. If you need to use an XS
module and you're working on a severely limited platform that lacks a
dynamic linker, you can build a Perl binary that statically links the
module.

IO::Select is a "core" module. That is, it's included with Perl, it's not
an add-on. Does that make a difference?

sherm--
 
J

Jürgen Exner

Kenny said:
Thanks, but no thanks. I don't use modules.

Hmmm, why not?
Not using modules is like typing on a keyboard with at least one hand tied
behind your back.

You certainly have the right to do so, but I wonder why you want to make
your live difficult. Most people would prefer to make their lives easier.

jue
 
K

Kenny McCormack

Um... why not?

Honestly - I don't intend to criticize. I'm simply having trouble with the
concept. To me, Perl as a language is not all that remarkable; most of its
value comes from its extensive library of modules.

Do you mean "won't" or "can't?" That is, do you avoid them for your own
reasons, or is there some technical restriction that prevents you from
using them? If it's the latter, someone here could probably help you find
a way around it.

The former. The short answer is: I have my reasons and I should not have
to answer this question. The slightly longer answer is: I'm not a Perl
fan; I'm not into Perl for its own sake. For me, Perl fills a very narrow
slice in my toolkit, between AWK and C. To say much more at this point
would bring us into advocacy. If you want more, email me (yes, it's valid).

Suffice it to say that I did "perldoc IO::Select" and it looked interesting,
but I didn't recognize any of the syntax (I learned Perl long ago), and as
I've indicated, it is not worth my time to learn it at this point in time.
 
J

Jay Tilton

(e-mail address removed) (Kenny McCormack) wrote:

: In article <[email protected]>,
: >On Sat, 17 Jan 2004 03:37:42 +0000, Kenny McCormack wrote:
: >
: >> Thanks, but no thanks. I don't use modules.
: >
: >Um... why not?

[snip]

: Suffice it to say that I did "perldoc IO::Select" and it looked interesting,
: but I didn't recognize any of the syntax (I learned Perl long ago),

It is ordinary Perl object-oriented syntax, and is easily grasped if you
already understand the concepts and normal Perl syntax.

: it is not worth my time to learn it at this point in time.

It's an odd reason. Perl programmers are pathologically frugal on
matters of spending time writing code. If they recommend a module, you
can be certain that using it will solve a problem with less effort than
hand-rolling a solution.
 
S

Sherm Pendley

The former. The short answer is: I have my reasons and I should not have
to answer this question.

I didn't ask for the sake of advocacy - I simply wanted to get at the core
of the problem, to see if I could help or not.

Unfortunately, I'm afraid I can't help. If you wanted to learn the OO
notation used in IO::Select, I could help with that, or with the other
things I mentioned. Having looked at 'perldoc select', I find myself
somewhat mystified by it.

sherm--
 
K

Kenny McCormack

I didn't ask for the sake of advocacy - I simply wanted to get at the core
of the problem, to see if I could help or not.

Unfortunately, I'm afraid I can't help. If you wanted to learn the OO
notation used in IO::Select, I could help with that, or with the other
things I mentioned. Having looked at 'perldoc select', I find myself
somewhat mystified by it.

OK - I thank you for your honesty.

I think the documentation of select() is cryptic even by Perl standards.

The only thing I can say, is that 5 years ago, I did tease this all out, to
write a TCPIP server that listened on multiple ports (using select to
effect the "accept w/o blocking" paradigm). But I can't remember all the
details of how I did it.
 
B

Ben Morrow

I think the documentation of select() is cryptic even by Perl standards.

Yes, it is rather. It assumes that you know how to use select in C,
and also that you know how to use vec in Perl.

This is doubtless one of the reasons for the IO::Select module.

In answer to your original question: use vec. perldoc -f vec.

Ben
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The former. The short answer is: I have my reasons and I should not have
to answer this question. The slightly longer answer is: I'm not a Perl
fan; I'm not into Perl for its own sake. For me, Perl fills a very narrow
slice in my toolkit, between AWK and C. To say much more at this point
would bring us into advocacy. If you want more, email me (yes, it's valid).

If that's the case, why are you using Perl for this particular task in
the first place? It sure sounds like you might be better off writing
your code in C, and you'd be happier about it, too.

Modules (especially widely-used ones like IO::Select) are an essential
part of Perl programming. If you refuse to use them, you should not
expect a huge amount of help from the Perl community, who recognize
their essentiality (it's a word now, dammit!).

I bet the answer to your question is in the Perl source code. Happily,
it's written mostly in C, so you should be able to find your answer
quickly enough.

- --keith

- --
(e-mail address removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFACXe9hVcNCxZ5ID8RAq31AJ9z+Vf73SN8B4B3NFT8j5fcA65PlgCgl1M+
yYFko8BmXa0a+JwSFtD+hdg=
=BChM
-----END PGP SIGNATURE-----
 
K

Kenny McCormack

The former. The short answer is: I have my reasons and I should not
have to answer this question. The slightly longer answer is: I'm not
a Perl fan; I'm not into Perl for its own sake. For me, Perl fills a
very narrow slice in my toolkit, between AWK and C. To say much more
at this point would bring us into advocacy. If you want more, email
me (yes, it's valid).

If that's the case, why are you using Perl for this particular task in
the first place? It sure sounds like you might be better off writing
your code in C, and you'd be happier about it, too.[/QUOTE]

(snip)

Why bother to post nonsense like this?
Does it make you feel better?

Note: You might ask the same of me - but at least I have a problem I am
trying to solve - I have an excuse.

Seriously: can we move this nonsense to an advocacy group or email?
 
R

Rocco Caputo

As I said, the documentation for select() shows how to use vec() as an
lvalue to build up the bits objects for the call, but they don't show how
to parse what comes back. It seems to assume you already know how to do it
from C.

I'm guessing that you use vec() as an rvalue to test the bits, after the
call. Is that correct?

That is correct. You can read perldoc -f vec for more details.
(And, please, no snotty comments about using modules and other silliness)

By the way, you really should be using POE instead of this foolish
insistence on socket(), select() and vec()!

(You may as well be insisting on writing C without stdlib. "No, please
don't tell me it's dumb!")
 
R

Randal L. Schwartz

Kenny> Why bother to post nonsense like this?
Kenny> Does it make you feel better?

Kenny> Note: You might ask the same of me - but at least I have a problem I am
Kenny> trying to solve - I have an excuse.

Perhaps you forget what nobull often says about this group:

Get real! This is a discussion group, not a helpdesk. You post
something -- we discuss its implications. If the discussion happens
to answer a question you've asked, that's incidental. If you post a
question that implies that you've got a problem finding answers to
trivial questions in the manual, then it is perfectly reasonable for
us to discuss how to do that.

Seriously, coming in to a newsgroup that wants to help, but asking
for help in a weird way (and frankly, saying "no modules" is a creepy
weird way), and then being confused by the result, is a bit looney,
if you ask me. {grin}

print "Just another Perl hacker,"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top