[19726] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1921 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 12 21:05:31 2001

Date: Fri, 12 Oct 2001 18:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1002935107-v10-i1921@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 12 Oct 2001     Volume: 10 Number: 1921

Today's topics:
    Re: Access a hash key by hash value ? <bootsy52@gmx.net>
        anonymous host <zoetnet@wanadoo.nl>
    Re: anonymous host <jeff@vpservices.com>
    Re: anonymous host <spam@thecouch.homeip.net>
    Re: anonymous host (Logan Shaw)
    Re: chdir() not working ? <Juha.Laiho@iki.fi>
        Code protection <tim_odomNOSPAM@NOSPAMhotmail.com>
    Re: Code protection <ilya@martynov.org>
    Re: creating primary key using dbd::odbc <matthew.garrish@sympatico.ca>
    Re: How do you get paid for Perl work over the Internet (Abigail)
    Re: How do you get paid for Perl work over the Internet <aperlprogrammer@yahoo.com>
    Re: Integration (Abigail)
    Re: Lightweight Languages Workshop, Cambridge MA 11/17/ <mslamm@mscc.huji.ac.il>
    Re: Lightweight Languages Workshop, Cambridge MA 11/17/ <echang@netstorm.net>
    Re: Pattern Matching <krahnj@acm.org>
    Re: Pattern Matching <goldbb2@earthlink.net>
    Re: Perl Power Limits? (Abigail)
        pipe to nawk within perl? (Mike Cambray)
    Re: Signal SEGV with overload (Abigail)
    Re: Stop Transversal of a Directory with Tar and Unzip <whataman@home.com>
    Re: Stop Transversal of a Directory with Tar and Unzip <whataman@home.com>
    Re: UDP server <goldbb2@earthlink.net>
    Re: UDP server (Logan Shaw)
    Re: Working with Bit Vectors (Logan Shaw)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 13 Oct 2001 01:01:12 +0200
From: "Carsten Menke" <bootsy52@gmx.net>
Subject: Re: Access a hash key by hash value ?
Message-Id: <pan.2001.10.13.01.01.09.684.3119@gmx.net>

On Fri, 12 Oct 2001 01:28:58 +0200, Garry Williams wrote:

Thanx that pointer helped a lot :-)
>   perldoc -q "sort.*hash"
> 
But what I have not found and what is not explained there is, how 
the values keep their reference to the keys they belong while they get
ordered. Or said in another way how is the hash reorginized?

			foreach (sort { $result{$a} cmp $result{$b} }
				       keys %result)
			   {
			   print $result{$_},$_,"\n";
			   }	

Would be really interesting how this works internally, for understanding.

A pointer or an explanation would be very nice :-)

Thanx for helping out

Carsten

P.S. I read the FAQ ago but must have overlooked this part


------------------------------

Date: Fri, 12 Oct 2001 22:48:27 GMT
From: "Archie Ballantine" <zoetnet@wanadoo.nl>
Subject: anonymous host
Message-Id: <%qKx7.6$ZK2.109@pollux.casema.net>

hi there

does anyone know how i can connect using the irc module using an anonymous
host?

i wanna listen to a channel where my host address is banned

cheers

any advice- send to 079@pagina.nl





------------------------------

Date: Fri, 12 Oct 2001 15:58:01 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: anonymous host
Message-Id: <3BC77579.3731576D@vpservices.com>

Archie Ballantine wrote:
> 
> i wanna listen to a channel where my host address is banned

And why in the world would anyone want to help you do that?

-- 
Jeff



------------------------------

Date: Fri, 12 Oct 2001 19:44:07 -0400
From: "Mina Naguib" <spam@thecouch.homeip.net>
Subject: Re: anonymous host
Message-Id: <bfLx7.1438$gp.199226@weber.videotron.net>


"Archie Ballantine" <zoetnet@wanadoo.nl> wrote in message
news:%qKx7.6$ZK2.109@pollux.casema.net...
> hi there
>
> does anyone know how i can connect using the irc module using an anonymous
> host?
>
> i wanna listen to a channel where my host address is banned
>
> cheers
>
> any advice- send to 079@pagina.nl
>
>
>



