[10352] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3945 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Oct 11 04:07:17 1998

Date: Sun, 11 Oct 98 01:00:19 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 11 Oct 1998     Volume: 8 Number: 3945

Today's topics:
        Assigning a value to a var <mpriatel@chat.carleton.ca>
    Re: Assigning a value to a var <mpriatel@chat.carleton.ca>
    Re: Help me in getting source code of counter in perl! (Joergen W. Lang)
        Help to compile or translate my software perl in C for  <horizons@hol.fr>
    Re: Help to compile or translate my software perl in C  <gadget@bc.sympatico.ca>
    Re: Help to compile or translate my software perl in C  <jbattikha@highsynth.com>
    Re: Help with this code (Mark-Jason Dominus)
    Re: How do I use die? (I R A Aggie)
    Re: How do I use die? <brettr@centuryinter.net>
        howto : tee - replicate the standard output naderr@my-dejanews.com
    Re: NEEDED: A good perl programer to... bhoylma@uswest.com
    Re: NETWORK & GRAPHIC CARD INFO (Nathan V. Patwardhan)
    Re: NETWORK & GRAPHIC CARD INFO <m.v.wilson@erols.com>
        ODBC access to Micro$oft SQL server <harlock1@home.com>
    Re: Question about locking files <minich@globalnet.co.uk>
    Re: Question about locking files (Mark-Jason Dominus)
    Re: sort with dynamic sort expression help (Mark-Jason Dominus)
    Re: sort with dynamic sort expression help <rick.delaney@shaw.wave.ca>
    Re: sort with dynamic sort expression help (Mark-Jason Dominus)
    Re: sort with dynamic sort expression help (Larry Rosler)
        To Mass Question Mailer... <bill@fccj.org>
        Whole Random number? (Ken Williams)
    Re: Whole Random number? (Ken Williams)
    Re: Why it doesn't work under UNIX?????? (I R A Aggie)
    Re: Writing an intelligent forking server. (Mark-Jason Dominus)
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: Sun, 11 Oct 1998 00:54:13 -0400
From: Mark Priatel <mpriatel@chat.carleton.ca>
Subject: Assigning a value to a var
Message-Id: <362039F5.6CF5D41D@chat.carleton.ca>

I actually have a fair amount of experience with Perl, but all the
scripts I've created have taken data from forms.  However, if your
running a script from a DOS environment  how can users assign data to
variables?





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

Date: Sun, 11 Oct 1998 01:09:41 -0400
From: Mark Priatel <mpriatel@chat.carleton.ca>
Subject: Re: Assigning a value to a var
Message-Id: <36203D95.D6D1EE33@chat.carleton.ca>

> I actually have a fair amount of experience with Perl, but all the
> scripts I've created have taken data from forms.  However, if your
> running a script from a DOS environment  how can users assign data to
> variables?

Ok, a minute after this post I took a quick look through the perl
manual...Is this a safe/common way to assign data?

$name = <stdin>;




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

Date: Sun, 11 Oct 1998 02:30:28 +0100
From: jwl@_munged_worldmusic.de (Joergen W. Lang)
Subject: Re: Help me in getting source code of counter in perl!
Message-Id: <1dgpqwf.1v4ui21xx860wN@host057-210.seicom.net>

Mark N. Salvador <markns@nsclub.net> wrote:

> Pls. help me in finding source code of perl script counter!
> I badly need it... thanks!
> 
> Mark

http://www.altavista.com/cgi-bin/query?pg=q&kl=XX&q=perl+script+counter+
source

yr. wlcm.
hth,

Joergen
-- 
  To reply by email please remove _munged_ from address Thanks !
-------------------------------------------------------------------
   "Everything is possible - even sometimes the impossible"
             HOELDERLIN EXPRESS - "Touch the void"


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

Date: Sun, 11 Oct 1998 01:19:36 +0200
From: Fred & Chris <horizons@hol.fr>
Subject: Help to compile or translate my software perl in C for win95
Message-Id: <361FEB88.9E936E89@hol.fr>

This is a multi-part message in MIME format.
--------------49459D97FF64CAF5A9F4045C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello,

    I know very well Perl and I have wrote a software but this software
must run on system windows 95 without perl.

    Anybody can translate it for me this software in C (or delphi) and I

compil it directly in .exe. or anybody can compil it for me?

    The software is attached.

    Thank you for your help

    It is really urgent...

    Ps: Can you send me by e-mail  fred@freudian.com the new program?

