[6438] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 63 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 6 13:46:12 1997

Date: Thu, 6 Mar 97 10:00:23 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 6 Mar 1997     Volume: 8 Number: 63

Today's topics:
     Re: /.*/ works, /[.]*/ doesn't, why!?!!?!! (Tad McClellan)
     Re: /.*/ works, /[.]*/ doesn't, why!?!!?!! <lmichael@gdc.com>
     Re: ARGUMENTS - HELP (Tad McClellan)
     Re: ARGUMENTS - HELP (Chris Russo)
     bidirectional by select() <per.josefsson@mailbox.postnet.se>
     Creating a Daemon (Luigi Mattera)
     Do I have to worry about memory leak if I am designing  (Wing Choy)
     Re: Do I have to worry about memory leak if I am design <eryq@enteract.com>
     FrontPage Publish & WebBot Emulation <jrhamilton@lucent.com>
     Re: How to make perl scripts appear locally on the brow (Ronald L. Parker)
     How to use perl as an interface to another program (John Jiang)
     Initializing contents of arrays to zeros. really_eliot@dg-rtp.dg.com_but_mangled_to_stop_junk_email
     main differences between Awk and Perl <chauve@info.univ-angers.fr>
     Re: mathematically correct? <myshkin@cyclone.stanford.edu>
     Perl 5.0 bug?  dbmopen(), each(), and hash references <rnewman@cybercom.net>
     Re: Perl on Windows 95 (Walt Mills)
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Thu, 6 Mar 1997 10:01:14 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: /.*/ works, /[.]*/ doesn't, why!?!!?!!
Message-Id: <aopmf5.162.ln@localhost>

Peter de Vroomen (peterv@valkieser.nl) wrote:
: Hello wizards,

: I'm trying to break a line into words by using the following
: construction:

: while( <> )
: {
:     $new_line = "";
:     while( $_ ne "" )
:     {
:         $new_line .= $1;
:         /(\s*)([^\s\n]*)([\n.]*)/;
:         if( /$match/ )
:         {
:             $new_line .= $somestring . $2 . $anotherstring;
:         }
:         else
:         {
:             $new_line .= $2;
:         }
:         $_ = $3;
:     }
:     print $new_line;
: }

: Please note that I wrote this code to be readable....

: The trouble is $3 is empty, no matter what. I tried to narrow down the
: problem by trying out the following pattern-matches:

: 1. /[\n.]*/


/.*/s   is equivalent to what you were _trying_ to get above.
    ^
    ^

the 's' allows dot to match newline


: 2. /[.]*/
: 3. /.*/

: Only the last one works (as I'd been expecting). The question is:
: WHY!?!?!!?!!

: If /a*/ and /[a]*/ do the same thing, then why not /.*/ and /[.]*/???
                                             ^^^^^^^^^^^^^^^^^^^^^

Because dot is not a meta-character when it appears inside of a
character class. That thingy in square brackets is a character
class.


: I'm using Perl version 5.003_25. Is this a bug or have I missed
: something in the docs?


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


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

Date: 06 Mar 1997 11:46:25 -0500
From: Laurence Michaels <lmichael@gdc.com>
To: peterv@valkieser.nl (Peter de Vroomen)
Subject: Re: /.*/ works, /[.]*/ doesn't, why!?!!?!!
Message-Id: <bhqrahtvuhq.fsf@gdc.com>

CC'd to original poster

The answer is that []'s remove the 'magic' from several special
characters, including '.' and '*'.

So, here's what perl will match:
Looking for /.*/:
In this case, . will match any character except for \n.  And so /.*/
matches 0 or more of any character except for \n.

Looking for /[.]*/:  [.] matches a '.', and so /[.]*/ matches 0 or
more '.'s.

What you want to use is
/(.|\n)*/
which should match 0 or more of any character, including \n. The
parentheses are necessary.

Of course, there is a simpler solution:
Read the documentation on 'split', 'join', and lists.
The following code may do the trick:
while (<>) {
    print join '\n', split / /, $_;
}
Untested, use at your own risk.

Later,
Laurence Michaels
I do not speak for anyone but myself :)


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

Date: Thu, 6 Mar 1997 09:52:47 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: ARGUMENTS - HELP
Message-Id: <f8pmf5.s22.ln@localhost>

Coulon (X.Coulon@herts.ac.uk) wrote:
: Hello,
: I would like to write a Perl script what gets command line arguments,
                                                ^^^^^^^^^^^^^^^^^^^^^