bounce




------------------------------

Date: 12 Oct 2001 19:30:38 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: anonymous host
Message-Id: <9q81ve$mmi$1@charity.cs.utexas.edu>

In article <%qKx7.6$ZK2.109@pollux.casema.net>,
Archie Ballantine <zoetnet@wanadoo.nl> wrote:
>does anyone know how i can connect using the irc module using an anonymous
>host?

If you used an anonymous address, how would the server know who to send
packets back to?

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


------------------------------

Date: 12 Oct 2001 19:44:03 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: chdir() not working ?
Message-Id: <9q7h63$7sf$1@ichaos.ichaos-int>

"John P" <john@trumpetweb.co.uk> said:
>Why doesn't chdir ever seem to work even when -d tells me that the dir
>exists ??
>
>use Cwd; $cwd=cwd;
>my $newdir = $cwd."/newdir";
>
>if (-d $newdir) # if exists
>{chdir($newdir);} # change dir
>
>else # doesnt exist
>{mkdir($newdir); # create it
>chdir($newdir);} # move to new dir

Have you actually done a "print cwd;" here? What do you get?

Remember though, that a perl script is a subprocess, and thus cannot
change anything in the parent process. So, if the above script is
run from the shell (command prompt), the script will change its own
current directory, but after the script exits (back to the prompt),
the current directory for the shell will be what it was before the
script was run. For the shell process the current directory didn't
change at all.
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ UH++++$ UL++++$ P++@ L+++ E(-) W+$@ N++ !K w !O
         !M V PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h--- r+++ y+++
"...cancel my subscription to the resurrection!" (Jim Morrison)


------------------------------

Date: Fri, 12 Oct 2001 22:28:13 GMT
From: "Tim from AZ" <tim_odomNOSPAM@NOSPAMhotmail.com>
Subject: Code protection
Message-Id: <18Kx7.120397$W8.2839964@bgtnsc04-news.ops.worldnet.att.net>

Has anyone here had any sucess in using a tracking system with their perl
programs to keep them from unauthorized resale?  In particular I'm thinking
of keeping a database on my web server  that contains a list of authorized
sites.  If the referrer url doesn't match up with one in the database, it
will simply cause the script not to work.  I realize that this is not
foolproof, but the point is to make it difficult to use without proper
authorization, not impossible.  I would also encrypt the code.  Any
ideas/suggestions/comments about such a system?

Tim from AZ




------------------------------

Date: 13 Oct 2001 02:42:21 +0400
From: Ilya Martynov <ilya@martynov.org>
Subject: Re: Code protection
Message-Id: <87g08ojx9u.fsf@abra.ru>

>>>>> On Fri, 12 Oct 2001 22:28:13 GMT, "Tim from AZ" <tim_odomNOSPAM@NOSPAMhotmail.com> said:

TfA> Has anyone here had any sucess in using a tracking system with
TfA> their perl programs to keep them from unauthorized resale?  In
TfA> particular I'm thinking of keeping a database on my web server
TfA> that contains a list of authorized sites.  If the referrer url
TfA> doesn't match up with one in the database, it will simply cause
TfA> the script not to work.

Referer can be spoofed. Easily, very easily, very very easily. Such
"protection" doesn't protect you at all.