--------------49459D97FF64CAF5A9F4045C
Content-Type: application/x-perl; name="anpe.pl"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline; filename="anpe.pl"

#!/usr/bin/perl 
print "Traduction du fichier Partenet\n";
print "Fred. FAUCOUNEAU Criateur <fred.freudian.com>\n";
print "Entrez le nom du fichier ` traiter \n";
$entree =<STDIN>; chomp($entree);

open (FILE,$entree) or die ("Impossible de trouver le fichier : $!");
open (FLUTE,'>/home/fred/traite.html') or die ("Oups,probleme :$!");

while (<FILE>) {
   chomp;
   $offre=$1 if /(Offre.*?X)/;
   $date=$1 if /(Date.*?98)/;
   $Ale=$1 if  /(Ale.*?TD)/;
   $duree=$1 if /(Recherche.*?FONT>)/;
   $descriptif= $1 if /(R><FO.*?FONT>)/;
   $lieu=$1 if /(Lieu.*?FONT>)/;
   if (/(B>H.*?FONT)/) {            # On est a la fin du record"
      $horaires =$1;

print FLUTE "<HTML>";
print FLUTE "<BODY>" ;
print FLUTE "&nbsp;";
print FLUTE "<TABLE BORDER COLS=1>";
print FLUTE "<TR>";
print FLUTE "<TD><FONT SIZE=1><PRE><B>$offre					<B>$date				<B>$Ale</PRE></FONT>";
print FLUTE "<BR><FONT SIZE=1><B>$duree&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp";
print FLUTE "<CENTE$descriptif</CENTER>";
print FLUTE "<FONT SIZE=1><B><PRE>$lieu                                 			      <$horaires></PRE></TD>";
print FLUTE "</TR>";
print FLUTE "</TABLE>";
print FLUTE "</BODY>";
print FLUTE "</HTML>";

   }
}

--------------49459D97FF64CAF5A9F4045C--



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

Date: Sat, 10 Oct 1998 22:59:51 -0700
From: Fred Richards <gadget@bc.sympatico.ca>
To: Fred & Chris <horizons@hol.fr>
Subject: Re: Help to compile or translate my software perl in C for win95
Message-Id: <36204957.22B7D03F@bc.sympatico.ca>

Fred & Chris wrote:

> Hello,
>
>     I know very well Perl and I have wrote a software but this software
> must run on system windows 95 without perl.
>
>     Anybody can translate it for me this software in C (or delphi) and I
>
> compil it directly in .exe. or anybody can compil it for me?
>
>     The software is attached.
>
>     Thank you for your help
>
>     It is really urgent...
>
>     Ps: Can you send me by e-mail  fred@freudian.com the new program?

Check out this site. Perl2exe is a Perl to .EXE compiler.

http://www.demobuilder.com/



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

Date: Sun, 11 Oct 1998 02:11:41 -0400
From: Jihad Battikha <jbattikha@highsynth.com>
Subject: Re: Help to compile or translate my software perl in C for win95
Message-Id: <36204C1D.C4E967D4@highsynth.com>

Fred & Chris wrote:

>     I know very well Perl and I have wrote a software
> but this software must run on system windows 95 without
> perl.

If all you need is a binary executeable (without access to
pre-compiled C source), go to http://www.demobuilder.com/ and get
Perl2Exe.

~jihad

-- 
<>>>===========================================================<<<>
 Jihad Battikha <jbattikha@highsynth.com> http://www.highsynth.com
 -=< graphics . 2d/3d animation . illustration . web authoring >=-
 -----------------------------------------------------------------
 PGP: http://www.highsynth.com/jihad/pgp.htm
 - fingerprint: 9B5E 0484 0D88 6454 380B  964D E9B7 7A58 15C0 CAD7
 - expires: 1/1/1999
<>>>===========================================================<<<>
 Before sending me commercial e-mail, the sender must first agree
 to my LEGAL NOTICE located at: http://www.highsynth.com/sig.html.
<>>>===========================================================<<<>


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

Date: 10 Oct 1998 23:55:48 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Help with this code
Message-Id: <6vpa84$558$1@monet.op.net>