: but I don't know how to do it. 
      ^^^^^^^^^^^^^^^^^^^^^^^^^

How you do it is, you look it up in the free documentation that is
included with the perl distribution!

Remember when you got the autoFAQ for c.l.p.m?

Remember reading this part?

------------------
6. Have you read the man pages? 
------------------

I guess not...

grep 'command line argument' *pod

finds _one_ line:

------------------
perlvar.pod:The array @ARGV contains the command line arguments intended for the
------------------


: Is it stored in a variable/array ?


So yeah, it's stored in an array.


: Thanks for your help.

Uh huh.


--
    Tad McClellan                          SGML Consulting
    Tag And Document Consulting            Perl programming
    tadmc@flash.net


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

Date: Thu, 06 Mar 1997 09:01:42 -0800
From: crusso@alink.net (Chris Russo)
Subject: Re: ARGUMENTS - HELP
Message-Id: <crusso-0603970901420001@buzz.alink.net>

In article <331E9BA9.7BA@herts.ac.uk>, Coulon <X.Coulon@herts.ac.uk> wrote:

>Hello,
>I would like to write a Perl script what gets command line arguments,
>but I don't know how to do it. Is it stored in a variable/array ?
>Thanks for your help.
>Xavier Coulon
>X.Coulon@herts.ac.uk

If you can't even be troubled to look at a couple of man pages, why should
people here take the trouble to help you?

Come back when you've made a little effort.

----------------------------------------------------------------------
Chris Russo                          A-Link Network Services, Inc.
crusso@alink.net                     Bolo me
http://www.alink.net/~crusso


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

Date: Thu, 06 Mar 1997 17:03:46 +0100
From: Per Josefsson <per.josefsson@mailbox.postnet.se>
Subject: bidirectional by select()
Message-Id: <331EEAE2.2D91@mailbox.postnet.se>

I don=92t understand select($rout=3D$rin, undef, $eout=3D$ein, undef); =


I would like a program witch copy data in both directions. Can anyone
please advice me.

Attached is a program that only works the first time in the loop. Second
time and fourth it falls through the =93select=94 function immediately.

Thanks in advance

/PerJ


-- follow ---- My missbehaving program, to be corected ---- follow --

#!/usr/local/bin/perl -w
require 5.002;
use strict;
use Socket;
use FileHandle;

my (	$remote, $port, $iaddr, $paddr, $proto, $line, =

	$rin, $rin1, $rin2, $rout, =

	$win, $win1, $win2, $wout, =

	$ein, $ein1, $ein2, $eout, =

	$read, $rc);
	=


$remote =3D shift || 'localhost';
$port =3D shift || 2345;
if ($port =3D~/\D/) { $port =3D getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr =3D inet_aton($remote)		or die "no host: $remote";
$paddr =3D sockaddr_in($port, $iaddr);

$proto =3D getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) 	or die "socket: $!";
connect(SOCK, $paddr);
$line =3D "";

SOCK->autoflush(1);
STDOUT->autoflush(1);

$rin=3D""; $rin1=3D""; $rin2=3D""; $rout=3D"";
$win=3D""; $win1=3D""; $win2 =3D""; $wout=3D"";
$ein=3D""; $ein1=3D""; $ein2=3D""; $eout=3D"";
vec($rin1,fileno(SOCK),1) =3D 1; vec($rin2,fileno(STDIN),1) =3D 1;
vec($win1,fileno(SOCK),1) =3D 1; vec($win2,fileno(STDIN),1) =3D 1;
vec($ein1,fileno(SOCK),1) =3D 1; vec($ein2,fileno(STDIN),1) =3D 1;
$rin =3D $rin1 | $rin2;
$win =3D $win1 | $win2;
$ein =3D $ein1 | $ein2;


for (;;) {
	print STDERR "Entering loop.\n";
	$rc =3D select($rout=3D$rin, undef, $eout=3D$ein, undef);
	print STDERR "Through \"$rc\" =3D select(", =

		unpack("b", $rout), ", ", =

		unpack("b", $wout), ", ", =

		unpack("b", $eout), =

		").\n";
	=

	if (vec($rout,fileno(STDIN),1)) {
					### do STDIN things
		print STDERR "STDIN things\n";
		sysread(STDIN, $line, 2048);
		print STDERR "=3D=3D>", $line, "<=3D=3D\n";
		syswrite(SOCK, $line, length($line));
		}
	}

	if (vec($rout,fileno(SOCK),1)) {
					### do SOCK things
		print STDERR "SOCK things\n";
		$read =3D sysread(SOCK, $line, 2048);
		print STDERR "=3D=3D $read =3D=3D>", $line, "<=3D=3D\n";
		print $line;
		}
	=