-- 
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
| Ilya Martynov (http://martynov.org/)                                    |
| GnuPG 1024D/323BDEE6 D7F7 561E 4C1D 8A15 8E80  E4AE BE1A 53EB 323B DEE6 |
| AGAVA Software Company (http://www.agava.com/)                          |
 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


------------------------------

Date: Fri, 12 Oct 2001 18:32:46 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: creating primary key using dbd::odbc
Message-Id: <h8Kx7.33778$xy1.3335880@news20.bellglobal.com>

For future reference...

I finally figured out that the problem was that I was not specifying an
index value. Imagine the Microsoft Help topics not being helpful! For anyone
who runs into the same problem and comes across this thread, here's what
finally worked (added to the end of the CREATE TABLE command):

 CONSTRAINT pkbillno PRIMARY KEY (bno)

where pkbillno is just an arbitrary index value I made up to satisfy Access'
need to be a pain-in-the-ass...

Matt


"Matt Garrish" <matthew.garrish@sympatico.ca> wrote in message
news:ZLIx7.33729$xy1.3281448@news20.bellglobal.com...
> Hello,
>
> I'm at my wits end with this one. I'm trying to set the primary key for
the
> new blank MS Access databases that can be added by the script, but every
> command I've tried has come back with an unable to execute on undefined
> value. I know that the table is defined, because it gets created despite
the
> script failing, which also means that the column bno exists as well. I've
> tried everything from specifying the primary key in the create table
command
> to disconnecting and reconnecting to the database to blindly changing the
> statement order around knowing how stupid MS can be about these things.
I've
> also tried adding data to the table before trying to specify the primary
> key, but nothing works. I would therefore be eternally grateful to whoever
> can tell me what's wrong with this statement that's causing the script to
> fail:
>
>
> my $nsk_sth = $ns_dbh->prepare("ALTER TABLE $nstable ADD PRIMARY KEY
> (bno)");
>
> Thanks in advance,
>
> Matt
>
>
>




------------------------------

Date: 12 Oct 2001 22:21:23 GMT
From: abigail@foad.org (Abigail)
Subject: Re: How do you get paid for Perl work over the Internet??
Message-Id: <slrn9ser6e.vs1.abigail@alexandra.xs4all.nl>

Carlos C. Gonzalez (aperlprogrammer@yahoo.com) wrote on MMCMLXIV
September MCMXCIII in <URL:news:MPG.1630d611c8f908329897ab@news.edmonton.telusplanet.net>:
-- Hi everyone,
--
-- I am looking at marketing my services over the Internet to do some very 
-- basic Perl and other type of work but I was wondering how one would get 
-- paid for doing this?  


The same way as you would be doing very basic Python.

Please ask in comp.lang.python for the best way to have dimes and
nickles mailed to you.



Abigail
-- 
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:


------------------------------

Date: Sat, 13 Oct 2001 01:00:16 GMT
From: Carlos C. Gonzalez <aperlprogrammer@yahoo.com>
Subject: Re: How do you get paid for Perl work over the Internet??
Message-Id: <MPG.16313ff14741c3a79897af@news.edmonton.telusplanet.net>

Hi everyone,

Thanks to everyone that has responded by email.  I forgot that I already 
had the email address of aperlprogrammer@yahoo.com in my message header.  
If anyone else wants to respond to me through email either address in my 
previous message is fine.  

From what I have gathered from those who have responded the best way 
seems to be to have a legal, signed contract with penalties for late 
payment.  In addition I should probably have some kind of clause stating 
that I will want to be paid on at most a weekly basis for work done to 
date rather than waiting till the end of the month and risking a bigger 
consequence to me of non-payment.  

PayPal seems to be a good way to get payment from international clients.  

If anyone has any other input I would appreciate your emailing me.  I am 
especially interested in hearing from brand new, beginning programmers 
who might want to handle excess work in exchange for a commission to me.  
Assuming that there is no need to get a job done real quickly which would 
require more experience.   

Thanks

---
Carlos 
www.internetsuccess.ca (not operational yet)


------------------------------

Date: 12 Oct 2001 22:26:51 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Integration
Message-Id: <slrn9sergl.vs1.abigail@alexandra.xs4all.nl>

Ben Ridenhour (bridenho@bio.indiana.edu) wrote on MMCMLXIII September
MCMXCIII in <URL:news:9q4nlp$ac0$1@jetsam.uits.indiana.edu>:
}} Is there a way to make perl do integration (and perhaps other calculus 
}} functions)?  I'm running some simulations in mathematica and they are 
}} horribly slow, so I thought maybe I could do them in perl and speed up the 
}} algorithms...


Mathematica is a large tool designed to do all kinds of mathematical
stuff. Perl, OTOH, is a general purpose language, using internal
datastructures not very suited for large number crunching.

You haven't reached the level yet to tell whether Perl has a certain
impressive feature. Yet you think you can outperform Mathematica 
using Perl? You must be quite a confident person.