In article <6vorff$l25$1@nnrp1.dejanews.com>,  <stevenjm@olywa.net> wrote:
>>  if(/^#/) { next; }                     # Discard comments.
>
>If you are discarding the comments, shouldn't your if/next statement above be
>before the split? Seems a waste to attempt a split and then throw away the
>results (if any).

Yeah.

I've always liked this strategy better:

	while (<>) {
	  s/#.*//;		# Scrub comment
	  next unless /\S/;	# Ignore blank lines

	  ...
	}

This allows comments to occur on the same line as data, and also skips
blank lines.  It also seems totally clear and straightforward: Ignore
comments as if they were blank space, and skip totally blank lines.
I first wrote this in C in about 1990 or 1991, and I've used it ever
since.  It's so simple that it's even simple in C:

	unsigned blank;
	char *pound, *p;

	/* delete comments */
	if (pound = strchr(line, '#')) *pound = '\0';

	/* Is the line blank? */
	for (p=line; *p; p++) {
	  if (!isascii(*p) || !isspace(*p)) {
	    blank=0; break;
	  }
	}
	/* Skip blank lines */
	if (blank) continue;

Of course, it's not always appropriate.  But I try to design data
formats to that it is appropriate, because it's so nice.




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

Date: Sun, 11 Oct 1998 01:17:27 -0500
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: How do I use die?
Message-Id: <fl_aggie-1110980117280001@aggie.coaps.fsu.edu>

In article <6vo153$s9m$1@newsread1-mx.centuryinter.net>, "brettr"
<brettr@centuryinter.net> wrote:

+ I'm using the command line perl -w test.cgi to execute this program now. I
+ have also included the $! so I will capture the error string. Here's first
+ part of the script I'm running using die():
+ #!/usr/bin/perl
+ chdir('/user/printer') or die("$!");

+ The script will run fine if I do not include or die("$!"). What did I do
+ wrong here?

Have you manually done a "cd /user/printer"???

In the unix systems I've encountered, it is usually /usr/ not /user/.

James - even if they're pronounced the same...


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

Date: Sun, 11 Oct 1998 01:50:45 -0500
From: "brettr" <brettr@centuryinter.net>
Subject: Re: How do I use die?
Message-Id: <6vpkj3$h8o$1@newsread1-mx.centuryinter.net>

What versions of Perl support die()?

Jeff Zucker wrote in message <361EF9A2.F605196E@vpservices.com>...
>brettr wrote:
>> #!/usr/bin/perl
>> chdir('/user/printer')or die;
>> [snip]
>> I get Internal Server Error.
>
>Mistake #1: you left off -w on the first line.  Always use perl's
>warnings to find out what is going wrong.
>
>Mistake #2: you left off $! on the second line so you won't know what
>the error message is when it dies.
>
>Mistake #3: (and this is the big one) there's no way you can find out
>what went wrong by accessing that script from your browser - you have to
>run it on the command line to see what is going on.  Put in the -w and
>the $! and run it from the command line and you will find out why the
>script isn't working.  That is what die is for - to exit the program
>when something goes wrong so you can find out what went wrong.  You can
>use the CGI::CARP module to see a lot of specific error messages in your
>browser but even then it's best to test your scripts on the command line
>first so you can see where and often why the die exited the script.
>
>My guess BTW is that the script died because the directory you tried to
>chdir to didn't exist - either a typo or wrong path.
>
>HTH,
>
>- Jeff
>





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

Date: Sun, 11 Oct 1998 07:17:21 GMT
From: naderr@my-dejanews.com
Subject: howto : tee - replicate the standard output
Message-Id: <6vpm21$omp$1@nnrp1.dejanews.com>

G'day,

What's the equivalent way to do this in perl?

	echo "foo" | tee -a foo.log

TIA,

	Rob -

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Sun, 11 Oct 1998 05:18:27 GMT
From: bhoylma@uswest.com
Subject: Re: NEEDED: A good perl programer to...
Message-Id: <6vpf33$ggm$1@nnrp1.dejanews.com>

In article <361E2C1D.81E9B4CD@hhs.se>,
  Sune Karlsson <Sune.Karlsson@hhs.se> wrote:
> droby@copyright.com wrote:
> >
> > Yet another reason to prefer Linux to NT. ;-)
>
> Yes, but not for the reason you think. PIDs are not assigned
> sequentially on NT. Don't know the algorithm, but they never get large.
>

That's because one is rebooting the #$@%*@!% NT box so often! :^)

Peace.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Sun, 11 Oct 1998 04:01:50 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: NETWORK & GRAPHIC CARD INFO
Message-Id: <O4WT1.23$1K4.23195@news.shore.net>