close(SOCK)				or die "close: $!";
exit;


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

Date: 6 Mar 1997 17:09:29 GMT
From: mattera@ssga.ssb.com (Luigi Mattera)
Subject: Creating a Daemon
Message-Id: <5fmto9$i49@svna0001.clipper.ssb.com>

  I want to create a daemon process using perl, but the few books I
have been able to find don't mention it at all.  Is there a way to do
this?  I can create a daemon under C using system libraries and
commands, but I don't know how to do this with perl.  Any suggestions?
I have Perl V5, but my books are all based on V4.

  Speaking of C system libraries, is there any way to recreate their
use under perl?  I am interested in using some system calls under Perl
and was wondering if they were useable or had to be implemented
manually like fork.  If they exist under V5, then see my next
paragraph.

  Also, can anyone recommend a book on V5?  I only have access to V4
books and I would like to read up on the differences between the two
as we have v5 compiled.


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

Date: 6 Mar 1997 16:52:04 GMT
From: whc@mink.att.com (Wing Choy)
Subject: Do I have to worry about memory leak if I am designing a perl deamon?
Message-Id: <5fmsnk$2p6@newsb.netnews.att.com>

We are developing a perl script which act like a deamon that 
it listen to a socket, handle the request coming in, and
spawn child to handle the request.

Do I have to worry about memory leak?  Is there anything I 
need to watch out for when designing a deamon like this?

Thanks.

	Wing Choy
	whc@mink.att.com



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

Date: Thu, 06 Mar 1997 11:33:41 -0600
From: Eryq <eryq@enteract.com>
To: Wing Choy <whc@mink.att.com>
Subject: Re: Do I have to worry about memory leak if I am designing a perl deamon?
Message-Id: <331EFFF5.3DB56EF0@enteract.com>

Wing Choy wrote:
> 
> We are developing a perl script which act like a deamon that
> it listen to a socket, handle the request coming in, and
> spawn child to handle the request.
> 
> Do I have to worry about memory leak?  Is there anything I
> need to watch out for when designing a deamon like this?

If your daemon produces circular data structures while inside
its accept() loop, you will gradually gnaw away at core: Perl's
reference-counting garbage collection prevents it from cleaning
the things up: you have to break the circularity first.  

(Anyone... are we ever going to get mark-and-sweep?  Just curious.)

I think even the trivial case of:

	my $a;
	$a = \$a;

will produce an un-reapable scalar.

-- 
  ___  _ _ _   _  ___ _   Eryq (eryq@enteract.com)
 / _ \| '_| | | |/ _ ' /  Hughes STX, NASA/Goddard Space Flight Cntr.
|  __/| | | |_| | |_| |   http://www.enteract.com/~eryq
 \___||_|  \__, |\__, |___/\  Visit STREETWISE, Chicago's newspaper by/
           |___/    |______/ of the homeless: http://www.streetwise.org


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

Date: Thu, 06 Mar 1997 10:19:08 -0600
From: joseph_ray_hamilton <jrhamilton@lucent.com>
Subject: FrontPage Publish & WebBot Emulation
Message-Id: <331EEE7C.4CDE@lucent.com>

I have started to play with FrontPage 97 and more specifically,
the Publish function which allows you to create a collection of
Web Pages based on several existing templates.

Then you can "publish" this on an existing web site.  I am attempting
to target a Solaris environment.  Indeed, it works rather well for
basic page creation.  However, Microsoft's answer to CGI-programming
seems to be these WebBot thingies.

Has anyone started to build perl programs to emulate these DOS/Windows
programs?  I imagine that it would be a straightforward disassembly,
analysis and building it in perl.  But I am really hoping that
someone else has already done it.

Any ideas???

Please respond via email.

Joe Hamilton


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

Date: Thu, 06 Mar 1997 16:08:04 GMT
From: ron@farmworks.com (Ronald L. Parker)
Subject: Re: How to make perl scripts appear locally on the browser
Message-Id: <3323ce4c.3963788@10.0.2.33>

On Tue, 04 Mar 1997 11:06:41 -0800, Paul-Joseph de Werk
<Paul.deWerk@MCI.Com> wrote:

>herryh@xs4all.nl wrote:
>> 
>> Hi,
>> 
>> I have a problem getting my perl-scripts to output to my browser.
>> presently I test my scripts by copying them to the perl\bin dir. and
>> run
>> perl myfile.pl > myfile.html
>> and then have my browser read myfile.html.
>> 
>> But what I really want is having my browser read the perl script
>> directly. But all that happens is a unreadably quick appearing
>> and disappearing dos-box saying Bad Command or Filename.
>> I want the same output as when I run it manually at the dosprompt
>> and I want the output being directed to my browser.
>> 
>> I installed O'reilly's Website, Netscape 3.01 and Win32 Perl.
>
>Have you tried placing the perl scripts in WebSite's cgi-bin directory,
>and using the cgi-bin URL through WebSite to run it?  (Make sure you
>have .pl extensions associated with perl.exe)  I have this on my comp
>for my own development and it wrks fine for me.

(Strictly speaking, this isn't a Perl question or a Perl answer.  It
really belongs in one of the CGI groups.  Nonetheless, perhaps you
Perl groupies can tear apart the code that follows.)

I've put the following perl program where I can find it:

----------8<--clip-n-save------------
$ARGV[0] =~ /(.*)\\/;
chdir( $1 );
exec 'perl ' . join( ' ', @ARGV );
---------->8--clip-n-save------------

(I know, it's not perfect or taint-free or any of that.  It's not on a
public server and it works for my limited purposes.  Feel free to
modify it as necessary.)

and I changed the CGI shell to 
"c:\perl5\ntt\perl.exe c:\perlprog\webrun.pl"

which is the name of my perl interpreter followed by the name of the
above program.  This ensures that WebSite's behavior matches the
behavior of Apache in setting the current directory to the directory
the script resides in.  This gives me the advantage of keeping the
same link structure I have on the production server, where I don't
have write access to cgi-bin but I can execute CGI scripts in any
directory (rare, I know.)  It also means that anything of type
wwwserver/shellcgi is a Perl script, irrespective of whatever botched
mappings might exist in Windows.  (and believe me, they can get
botched.  Is a PCD file a PhotoCD or an MS Test pseudocode file?)

Now if I could just get WebSite to read .htaccess files....

--
Ron Parker
Software Engineer
Farm Works Software       Come see us at http://www.farmworks.com


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

Date: 6 Mar 1997 16:11:29 GMT
From: jiangj@bnr.ca (John Jiang)
Subject: How to use perl as an interface to another program
Message-Id: <5fmqbh$9fb@crchh327.rich.bnr.ca>

Greetings,

	I am trying to use the open() which has a pipe symbol (|) in
the filename  to open a command. If I put pipe at the beginning I can
write and if I put pipe at the end of filename then I can read. My
question is : How can I open a command and do input/output at the same
time like interactively. Thanks


--
 ______________________________________________
|XXXX^____.  _.    ^XXXX| Xueming Jiang (John) |
|XXX  \__ | | |      XXX| NORTEL, Richardson   |
|XX     | |_| |__.    XX|......................|
|X    __| |..\ \ |     X|     (972)684-8344    |
|X____\___/__/_/_|_____X|__jiangj@nortel.com___|


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

Date: 06 Mar 1997 10:37:58 -0500
From: really_eliot@dg-rtp.dg.com_but_mangled_to_stop_junk_email
Subject: Initializing contents of arrays to zeros.
Message-Id: <bvvi75qbe1.fsf@remus.i-have-a-misconfigured-system-so-shoot-me>


Thanks to Bill Cowan <billc@tibinc.com> for pointing out that what I
wanted was to assign
	(0) x $num_elements
into the array.  I had missed putting parentheses around the 0, and
consequently got "00000000...".

Someone else kindly pointed out that saying

foreach $index (0 .. $size-1)

resulted in the creation of a temporary array, which was then promptly
discarded.  Does using the "(0) x $num_elements" construct have any
such hidden pitfalls?

Topher Eliot                           Data General Unix Core Development
(919) 248-6371                                        eliot at dg-rtp.dg.com
Obviously, I speak for myself, not for DG.
Visit misc.consumers.house archive at http://www.geocities.com/Heartland/7400
"I like to get the chicks messy."  
	-- Peter, age 4, on why he likes the barnyard-scene cereal bowl.


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

Date: Thu, 06 Mar 1997 18:06:33 +0100
From: Candice Chauve <chauve@info.univ-angers.fr>
Subject: main differences between Awk and Perl
Message-Id: <331EF999.2D21@info.univ-angers.fr>

What are the principal reasons for using Perl or Awk?
What are the main functions of Perl in comparaison with the functions of
Awk?
What can decide us using Perl more than Awk?
We are french students and we only use Awk ,because we don't know the
Perl language!So we are waiting from you to decide us to learn Perl!


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