Abigail
-- 
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


------------------------------

Date: Sat, 13 Oct 2001 00:25:58 +0200
From: "Ehud Lamm" <mslamm@mscc.huji.ac.il>
Subject: Re: Lightweight Languages Workshop, Cambridge MA 11/17/01
Message-Id: <9q7qru$jk8$1@news.huji.ac.il>

Is there any chance that at least some of the presentation be webcast for
the benfit of those unable to attend?

Ehud Lamm
http://lambda.weblogs.com  <== The Programming Languages weblog




------------------------------

Date: Fri, 12 Oct 2001 23:04:40 GMT
From: "E.Chang" <echang@netstorm.net>
Subject: Re: Lightweight Languages Workshop, Cambridge MA 11/17/01
Message-Id: <Xns9138C2FE9892echangnetstormnet@207.106.93.86>

"Ehud Lamm" <mslamm@mscc.huji.ac.il> wrote in
news:9q7qru$jk8$1@news.huji.ac.il: 

> Is there any chance that at least some of the presentation be
> webcast for the benfit of those unable to attend?
> 

Did you read the announcement?  It said "If you can't make it to sunny 
Cambridge, Massachusetts for the day, you can still watch on our live 
webcast." 

-- 
EBC


------------------------------

Date: Fri, 12 Oct 2001 22:12:32 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Pattern Matching
Message-Id: <3BC76B53.892315B1@acm.org>

Tim wrote:
> 
> Hi,
> 
> Could someone please help on this ?
> 
> my script  does not work  the way I expected because
> it cannot tell the different between the data as shown below:
> 
> Below is data:
> 
> vcc:    2.30     2.50     2.70
> vcc:    3.0
> vcc:    2.4      2.0
> 
> Here is my script:
>                         if ( /vcc:\s*(\d\.\w*)\){
>                                 $vccio1=$1;
>                         }                               }
>                         if ( /vcc:\s*(\d\.\w*)\s*(\d.\w*)/){
>                                 $vccio1=$1;
> 
>                                 $vccio2=$2;
>                                                                                                         }
>                         if ( /vcc:\s*(\d\.\w*)\s*(\d.\w*)\s*(\d.\w*)/){
>                                 $vccio1=$1;
>                                 $vccio2=$2;
>                                 $vccio3=$3
>                         }


( undef, $vccio1, $vccio2, $vccio3 ) = split;

Maybe you should be using an array for vccio?


John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Fri, 12 Oct 2001 18:18:47 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Pattern Matching
Message-Id: <3BC76C47.F7000811@earthlink.net>

Tim wrote:
> 
> Hi,
> 
> Could someone please help on this ?
> 
> my script  does not work  the way I expected because
> it cannot tell the different between the data as shown below:
> 
> Below is data:
> 
> vcc:    2.30     2.50     2.70
> vcc:    3.0
> vcc:    2.4      2.0
[code snipped]

Any time you have variables with names like $foo1 $foo2 $foo3, it means
you should be using an array instead.

my @vccio = m/(\d+\.\d+)/g;

-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


------------------------------

Date: 12 Oct 2001 22:30:52 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Perl Power Limits?
Message-Id: <slrn9sero6.vs1.abigail@alexandra.xs4all.nl>

Sean O'Dwyer (notspam@spamfree.dud) wrote on MMCMLXIV September MCMXCIII
in <URL:news:notspam-F2961E.22140211102001@news.compuserve.com>:
'' Hello,
'' 
'' I'm working on a site at the moment that gets approximately 900k hits 
'' and 32k unique visits per month.
'' 
'' There are some aspects of the site that should be databased and I'm 
'' wondering what the real world outside limit for Perl might be: 1,000 SSI 
'' calls per hour, 10,000?
'' 
'' In other words, is this a job for C++ or can Perl handle it with room to 
'' spare? Does my question even make sense?


No, your question doesn't make any sense.


1,000 SSI could be too much not only for Perl but also for C or C++,
but 10,000 SSI calls could be easily handled.

It all depends. How much work is each call? How efficient is it programmed?
How fast is your CPU, disk, I/O busses, network? How many CPUs do you
have? How many spindels? 