SattarS (sattars@aol.com) wrote:


: Can anyone tell me how to get Network card and Graphic Card details
: from Perl? Thanks

Thanks for the two-line message preceded by the 80-line header.  Die.

*plonk*


--
Nate Patwardhan|root@localhost
"Fortunately, I prefer to believe that we're all really just trapped in a
P.K. Dick book laced with Lovecraft, and this awful Terror Out of Cambridge
shall by the light of day evaporate, leaving nothing but good intentions in
its stead." Tom Christiansen in <6k02ha$hq6$3@csnews.cs.colorado.edu>


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

Date: Sun, 11 Oct 1998 04:25:41 +0000
From: WMWilson <m.v.wilson@erols.com>
Subject: Re: NETWORK & GRAPHIC CARD INFO
Message-Id: <36203345.DDD0CFAD@erols.com>

Nathan V. Patwardhan wrote:
> 
> SattarS (sattars@aol.com) wrote:
> 
> : Can anyone tell me how to get Network card and Graphic Card details
> : from Perl? Thanks
> 
> Thanks for the two-line message preceded by the 80-line header.  Die.
> 
> *plonk*
> 
> --
> Nate Patwardhan|root@localhost
> "Fortunately, I prefer to believe that we're all really just trapped in a
> P.K. Dick book laced with Lovecraft, and this awful Terror Out of Cambridge
> shall by the light of day evaporate, leaving nothing but good intentions in
> its stead." Tom Christiansen in <6k02ha$hq6$3@csnews.cs.colorado.edu>

NOT TO MENTION THE ALL CAPS HEADER.
-- 
		\||/
		(..)
    +---oOOo-----(_)-----oOOo-----+
    | mailto:m.v.wilson@erols.com |
    |	       WMWilson	  	  |
    |__USCS Data Center Sysadmin__|


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

Date: Sun, 11 Oct 1998 07:30:42 GMT
From: Lonnie Watson <harlock1@home.com>
Subject: ODBC access to Micro$oft SQL server
Message-Id: <3621AF12.4C88458C@home.com>

I
currently
have a
need to
connect
DBI and
DBD
modules in
Perl to a
Microsoft
SQL server
running on
an NT
machine
also in
the
network. I
am running
Red Hat
5.1 with
perl 5.04
( I think
its .04 ).
At any
rate I am
looking
for anyone
who can
tell me
what is
also
necessary
on the
Linux side
for this
to happen.
I think I
need to
use the
iODBC
driver. I
have taken
that
product
and
compiled
it into
the
system but
when the
DBD module
goes
throuh its
tests it
fails to
connect to
the sql
server. On
the Sql
side I see
no
evidence
that the
connection
attempt
was even
made. Not
really
sure where
to turn
here...

Has anyone
out there
in Perl
land
achieved
this
before?




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

Date: Sun, 11 Oct 1998 02:56:24 +0100
From: "Martin" <minich@globalnet.co.uk>
Subject: Re: Question about locking files
Message-Id: <6vp37r$lrl$1@newnews.global.net.uk>

>You lock it because you want to make sure that there *aren't* any
>other users writing to the file.  Unless you lock it, you can't be
>sure.


Could I just ask, if a file is being read by a script, and then another
script opens it and adds a line to it and closes it again, would that
line still be there when the first script closes it? I think it would but am
not sure. So, if I was writing a script to add a name to the end of a file
every few seconds and then several other users read the data from that
file every second or so, if a person was reading data from the file when
it was being appended to, I'm assuming no data would be lost. The user
might not see the latest addition to the file, but I assume that the
addition
will be made.

Martin




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

Date: 10 Oct 1998 22:33:55 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Question about locking files
Message-Id: <6vp5ej$4pc$1@monet.op.net>

In article <6vp37r$lrl$1@newnews.global.net.uk>,
Martin <minich@globalnet.co.uk> wrote:
>Could I just ask, if a file is being read by a script, and then another
>script opens it and adds a line to it and closes it again, would that
>line still be there when the first script closes it? 

It depends!  See http://www.plover.com/~mjd/perl/buffer_flush.html
for details.

>So, if I was writing a script to add a name to the end of a file
>every few seconds and then several other users read the data from
>that file every second or so, if a person was reading data from the
>file when it was being appended to, I'm assuming no data would be
>lost. 