Date: 06 Mar 1997 09:29:09 -0800
From: Robert Au <myshkin@cyclone.stanford.edu>
To: tloser@valdemar.microserve.com (Thomas A. Loser)
Subject: Re: mathematically correct?
Message-Id: <mb87mjludy2.fsf@cyclone.stanford.edu>

[posted and mailed]

tloser@valdemar.microserve.com (Thomas A. Loser) writes:
>   The PERL (camel) book gives an example for selecting a random line
> from a file of unknown length with a single pass through the file:
> 
>   (paraphrased from page 246) 
> 
>   "This procedure selects a line at random from a file, using just
> one pass over the file and without knowing in advance the number of
> lines. It works by calculating the probability that the current line
> would be selected with a probability of 100%, but the second line 
> has a 50% chance of replacing the first one, the third line a 33%
> chance of replacing one of the first two, and so on."
> 
>   srand;
>   rand($.) < 1 && ($it = $_) while <>;
> 
>   I'm no math genius but if I assume just 3 lines it appears that line
> 2 will replace line 1 once every other time (1 out of 2) and line 3
> will replace 1 *or* 2 every third time (1 out of 3) leaving line 1 as
> the selected line just 1 out of every six times. (1/2 + 1/3 = 5/6
> chance of line 1 being replaced). Is my logic faulty or does this 
> routine heavily favor lines later in the file? I'm attracted to its
> simplicity but not if it's way skewed.

Nope, the algorithm works as advertised.  You're double-counting the one
in six times in which 2 replaces 1, and then 3 replaces 2.  To wit:

Line 1 is chosen if it was chosen first (1/1) AND if 2 does not replace
it (1/2) AND if 3 does not replace it (2/3); that is, it is chosen with
probability 1*(1/2)*(2/3) = 1/3.

-- 
Robert Au
feyngold@leland.stanford.edu
myshkin@cyclone.stanford.edu  


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

Date: 6 Mar 1997 00:21:32 -0500
From: Ron Newman <rnewman@cybercom.net>
Subject: Perl 5.0 bug?  dbmopen(), each(), and hash references
Message-Id: <5flk8s$8rd@jimmy.zippo.com>

The following Perl script misbehaves very mysteriously in Perl 5.002.
It should print out 351 lines, one for each element of the hash.
Instead, it prints out just 8 lines:

   343 = -343
   342 = -342
   311 = -311
   310 = -310
   307 = -307
   0 = -0
   349 = -349
   344 = -344

If I remove the lines

        # program works correctly if you remove the next line
	my $i = $hashref->{int(rand(350))};

then the program works correctly.

Am I doing something really wrong here, or is this a bug in
Perl 5.002?

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

#!/usr/bin/perl

use strict;

sub make_hash {
    my %hash;
    my $i;
    dbmopen (%hash, "/tmp/hash", 0666);
    foreach $i (0..350) {
	$hash{$i} = -$i;
    }
    return \%hash;
}

sub list_hash {
    my ($hashref) = @_;
    my ($key, $value);
    while (($key, $value) = each %{$hashref}) {
	print "$key = $value\n";
        # program works correctly if you remove the next line
	my $i = $hashref->{int(rand(350))};
    }
}

my $hashref = make_hash();
list_hash($hashref);


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

Date: Thu, 06 Mar 1997 15:20:08 GMT
From: millsjw@airmail.net (Walt Mills)
Subject: Re: Perl on Windows 95
Message-Id: <331edf96.298657866@news.airmail.net>

On Fri, 28 Feb 1997 09:16:18 -0500, Charlie Wu 
>
>I have Perl5.003 and pws1.0 - and also associated .pl with
>c:\perl5\bin\perl.exe, and in the registry added an entry 
>".pl	c:\perl5\bin\perl.exe" (as suggested in one of the dejanews
>articles) but still no luck...
>

The short answer:

1) Put Perl in the /cgi directory. (dangerous)
2) Call your script with
http://yourserver.com/cgi/perl.exe?script.pl
 or use a batch file that has:

@perl.exe script.pl

as the only line.

There are also some clever ways to have the batch file call itself and
invoke Perl on itself and execute. Saves one file and is cleaner.

Steps 1 & 2 will tell you if Perl is going to work. Perl should also
work at the command prompt in the /cgi directory. You will see the
HTML output when you invoke perl.exe script.pl

add a -w for some troubleshooting hints (perl.exe -w script.pl)



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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.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 63
************************************

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