Where is your bottleneck anyway?



Abigail
-- 
perl -wleprint -eqq-@{[ -eqw+ -eJust -eanother -ePerl -eHacker -e+]}-
#    A mosquito near
#    a grass field. Four foxes crouch beside
#    a pond. Or Wunt.


------------------------------

Date: 12 Oct 2001 17:06:03 -0700
From: michael.cambray@wellpoint.com (Mike Cambray)
Subject: pipe to nawk within perl?
Message-Id: <30d612e5.0110121606.17c983e9@posting.google.com>

Could anyone out there tell me why I can't get the pipe to nawk
part of the let2 var below to work (from within a perl script)?
I want let2 to equal '/u1/letters/033308250.o2540'
($ssn = '033308250' and $date2grep = 'Sep 11')

$let1 = `ls -l $ld/$ssn.o* | grep '$date2grep'`;
$let2 = `ls -l $ld/$ssn.o* | grep '$date2grep' | nawk '{print $9}'`;

print "let1 =\n";
print "$let1\n";

print "let2 =\n";
print "$let2\n";

Output:
let1 =
-rw-r--r--   1 bcsadmin bcs  1126 Sep 11 08:57 /u1/letters/033308250.o2540

let2 =
-rw-r--r--   1 bcsadmin bcs  1126 Sep 11 08:57 /u1/letters/033308250.o2540


------------------------------

Date: 12 Oct 2001 22:05:42 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Signal SEGV with overload
Message-Id: <slrn9seq91.vs1.abigail@alexandra.xs4all.nl>

Ari Jolma (ajolma@water.hut.fi) wrote on MMCMLXIII September MCMXCIII in
<URL:news:9q52gl$ao1$1@water.hut.fi>:
;; Why do I get a Signal SEGV with this code:

You shouldn't. That's a bug in Perl.

Please find a short(!) program exhibiting this bug and use the perlbug
command to report it to those who-can-fix-the-bug.



Abigail
-- 
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
 .qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
 .qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'


------------------------------

Date: Fri, 12 Oct 2001 22:48:48 GMT
From: "What A Man !" <whataman@home.com>
Subject: Re: Stop Transversal of a Directory with Tar and Unzip
Message-Id: <3BC77365.1469159D@home.com>

Benjamin Goldberg wrote:
> 
> Randal L. Schwartz wrote:
> >
> > >>>>> "BUCK" == BUCK NAKED1 <dennis100@webtv.net> writes:
> >
> > BUCK> The strange thing is that if I break the above into more
> > BUCK> readable lines (below), the -x doesn't work. Any idea why?
> >
> > BUCK> `unzip -qjnCL $tmpfile
> > BUCK>     -x "*\.pl" "*readme*" "*\.ht" "*\.exe"
> > BUCK>     -d $tmpdir 2>&1`
> >
> > Yes, newlines inside of qx// are given as newlines to the shell,
> > and that's a statement delimiter!

Thanks Randal.

> The correct way to do this from within perl would be:
> unless( fork ) {
>     open(STDERR, "<&STDOUT") or die "Couldn't dup STDOUT: $!";
>     exec( "unzip", "-qjnCL", $tmpfile,
>         qw(-x *.pl *readme* *.ht *.exe),
>         "-d", $tmpdir );
>     die "Couldn't exec unzip: $!";
> }

Thanks, Ben. I was wondering if this thread even belonged in this group,
but now with you perl solution, I feel better about that. What I really
wanted *was* a perl solution, though I didn't express that well. Thanks
for reading my mind :) and for sticking with me on this. 

A couple of further questions, though. How do I keep your script above
from giving me an error message if an excluded pattern is not found, yet
keep the error message if the rest of it fails? ALSO, should I change
*exec* to *system* if there is more script below the *unzip* portion? 

Here is what I'm now testing...

### TEST the ZIP File first 
`unzip -qt $tmpfile`;
if ($? == 0 ) {

### UNZIP it 
unless( fork ) { 
  open(STDERR, "<&STDOUT") or die "Couldn't dup STDOUT: $!";
  exec( "unzip", "-jCLX", $tmpfile, 
      qw(-x *.pl *readme* *.ht *.exe), 
      "-d", $tmpdir ); 
  die "Couldn't exec unzip: $!"; 
 };
}