None would be lost, but the readers might get garbled data or partial lines.

>I assume that the addition will be made.

If you're sure you're the only one writing to the file then yes, all
the data will be written.  



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

Date: 10 Oct 1998 21:31:53 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: sort with dynamic sort expression help
Message-Id: <6vp1q9$490$1@monet.op.net>

In article <6vo4u6$duc$1@news.jump.net>,
John Newman  <jnn@serv1.jump.net> wrote:
>
>Basically, I want to be able to have a scalar that 
>represents an expression to use for sorting so I can use different
>sorts based on command line parameters.  

Depending on what you really want, this might be better than Larry
Rosler's suggestion:


	$sorteq = sub { $a cmp $b };

	foreach (sort $sorteq keys %hash) { ... }


>$sorteq = "$a cmp $b";
>Undefined sort subroutine "main::cmp" called at ./2week.pl line 59, <FH> chunk 112.
>This seems to indicate it's doing something somewhat close to the
>general idea of what I'm trying to get done <g>.

Nope, totally wrong.  Try changing it to say 
>    $sorteq = "(($a cmp $b))";

and you'll see what went wrong.



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

Date: Sun, 11 Oct 1998 03:51:02 GMT
From: Rick Delaney <rick.delaney@shaw.wave.ca>
Subject: Re: sort with dynamic sort expression help
Message-Id: <36202CC8.9DFCD4D5@shaw.wave.ca>

Mark-Jason Dominus wrote:
> 
> Depending on what you really want, this might be better than Larry
> Rosler's suggestion:
> 
>         $sorteq = sub { $a cmp $b };
> 
>         foreach (sort $sorteq keys %hash) { ... }
> 

It might be, if it worked.  :-)

    Not a GLOB reference 

You need something like:

    sort { &$sorteq } keys %hash

like in lr's post.

Another thing that might be useful would be to provide a name for the
type of sort desired, instead of the code.

    my %sort_type = (
        NUM => sub { $a <=> $b }, 
        CMP => sub {$a cmp $b},
    );

    my $sortsub = $sort_type{NUM};
    my @sorted = sort { $&sortsub } @unsorted;

This can also be done by providing the name for the sort subroutine,
like in rjk's post.

-- 
Rick Delaney
rick.delaney@shaw.wave.ca


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

Date: 11 Oct 1998 00:10:06 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: sort with dynamic sort expression help
Message-Id: <6vpb2u$583$1@monet.op.net>

In article <36202CC8.9DFCD4D5@shaw.wave.ca>,
Rick Delaney  <rick.delaney@shaw.wave.ca> wrote:
>Mark-Jason Dominus wrote:
>> This might be better than Larry
>> Rosler's suggestion:
>
>It might be, if it worked.  :-)

Egads!  And I was so sure!  Thanks for pointing out that I was
mistaken.