> Note that . is not special inside of a glob, so it does not have to be
> escaped.

Thanks for clarifying that. I wasn't sure. Through more testing, I also
found that that the match pattern for -x will accept a glob pattern
"read*" or a filename such as "readme".

Unfortunately, I'm stuck using gnu *unzip* and *tar* for awhile until I
find a new webhost. I had forgot about the thread here several months
ago where people gave me a Archive::Tar and Archive::Zip solution, then
I discovered that I couldn't use it, because I needed Compress::Zlib and
I can't compile on it on my present server.

Regards,
Dennis


------------------------------

Date: Fri, 12 Oct 2001 22:55:43 GMT
From: "What A Man !" <whataman@home.com>
Subject: Re: Stop Transversal of a Directory with Tar and Unzip
Message-Id: <3BC77504.4C2C38C2@home.com>

"Randal L. Schwartz" wrote:
> 
> >>>>> "Benjamin" == Benjamin Goldberg <goldbb2@earthlink.net> writes:
> 
> Benjamin> The correct way to do this from within perl would be:
> Benjamin> unless( fork ) {
> Benjamin>     open(STDERR, "<&STDOUT") or die "Couldn't dup STDOUT: $!";
> 
> {ahem} "correct" being used loosely?
> 
>         open STDERR, ">&STDOUT" ...
> 
> :-)
> 

Change noted. Thanks.

--Dennis


------------------------------

Date: Fri, 12 Oct 2001 18:34:21 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: UDP server
Message-Id: <3BC76FED.6245FA10@earthlink.net>

Test Account wrote:
> 
> Hi, I'm trying to write a UDP server; what I've come up so far is
> (please don't laugh):
> 
> #!/usr/bin/perl -w
> 
> use strict;
> use IO::Socket;
> 
> $| = 1;
> 
> my $msgin;
> my $listenport = 9999;
> my $maxlen = 5000;
> my $port = shift || $listenport;
> $SIG{'INT'} = sub {exit 0};
> my $sock = IO::Socket::INET->new(Proto=>'udp',LocalPort=>$port) or die
> "Cannot open socket!";
> 
> while (1) {
>     next unless $sock->recv($msgin,$maxlen);
>     do_stuff_with($msgin);
>     }
> }
> 
> This is rather simple-minded, and it only accepts one message at a
> time, and it can't listen for more messages while it's doing something
> with $msgin.  How can I modify this to be "multi-threaded"?

Fork for each message recieved.

while( $sock -> recv ( $msgin, $maxlen ) ) {
   next if fork;
   do_stuff_with( $msgin );
}


-- 
    "Just how stupid are you Kuno?"
    "Verily, Tatewaki Kuno knows no limits."


------------------------------

Date: 12 Oct 2001 19:26:52 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: UDP server
Message-Id: <9q81oc$mli$1@charity.cs.utexas.edu>

In article <3BC76FED.6245FA10@earthlink.net>,
Benjamin Goldberg  <goldbb2@earthlink.net> wrote:
>Test Account wrote:
>> 
>> while (1) {
>>     next unless $sock->recv($msgin,$maxlen);
>>     do_stuff_with($msgin);
>>     }
>> }
>> 
>> This is rather simple-minded, and it only accepts one message at a
>> time, and it can't listen for more messages while it's doing something
>> with $msgin.  How can I modify this to be "multi-threaded"?
>
>Fork for each message recieved.
>
>while( $sock -> recv ( $msgin, $maxlen ) ) {
>   next if fork;
>   do_stuff_with( $msgin );
>}

That certainly *is* an approach, but since datagrams
are almost never more than 1.5k, do you *really* thing
it's good fork a whole new process for each one?[1]

The operating system should queue the datagrams (up to a certain
point -- there is no flow control among other things), so
processing them in a single-threaded fashion will probably be
faster than doing the fork because of the hugely smaller overhead.