cat >>PROJECTS
	`sort' should accepts a coderef that refers to thee comparator
	function. 
^D




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

Date: Sat, 10 Oct 1998 21:59:18 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: sort with dynamic sort expression help
Message-Id: <MPG.1089db04f5c401dc9898ad@nntp.hpl.hp.com>

[Posted to comp.lang.perl.misc and copy mailed.]

In article <36202CC8.9DFCD4D5@shaw.wave.ca> on Sun, 11 Oct 1998 03:51:02 
GMT, Rick Delaney <rick.delaney@shaw.wave.ca> says...

<SNIP>(correction of attempted improvement by MJD of my code :-)</SNIP>
 
>     my %sort_type = (
>         NUM => sub { $a <=> $b }, 
>         CMP => sub {$a cmp $b},
>     );
> 
>     my $sortsub = $sort_type{NUM};
>     my @sorted = sort { $&sortsub } @unsorted;

Indeed, selecting one of a set of predefined sorts is a possible 
interpretion of the original request:

'...  Basically, I want to be able to have a scalar that 
represents an expression to use for sorting so I can use different
sorts based on command line parameters.  Here's what it should look
like:  $sorteq = "$a cmp $b"; ...'

I interpreted this as something rather more sophisticated:  where the 
comparison algorithm is itself provided to the program as a parameter in 
the form of a string.  In that case, an 'eval STRING' is unavoidable, and 
can be done only once -- to create the anonymous comparison subroutine, 
which can then be invoked as shown above.  

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Sat, 10 Oct 1998 20:04:26 -0400
From: Bill Jones <bill@fccj.org>
Subject: To Mass Question Mailer...
Message-Id: <361FF60A.CE1DF16@fccj.org>

Nothing like using a shot gun to harrass 80+
people with your question...

-Sneex- :( 
__________________________________________________________________
Bill Jones  FCCJ Webmaster  http://www.fccj.org/cgi/mail?webmaster


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

Date: Sun, 11 Oct 1998 05:38:08 GMT
From: tekkin@hotmail.com (Ken Williams)
Subject: Whole Random number?
Message-Id: <36204483.0@news.cgocable.net>

When I do a:

$iChoice = rand(10);
print "$iChoice";

I get a number like 4.39449955243617

How do tell perl or rand or to only return the 4(or random number) and not the 
 .39...

Thanks.


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

Date: Sun, 11 Oct 1998 05:45:25 GMT
From: tekkin@hotmail.com (Ken Williams)
Subject: Re: Whole Random number?
Message-Id: <3620463a.0@news.cgocable.net>

In article <36204483.0@news.cgocable.net>, tekkin@hotmail.com (Ken Williams) wrote:
>When I do a:
>
>$iChoice = rand(10);
>print "$iChoice";
>
>I get a number like 4.39449955243617
>
>How do tell perl or rand or to only return the 4(or random number) and not the 
>.39...

Never mind, its like $iChoice = int rand(10);

Thanks, your all the greatest.  Linux rulez.


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

Date: Sun, 11 Oct 1998 01:12:36 -0500
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: Why it doesn't work under UNIX??????
Message-Id: <fl_aggie-1110980112370001@aggie.coaps.fsu.edu>

In article <361FB514.F06B8911@mcmahon.qal.berkeley.edu>, Andrew Perrin
<aperrin@mcmahon.qal.berkeley.edu> wrote:

+ Why would it?  These lines make no sense at all to me:
+ > @txt = <*.txt>;
+ > @zip = <*.zip>;
+ 
+ you're asking perl to take the lines in a filehandle called *.zip
(which, by the
+ way, could add lots of troubles since * also signifies a typeglob)

Your first interpretation is incorrect, while your second thought touches
upon the correct answer:

<*.txt> is a typeglob. Exhibit A:

% touch a.txt b.txt c.txt
% touch a.zip b.zip c.zip

% perl
@txt = <*.txt>;
@zip = <*.zip>;
foreach $txt (@txt) {
print "$txt\n";
}
foreach $zip (@zip) {
print "$zip\n";
}

Results:

a.txt
b.txt
c.txt
a.zip
b.zip
c.zip

Oh, well, it happens... :)

James


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

Date: 10 Oct 1998 23:46:24 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Writing an intelligent forking server.
Message-Id: <6vp9mg$53i$1@monet.op.net>

Ed Hill wants to do something interesting:
>    * The main process sits on a socket and reads in the first line of
>      a clients request.  That line contains a index key that can be
>      reused by multiple requestors (a session indicator).

Fine so far.

>    * The main process looks to see if it has already forked a child to 
>      handle that session, and if not it forks off a child, shares the
>      socket with the child 

Also fine, but sharing the socket is pointless.  The client is going
to close the socket soon, and then the socket will be disconnected and
useless.  Next time the client connects for the same session, the
parent server will have a new socket; it can't give this socket to the
child process, so it will have to pass the data to the child through
some other channel.  Since you'll have to program this second method
anyway, you might as well use it for the initial request as well.

Here's how I might do what you want to do:

* The master process will get a connection, read the session
  identifier, and decide whether or not to fork a child.   

* Skip this whole step if the child is already running.

  If a new child is neccesary, the master should create two FIFOs.
  Then it should fork.  The parent should go back to what it was
  doing.  The child should attach its stdin and stdout to the FIFOs
  and then exec the session manager program that will talk to the client.

  A FIFO has the semantics of a pipe, but it also has a name in the
  filesystem like a file.  The filename lets a process open the pipe
  even if it didn't create the pipe.

* Whether or not the session is new, the master should arrange that
  the rest of the client data gets put into the input FIFO and the
  data coming out of the child via the output FIFO gets sent back to
  the client.  See below for strategies about this.

* The session managers will talk to the clients via the FIFOs.  They
  can implement the session destruction policy.  If the client says
  the session is over, or iff too much time elapses without a
  connection, the session manager can exit.  When a session manager
  exits, the master server will detect in the usual way (with wait* or
  SIGCHLD) and should unlink the FIFOs.

* The session manager programs should just read stdin and write
  stdout.  The master server can invoke them with stdin and stdout
  already attached to the correct FIFOs.  This will make them more
  flexible.  You can develop them independently from the master
  server, which doesn't have to manage sessions at all.  You can have
  several different session managers, called under different
  circumstances, and they will all be easy to develop and test because
  you can develop them without using the network.

I hope that's clear.

To create a FIFO, use

	use Fcntl;             # For S_IFIFO
	require 'syscall.ph';  # For SYS_mknod
	syscall(&SYS_mknod, $filename, S_IFIFO | 0640, 0);
	# 0640 is the permissions of the FIFO.  0 is ignored.

or execute the `mknod' or `mkfifo' command.

There are several ways to arrange that the client data goes to and from
the FIFOs.

1. The master server can take care of copying the data, by selecting
   on the sockets and FIFOs and copying the data when it is available.

2. The master server can fork a couple of copies of `cat' to take care
   of copying the data.  cat #1 has its stdin attached to the client
   socket and its stdout attached to the input FIFO, and cat #2 has
   its stdin attached to the output FIFO and its stdout attached to
   the client socket.  Then the master server can forget about copying
   data; the cats will take care of it and exit when the data is gone.

3. You can write a little data-copying program that inherits the
   client socket and the open FIFOs from its parent, and copies the
   data back and forth, either by using `select' or by paying
   attention to the content and understanding the protocol so that it
   knows when to read and when to write.

Which one of these you choose depends on how long the conversations
will last.  #1 has low startup costs but is hardest to program it's
also stupid, since the whole point of the exercise was to exclude the
master server from having to handle client sessions, and in #1 we have
it shuttling all the data from the client to the session manager and
back; it might as well just manage the session itself.  But I put it
in because it's simple and makes it easy to see where the data is
going.  #2 is very easy to program and trnsfers the data very
efficiently, but requires that you start two new processes per client.
#3 is somewhere in between.  If processes will have to copy a lot of
data in each conversation, the startup costs are not so important as
if conversation is short.

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

Now, all that said, that is not necessarily what I would want to do.
If you control the protocol, you can tell the client to make future
connections to a different port.  Then you can do this:

	Client connects to master server.
	Server selects random port number, say 1090.
	Server creates and binds new socket listening on port 1090.
	Server forks.  Parent closes socket 1090.
	Parent sends client a message that says `Please direct future
		communications to port 1090.'  Parent ends connection
		to client.
	Child waits for client to connect, possibly execs session
		manager, etc.
	Client reconnects to port 1090 and takls to child, as
		instructed to by parent.

This works very well in some environments.  For example, if the server
is an HTTP server, it can send the cilent a redirect message to tell
it to start talking to http://your.host:1090/.  When the client
connects, it's talking to its own private HTTP session server.  The
CGI::MiniSvr module does this.

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

>So there has to be some type of sharing of filehandles between parent
>and child or other trickery that I can't seem to wrap my brain
> around.

There is only one way to share a filehandle:  When a process forks,
its filehandles are inherited by its children.  But processes have
other ways to communicate:

1. They can exchange data through a file.
2. They can send signals.
3. They can exchange data through a pipe, if they are related.
4. They can exchange data through a FIFO.
5. They can use the System V IPC mechanisms:  Semephores, message
   queues, and shared memory segments.
6. When a child process exits, its parent can find out and can get the
   exit status with `wait' calls.

The first method I described above mostly uses FIFOs, but you could
subsititute message queues or shared memory for the FIFOs with no
significant changes.   You could also replace the FIFOs with plain
fails, perhaps using signals or semaphores to tell the processes when
data was ready.

The CGI::MiniSvr idea sidesteps the whole issue by directing the
client to talk directly to the session manager.  That is a big
benefit.

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

>Does anyone have any advice on implementing what I'm trying to achieve?

1. You might also want to address this question to comp.unix.programmer.
It is more in their line, since it doesn't really have anything in
particular to do with Perl.  (Not that I care, because it was not boring.)

2. What you are doing is difficult and interesting, not an ordinary
thing to do.

3. It always pays to avoid signals.  Signals suck.


Hope this helps,



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

Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>


Administrivia:

Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.

If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu. 


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.misc (and this Digest), send your
article to perl-users@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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 3945
**************************************

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