That's not necessarily always true.  If do_stuff_with()
contains the code "sleep(10);", then fork will be
better.  Well, except for the fact that you've just set
yourself up for a giant denial of service attack...

But I think overall if do_stuff_with() takes only a
very short period of time to execute, then leaving it
as a serial process is going to be more efficient with
CPU resources and will make for a faster server too.

An alternative is to fork a fixed number of threads in advance and
have each one read datagrams from the socket.  This is great unless
the datagrams have something to do with each other, in which case
you'd have to communicate between processes, which would be a pain.

  - Logan

[1]  I admit, the original poster didn't say they wanted a solution
     that was good.  They just said they wanted it to be
     multi-threaded.
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


------------------------------

Date: 12 Oct 2001 19:05:18 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Working with Bit Vectors
Message-Id: <9q80fu$m5e$1@charity.cs.utexas.edu>

In article <3BC7244E.7D4AE5FE@chem.uga.edu>,
Joel Caughran  <caughran@chem.uga.edu> wrote:
>I'm working on a short script that represents people's schedules and
>job's times as bit vectors.  It works but it seems to me that there must
>be a better way to see if there is a conflict between a person's
>schedule and a job's time.

Bit vectors are one way to do it, and it is pretty straightforward to
do it that way, but there's sort of a fundamental tradeoff that you
can't (easily) avoid with them:  either you have low resolution (say
hour-long increments), or you have long bit vectors and processing of
them is slow because they explicitly represent big chunks of
uninteresting stuff.

Personally, if I were working with schedules, I'd store them as sets of
pairs of start and end times.  That way, you have very good precision
(whatever your numeric types allow) and yet you can easily test if a
new event N conflicts with a person's schedule.  How?  You take a list
of events in the person's schedule and remove all those that don't end
before N starts.  Then you remove all those that start before N ends.
The ones you're left with are the conflicts.

In Perl, that might look like this:

    sub event_conflicts
    {
	my ($event, $schedule) = @_;

	return
	    grep (($_->[1] > $event[0] and $_->[0] < $event[1]), @$schedule);
    }

    @schedule = ([11,12.5], [13,14], [16,17], [17,18.5]);
    @event = (12.25, 13.25);

    @conflicts = event_conflicts (\@event, \@schedule);

    if (@conflicts)
    {
	print "$event[0] to $event[1] conflicts with:\n",
	    map ("    $_->[0] to $_->[1]\n", @conflicts);
    }
    else
    {
	print "No conflicts.\n";
    }

Actually, you could get even fancier and reduce the running time so
that it's logarithmic and doesn't have to compare the new event to
every single existing event, but that's more work.

>#	Method 1	###################################
>if (unpack("b*", $bitmap1 & $bitmap2) =~ /^0+$/) {
>	print "No Conflict\n";
>} else {
>	print "Conflict\n";
>}

You could make that a little faster by searching whether a ones exists
instead of verifying that it's all zeros:

    $does_conflict = (unpack("b*", $bitmap1 & $bitmap2) =~ /1/);

You could also check hex digits (resulting in a shorter string) for
non-zero:

    $does_conflict = (unpack("b*", $bitmap1 & $bitmap2) =~ /[^0]/);

Or whole bytes:

    $does_conflict = (unpack("C*", $bitmap1 & $bitmap2) =~ /[^\0]/);

>#	Method 2	###################################
>$bitmap3 = $bitmap1 & $bitmap2;
>my $conflict = 0;
>for (my $i=0; $i<=length($bitmap3)*8; $i++) {
>	if (vec($bitmap3, $i, 1)) {
>     	$conflict = 1;
>     }

The most obvious improvement to this (and to lots of such things) would
be to make it quit the loop as soon as it sees there's a conflict.
There's really no need to keep examining past that point.

So, I'd write this:

    sub conflict
    {
	my $bitmap = $_[0] & $_[1];

	for (my $i = 0; $ < length ($bitmap) * 8; $i++)
	{
	    return 1 if vec($bitmap3, $i, 1);
	}

	return 0;
    }

Hope that helps.

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1921
***************************************


home help back first fref pref prev next nref lref last post