[17329] in Perl-Users-Digest
Perl-Users Digest, Issue: 4751 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 28 18:05:27 2000
Date: Sat, 28 Oct 2000 15:05:10 -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: <972770710-v9-i4751@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 28 Oct 2000 Volume: 9 Number: 4751
Today's topics:
Re: "unrequire foo;":: Is this possible? <lincmad001@telecom-digest.zzn.com>
ANNOUNCE: Math::Cepehes 0.25 <randy@theory.uwinnipeg.ca>
Re: Bitreverse an integer (Logan Shaw)
Re: Bitreverse an integer <sb@muccpu1.muc.sdm.de>
chmod problems? I think. <cochise@sky.net>
Re: chmod problems? I think. <steve@peachy.com>
Re: debugging cgi scripts <steve@peachy.com>
Re: Detecting socket closure (Garry Williams)
Re: encrypting and decrypting a string (Tad McClellan)
Re: func (a) func !a! func ~a~ func a .... more? <brondsem@my-deja.com>
grep vs. a subroutine to find an occurance of an elemen (Clay Irving)
Re: grep vs. a subroutine to find an occurance of an el <harrisr@bignet.net>
Re: grep vs. a subroutine to find an occurance of an el (Clay Irving)
Re: grep vs. a subroutine to find an occurance of an el (Daniel Chetlin)
Re: grep vs. a subroutine to find an occurance of an el (Clay Irving)
Help with regex espenei@my-deja.com
Re: Help with regex <ren.maddox@tivoli.com>
Re: Help with regex (Tad McClellan)
How to call a perl script from HTML <Bill@net.com>
Re: How to call a perl script from HTML <xzrgpnys@yvtugubhfrovm.pbz>
how to find oldest file in a directory mr_potato_head@my-deja.com
Re: how to find oldest file in a directory (Clay Irving)
How to loop in the background 7brankin9@my-deja.com
Re: Installing HTML module <whataman@home.com>
Re: No such file or directory (NT) (Tad McClellan)
Re: okay i tried ftp but... i dont get it. (Tad McClellan)
Re: perl/cgi programmer in Atlanta (Tad McClellan)
Sending mails from a Unix webserver <g.soper@soundhouse.co.uk>
Re: Sending mails from a Unix webserver (Clay Irving)
variable in module <pilsl@goldfisch.atat.at>
Re: variable in module (Martien Verbruggen)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 28 Oct 2000 14:47:49 -0700
From: Linc Madison <lincmad001@telecom-digest.zzn.com>
Subject: Re: "unrequire foo;":: Is this possible?
Message-Id: <281020001447495227%lincmad001@telecom-digest.zzn.com>
In article <39FAB6E2.C96E9BA9@addtoit.com>, William Lewis Brown
<bbrown@addtoit.com> wrote:
> Hi,
> I have need for the ability to wipe out the definition of a Perl
> class from
> Perl's runtime environment. The operation would eliminate all symbols from
> Perl's symbol table which begin with the name of the class, ex.
> "my::class::".
> I then intend to "re-require" the class. The result, if the class definition
> has changed since the first require, would be a new version of the class in
> Perl's symbol table. It is safe to assume that the class is defined in a
> single
> module.
> Is this possible?
> If it is possible, has anyone already done it?
> If it is possible and it has not been done, could you please give me
> some
> pointers that will start me off in the right direction.
>
> Thanks for your help.
>
> Bill
Camel book, pages 238-239.
------------------------------
Date: Sat, 28 Oct 2000 14:08:51 -0500
From: "Randy Kobes" <randy@theory.uwinnipeg.ca>
Subject: ANNOUNCE: Math::Cepehes 0.25
Message-Id: <svmaafjmvl8038@corp.supernews.com>
NAME
Math::Cephes - perl interface to the cephes math library
DESCRIPTION
Math::Cephes provides a perl interface to over 150 functions
of the cephes math library of Stephen Moshier. These include
various trig, hyperbolic, exponential, Bessel, Gamma, Beta,
elliptic, and hypergeometric functions, among others, as well
as some utilities and statistical distributions. See the README
file for a full list. A simple "calculator" script is included which
provides a command line interface to the module.
CHANGES
The major change in this version involves some fixes needed
to work on multi-threaded perls like ActivePerl.
PLATFORMS
This package requires a C compiler to build. It has been tested
on a Linux (perl 5.005_03) and on a Win98 (ActivePerl 618, based
on perl 5.6.0) machine. To build on different systems will probably
require editing the header mconf.h, which among other things
defines the type of computer arithmetic used. If you need to edit
this file for your system, please let me know of the changes needed,
so they can be automatically set in a future release.
AVAILABILITY
Version 0.25 of this package has been uploaded to CPAN, and
should be available soon in
http://www.perl.com/CPAN/authors/id/R/RK/RKOBES/
A ppm version, suitable for Win32 ActivePerl 6xx, can be
installed via
ppm install
http://theoryx5.uwinnipeg.ca/ppmpackages/Math-Cephes.ppd
best regards,
randy kobes
------------------------------
Date: 28 Oct 2000 14:44:33 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Bitreverse an integer
Message-Id: <8tfab1$o6o$1@provolone.cs.utexas.edu>
In article <8tev8c$ap1$1@lublin.zrz.tu-berlin.de>,
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>Pete M. Wilson <wilsonpm@drop.gamewood.net> wrote in comp.lang.perl.misc:
>>So, use successive masks to reverse the upper and lower half of the
>>string, every quarter, every eigth, etc.
>>
>>In C, it goes something like this:
>>
>>unsigned long x; // 32 bit unsigned integer
>>
>>x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
>>x = ((x & 0xff00ff00) >> 8) | (( x & 0x00ff00ff) << 8);
>>x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
>>x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
>>x = ((x & 0xaaaaaaaa) >> 1) | (x & 0x55555555) << 1);
>
>The one possible drawback is that it relies on a fixed integer size
>(of 32 bits).
That is easy enough to fix. If you write it in Perl like this:
$twiddles =
[
[ 0xffff0000, 0x0000ffff, 16 ],
[ 0xff00ff00, 0x00ff00ff, 8 ],
[ 0xf0f0f0f0, 0x0f0f0f0f, 4 ],
[ 0xcccccccc, 0x33333333, 2 ],
[ 0xaaaaaaaa, 0x55555555, 1 ],
];
foreach $twiddle (@$twiddles)
{
$x = (($x & $twiddle->[0] >> $twiddle->[2])
| ($x & twiddle->[1] >> $twiddle->[2]));
}
Then, different values of $twiddles will make it work for different bit
widths. Since you typically won't use very many different bit widths,
it's reasonable to dynamically compute $twiddles and cache the
results. Here's a first crack at a function to compute it for any bit
width which is a power of 2:
sub twiddles
{
my ($bits) = @_;
my (@twiddles, @masks, $flip_flop, $bit, $period);
foreach ($period = $bits >> 1; $period; $period >>= 1)
{
@masks = ( 0, 0 );
$flip_flop = 0;
for ($bit = 1; $bit <= $bits; $bit++)
{
$masks[0] <<= 1;
$masks[1] <<= 1;
$masks[$flip_flop] |= 1;
$flip_flop = 1 - $flip_flop
if $bit % $period == 0;
}
push (@twiddles, [ @masks, $period ]);
}
return \@twiddles;
}
There's actually a way to compute all powers of 2 up to and including
the one you want all at once, but I don't guess the extra efficiency
gained in doing so is worth the trouble.
- Logan
------------------------------
Date: 28 Oct 2000 20:04:32 GMT
From: Steffen Beyer <sb@muccpu1.muc.sdm.de>
Subject: Re: Bitreverse an integer
Message-Id: <8tfbgg$4dh$1@solti3.sdm.de>
In article <8tev8c$ap1$1@lublin.zrz.tu-berlin.de>, Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>>unsigned long x; // 32 bit unsigned integer
>>x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);
>>x = ((x & 0xff00ff00) >> 8) | (( x & 0x00ff00ff) << 8);
>>x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
>>x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
>>x = ((x & 0xaaaaaaaa) >> 1) | (x & 0x55555555) << 1);
>>// x is now bit reversed!
>>So, in Perl, try this:
>>$x = (($x & 0xffff0000) >> 16) | (($x & 0x0000ffff) << 16);
>>and so on for the remaining lines.
> To fully comply with the specifications of a reversal of $size bits,
> the 32-bit reversal must be followed by a 32 - $size right shift.
> [...] I suppose this is also the algorithm Steffen
> Beyer mentions.
Yes, this is exactly what I remembered having seen before somewhere else.
Thanks a lot for the benchmarks, I wasn't even expecting such a significant
performance boost with only 32 bits, since log(n) grows almost linearly for
small n, i.e., log(n) is almost equal to n for small n.
Very impressive!
Again, thanks for your time and effort!
Best regards,
--
Steffen Beyer <sb@engelschall.com>
http://www.engelschall.com/u/sb/whoami/ (Who am I)
http://www.engelschall.com/u/sb/gallery/ (Fotos Brasil, USA, ...)
http://www.engelschall.com/u/sb/download/ (Free Perl and C Software)
------------------------------
Date: Sat, 28 Oct 2000 16:04:11 -0500
From: Steve <cochise@sky.net>
Subject: chmod problems? I think.
Message-Id: <39FB3F4B.9652747C@sky.net>
We're changing servers and I'm trying to get all the Perl stuff working
before we make the final switchover.
I have a form in our main .html directory that when submitted, sends the
data to a script (Perl) in the "cgi-bin" directory. The program creates
2 different files and updates a third. Updating the file goes OK, but
when I try to access any of the files with my browser I've been getting
the same error:
"Forbidden: You don't have permission to access /cgi-bin/filename.html
on this server."
Which tells me that the chmod permissions are not set correctly. Do I
need to set the chmod in the program somehow? I have tried:
$mode = 0755; chmod $mode, $filename;
but it doesn't help.
The reason I ask is that on our current server I don't, the default (I
guess) is -rwxr-xr-x which allows everyone to access the file.
If I telnet to the new server so that I can do something like a chmod
755 *.txt, will that set the permissions for .txt files to be created in
the future??
Any suggestions? Other than "Hire someone who knows what they're doing"
:)
Thanks for any help,
Steve
------------------------------
Date: Sat, 28 Oct 2000 22:18:14 +0100
From: "Steve Kay" <steve@peachy.com>
Subject: Re: chmod problems? I think.
Message-Id: <8tffc1$ncb$1@neptunium.btinternet.com>
Perhaps adding umask command into your .profile will allow all files created
to have the appropriate permission?
See man umask.
Steve
Steve <cochise@sky.net> wrote in message news:39FB3F4B.9652747C@sky.net...
> We're changing servers and I'm trying to get all the Perl stuff working
> before we make the final switchover.
>
> I have a form in our main .html directory that when submitted, sends the
> data to a script (Perl) in the "cgi-bin" directory. The program creates
> 2 different files and updates a third. Updating the file goes OK, but
> when I try to access any of the files with my browser I've been getting
> the same error:
>
> "Forbidden: You don't have permission to access /cgi-bin/filename.html
> on this server."
>
> Which tells me that the chmod permissions are not set correctly. Do I
> need to set the chmod in the program somehow? I have tried:
>
> $mode = 0755; chmod $mode, $filename;
>
> but it doesn't help.
>
> The reason I ask is that on our current server I don't, the default (I
> guess) is -rwxr-xr-x which allows everyone to access the file.
>
> If I telnet to the new server so that I can do something like a chmod
> 755 *.txt, will that set the permissions for .txt files to be created in
> the future??
>
> Any suggestions? Other than "Hire someone who knows what they're doing"
> :)
>
> Thanks for any help,
> Steve
------------------------------
Date: Sat, 28 Oct 2000 22:13:58 +0100
From: "Steve Kay" <steve@peachy.com>
Subject: Re: debugging cgi scripts
Message-Id: <8tff40$8vc$1@plutonium.btinternet.com>
Use the CGI module's Carp function, so that stderr will be sent into a
browser viewable format.
Steve
<kmead@socrates.Berkeley.EDU> wrote in message
news:8tdo3p$qbj$1@agate.berkeley.edu...
> I've just found out that I'm now in charge of a bunch of
> Perl CGI scripts. While I'm quite familiar with perl scripts
> I'm less so with perl CGI scripts.
>
> My question is, what is the best way to go about debugging
> CGI scripts? Is there a better solution than just putting
> in print statements? I'd love to be able to single-step in
> a debugger through some of these scripts.
>
> Thanks,
>
> Keith
------------------------------
Date: Sat, 28 Oct 2000 20:47:36 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Detecting socket closure
Message-Id: <IXGK5.121$ap4.3924@eagle.america.net>
On Sat, 28 Oct 2000 15:55:46 GMT, Uri Guttman <uri@sysarch.com> wrote:
>
>if you have the socket in non-blocking mode and test for reading with
>select (or IO::Seelct or Event.pm, etc.) and you read 0 bytes, the
>socket is closed.
This is handy information. Thank you.
What about the following case:
The client sends a request to the server expecting to read a reply
when the request can be satisfied. That is, the client is blocking in
a <$socket>. The server cannot produce a reply immediately, but will
be able to in the future so it calls pause(). Certain events cause
the server to be signaled. It is possible that the server will then
be able to satisfy the client's request and thus write a response on
the socket that the client is blocked on.
How can the server discover that the client interrupted the blocking
read and then closed the socket (exited)? I have tried using
select(), but it always returns that the socket can_write, even when
the client closed and exited long ago.
--
Garry Williams
------------------------------
Date: Sat, 28 Oct 2000 14:12:28 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: encrypting and decrypting a string
Message-Id: <slrn8vm5oc.lt6.tadmc@magna.metronet.com>
On Sat, 28 Oct 2000 15:53:48 GMT, metamp@my-deja.com <metamp@my-deja.com> wrote:
>can anybody give me a tip how to simply encrypt a strng an derypt it
^^^^^^
>back to its original state
You asked for a simple encryption, so here's one:
-----------------------------------
#!/usr/bin/perl -w
use strict;
$_ = 'I am a secret string';
my $coded = encode($_);
print "Encoded: '$coded'\n";
my $decoded = decode($coded);
print "Decoded: '$decoded'\n";
sub encode {
my($str) = @_;
$str =~ tr/a-zA-Z/n-za-mN-ZA-M/; # rot13
return $str;
}
sub decode { encode @_ }
-----------------------------------
But that is so simple as to be nearly useless.
What kind of encryption do you require for your application?
CPAN has several modules for encrypting.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 28 Oct 2000 20:53:24 GMT
From: Dave Brondsema <brondsem@my-deja.com>
Subject: Re: func (a) func !a! func ~a~ func a .... more?
Message-Id: <8tfec1$735$1@nnrp1.deja.com>
In article <x77l6tlfzv.fsf@home.sysarch.com>,
Uri Guttman <uri@sysarch.com> wrote:
> >>>>> "DB" == Dave Brondsema <brondsem@my-deja.com> writes:
>
> DB> ok, I tried some things and looked through some perl
documentation and
> DB> you're right. What I really want to know is what can I use
instead
> DB> of / in a regexp.
>
> DB> print qq(1);
> DB> print qq~2~;
> DB> print qq!3!;
> DB> print qq/4/;
>
> those are double quoted strings and not regexes. which book or
education
> source is leading you to these false beliefs?
The title "Regexp Quote-Like Operators" from perlop
>
> uri
>
> --
> Uri Guttman --------- uri@sysarch.com ----------
http://www.sysarch.com
> SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX
Consulting
> The Perl Books Page ----------- http://www.sysarch.com/cgi-
bin/perl_books
> The Best Search Engine on the Net ----------
http://www.northernlight.com
>
--
Dave Brondsema
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 28 Oct 2000 19:50:57 GMT
From: clay@panix.com (Clay Irving)
Subject: grep vs. a subroutine to find an occurance of an element in a list
Message-Id: <slrn8vmbh1.pfi.clay@panix3.panix.com>
I'm not seeing something and it's probably right in front of my nose.
The FAQ indicates:
Please do not use
$is_there = grep $_ eq $whatever, @array;
or worse yet
$is_there = grep /$whatever/, @array;
These are slow (checks every element even if the
first matches), inefficient (same reason), and
potentially buggy (what if there are regex
characters in $whatever?). If you're only testing
once, then use:
$is_there = 0;
foreach $elt (@array) {
if ($elt eq $elt_to_find) {
$is_there = 1;
last;
}
}
if ($is_there) { ... }
But why is this subroutine much slower than grep?
#!/usr/local/bin/perl -w
use Benchmark;
open F, "foo" or die "$!\n";
@foo = <F>;
foreach (@foo) {
# don't do anything...
}
$count = 500;
timethese($count, {
'grep' => '$found = grep(/ratchet/, @foo);',
'lookfor' => 'if (lookfor("ratchet", @foo)) { $found = 1; }'
});
sub lookfor {
my $is_there = 0;
my ($string, @array) = @_;
foreach $elt (@array) {
if ($elt =~ /$string/) {
$is_there = 1;
last;
}
}
return $is_there;
}
Result:
grep: 4 wallclock secs ( 3.47 usr + 0.00 sys = 3.47 CPU) @ 144.09/s
(n=500)
lookfor: 6 wallclock secs ( 5.90 usr + 0.00 sys = 5.90 CPU) @ 84.75/s
(n=500)
--
Clay Irving <clay@panix.com>
Tact is the ability to describe others as they see themselves.
- Abraham Lincoln
------------------------------
Date: Sat, 28 Oct 2000 16:16:43 -0400
From: "Randy Harris" <harrisr@bignet.net>
Subject: Re: grep vs. a subroutine to find an occurance of an element in a list
Message-Id: <svmd0ce97is44d@corp.supernews.com>
Clay Irving <clay@panix.com> wrote in message
news:slrn8vmbh1.pfi.clay@panix3.panix.com...
> I'm not seeing something and it's probably right in front of my nose.
>
> The FAQ indicates:
>
> Please do not use
>
> $is_there = grep $_ eq $whatever, @array;
>
> or worse yet
>
> $is_there = grep /$whatever/, @array;
>
> These are slow (checks every element even if the
> first matches), inefficient (same reason), and
> potentially buggy (what if there are regex
> characters in $whatever?). If you're only testing
> once, then use:
>
> $is_there = 0;
> foreach $elt (@array) {
> if ($elt eq $elt_to_find) {
> $is_there = 1;
> last;
> }
> }
> if ($is_there) { ... }
>
> But why is this subroutine much slower than grep?
My guess is that it has a lot to do with what is in foo. If foo is very
short, you don't have the opportunity to take advantage of the last;.
If foo is a very long file, that last; could save huge amounts of time.
I suspect you have also skewed the results a bit by using a subroutine
call to lookfor. 500 calls to that routine must comsume some time that
you didn't saddle your grep call with. That could be verified rather
easily by commenting out the contents of your sub and then running the
benchmark. You would then see how much time was actually spent running
that code vs. the subroutine call.
>
> #!/usr/local/bin/perl -w
>
> use Benchmark;
>
> open F, "foo" or die "$!\n";
> @foo = <F>;
>
> foreach (@foo) {
> # don't do anything...
> }
>
> $count = 500;
>
> timethese($count, {
> 'grep' => '$found = grep(/ratchet/, @foo);',
> 'lookfor' => 'if (lookfor("ratchet", @foo)) { $found = 1; }'
> });
>
> sub lookfor {
>
> my $is_there = 0;
> my ($string, @array) = @_;
>
> foreach $elt (@array) {
> if ($elt =~ /$string/) {
> $is_there = 1;
> last;
> }
> }
> return $is_there;
> }
>
> Result:
>
> grep: 4 wallclock secs ( 3.47 usr + 0.00 sys = 3.47 CPU) @
144.09/s
> (n=500)
> lookfor: 6 wallclock secs ( 5.90 usr + 0.00 sys = 5.90 CPU) @
84.75/s
> (n=500)
>
> --
> Clay Irving <clay@panix.com>
> Tact is the ability to describe others as they see themselves.
> - Abraham Lincoln
------------------------------
Date: 28 Oct 2000 21:16:15 GMT
From: clay@panix.com (Clay Irving)
Subject: Re: grep vs. a subroutine to find an occurance of an element in a list
Message-Id: <slrn8vmggu.rp9.clay@panix3.panix.com>
On Sat, 28 Oct 2000 16:16:43 -0400, Randy Harris <harrisr@bignet.net> wrote:
>Clay Irving <clay@panix.com> wrote in message
>news:slrn8vmbh1.pfi.clay@panix3.panix.com...
>> I'm not seeing something and it's probably right in front of my nose.
>>
>> The FAQ indicates:
>>
>> Please do not use
>>
>> $is_there = grep $_ eq $whatever, @array;
>>
>> or worse yet
>>
>> $is_there = grep /$whatever/, @array;
>>
>> These are slow (checks every element even if the
>> first matches), inefficient (same reason), and
>> potentially buggy (what if there are regex
>> characters in $whatever?). If you're only testing
>> once, then use:
>>
>> $is_there = 0;
>> foreach $elt (@array) {
>> if ($elt eq $elt_to_find) {
>> $is_there = 1;
>> last;
>> }
>> }
>> if ($is_there) { ... }
>>
>> But why is this subroutine much slower than grep?
>
>My guess is that it has a lot to do with what is in foo. If foo is very
>short, you don't have the opportunity to take advantage of the last;.
>If foo is a very long file, that last; could save huge amounts of time.
>
>I suspect you have also skewed the results a bit by using a subroutine
>call to lookfor. 500 calls to that routine must comsume some time that
>you didn't saddle your grep call with. That could be verified rather
>easily by commenting out the contents of your sub and then running the
>benchmark. You would then see how much time was actually spent running
>that code vs. the subroutine call.
Ok, this is another test:
$count = 500;
$number = 4055578;
timethese($count, {
'grep' => 'if (! grep(/$number/, @foo)) { $count6++; } ',
'lookfor' => 'if (! lookfor($number, @foo)) { $count6++; }'
});
I changed the subroutine to:
sub lookfor {
my $is_there = 0;
my ($num, @array) = @_;
foreach $elt (@array) {
if ($elt eq $num) {
$is_there = 1;
last;
}
}
return $is_there;
}
@foo contains 3,395 7 digit numbers. The result is:
Benchmark: timing 500 iterations of grep, lookfor...
grep: 6 wallclock secs ( 6.39 usr + 0.00 sys = 6.39 CPU) @ 78.25/s
(n=500)
lookfor: 10 wallclock secs (10.26 usr + 0.00 sys = 10.26 CPU) @ 48.73/s
(n=500)
I commented out the contents of the subroutine and reran the program. The
result is:
Benchmark: timing 500 iterations of grep, lookfor...
grep: 6 wallclock secs ( 6.32 usr + 0.00 sys = 6.32 CPU) @ 79.11/s
(n=500)
lookfor: 1 wallclock secs ( 0.19 usr + 0.00 sys = 0.19 CPU) @ 2631.58/s
(n=500)
(warning: too few iterations for a reliable count)
grep still wins.
--
Clay Irving <clay@panix.com>
Everyone complains of his memory, but no one complains of his judgement.
- Duc De La Rochefoucauld
------------------------------
Date: 28 Oct 2000 20:56:00 GMT
From: daniel@chetlin.com (Daniel Chetlin)
Subject: Re: grep vs. a subroutine to find an occurance of an element in a list
Message-Id: <8tfeh001bb1@news2.newsguy.com>
On 28 Oct 2000 19:50:57 GMT, Clay Irving <clay@panix.com> wrote:
>I'm not seeing something and it's probably right in front of my nose.
[snip quote from FAQ]
>But why is this subroutine much slower than grep?
[snip some code]
> timethese($count, {
> 'grep' => '$found = grep(/ratchet/, @foo);',
> 'lookfor' => 'if (lookfor("ratchet", @foo)) { $found = 1; }'
^^^^
> sub lookfor {
>
> my $is_there = 0;
> my ($string, @array) = @_;
^^^^^^
>
> foreach $elt (@array) {
> if ($elt =~ /$string/) {
> $is_there = 1;
> last;
> }
> }
> return $is_there;
> }
Every time you call lookfor(), you're pushing every element of @foo onto
the stack, and then having the sub pull each element off and construct
its own copy of the array.
I changed your code to pass a ref to the array (and made some minor
cleanups), used /usr/dict/words as the initial file, and looked for
`rat' instead of `ratchet', and lookfor literally blows the doors off of
grep. Obviously, results will be different when you have a different
sized list and a word that doesn't appear so early, but I think you can
see what the FAQ is talking about now.
#!/usr/local/bin/perl -w
use Benchmark;
open F, "/usr/dict/words" or die "$!\n";
@foo = <F>;
$count = -5;
timethese($count, {
'grep' => sub { $found = grep(/rat/, @foo) },
'lookfor' => sub { if (lookfor("rat", \@foo)) { $found = 1 } }
});
sub lookfor {
my $is_there = 0;
my ($string, $array) = @_;
/$string/ && return 1 for (@$array);
return 0;
}
---===---
[~] $ perl -w dev/test/grep.pl
Benchmark: running grep, lookfor, each for at least 5 CPU seconds...
grep: 5 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU)
@ 27.24/s (n=143)
lookfor: 6 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU)
@ 15694.55/s (n=83495)
-dlc
------------------------------
Date: 28 Oct 2000 22:02:10 GMT
From: clay@panix.com (Clay Irving)
Subject: Re: grep vs. a subroutine to find an occurance of an element in a list
Message-Id: <slrn8vmj72.29k.clay@panix3.panix.com>
On 28 Oct 2000 20:56:00 GMT, Daniel Chetlin <daniel@chetlin.com> wrote:
>On 28 Oct 2000 19:50:57 GMT, Clay Irving <clay@panix.com> wrote:
>>I'm not seeing something and it's probably right in front of my nose.
>[snip quote from FAQ]
>>But why is this subroutine much slower than grep?
>[snip some code]
>> timethese($count, {
>> 'grep' => '$found = grep(/ratchet/, @foo);',
>> 'lookfor' => 'if (lookfor("ratchet", @foo)) { $found = 1; }'
> ^^^^
>> sub lookfor {
>>
>> my $is_there = 0;
>> my ($string, @array) = @_;
> ^^^^^^
>>
>> foreach $elt (@array) {
>> if ($elt =~ /$string/) {
>> $is_there = 1;
>> last;
>> }
>> }
>> return $is_there;
>> }
>
>Every time you call lookfor(), you're pushing every element of @foo onto
>the stack, and then having the sub pull each element off and construct
>its own copy of the array.
>
>I changed your code to pass a ref to the array (and made some minor
>cleanups), used /usr/dict/words as the initial file, and looked for
>`rat' instead of `ratchet', and lookfor literally blows the doors off of
>grep. Obviously, results will be different when you have a different
>sized list and a word that doesn't appear so early, but I think you can
>see what the FAQ is talking about now.
>
> #!/usr/local/bin/perl -w
> use Benchmark;
>
> open F, "/usr/dict/words" or die "$!\n";
> @foo = <F>;
>
> $count = -5;
>
> timethese($count, {
> 'grep' => sub { $found = grep(/rat/, @foo) },
> 'lookfor' => sub { if (lookfor("rat", \@foo)) { $found = 1 } }
> });
>
> sub lookfor {
> my $is_there = 0;
> my ($string, $array) = @_;
> /$string/ && return 1 for (@$array);
> return 0;
> }
>
>---===---
>
> [~] $ perl -w dev/test/grep.pl
> Benchmark: running grep, lookfor, each for at least 5 CPU seconds...
> grep: 5 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU)
> @ 27.24/s (n=143)
> lookfor: 6 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU)
> @ 15694.55/s (n=83495)
>
>-dlc
That's it.
In another test with 2,393 7 digit numbers in an array the result was:
Benchmark: timing 500 iterations of grep, lookfor...
grep: 6 wallclock secs ( 6.34 usr + 0.00 sys = 6.34 CPU) @ 78.86/s
(n=500)
lookfor: 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU) @ 25000.00/s
(n=500)
(warning: too few iterations for a reliable count)
Thanks, Daniel.
--
Clay Irving <clay@panix.com>
Contraceptives: To be used on all conceivable occasions.
------------------------------
Date: Sat, 28 Oct 2000 18:21:10 GMT
From: espenei@my-deja.com
Subject: Help with regex
Message-Id: <8tf5eh$8o$1@nnrp1.deja.com>
I am a perl rookie and hope someone can help me with my small problem.
I have a number of URL's that I would like to filter using regex.
Here is an example:
URL 1: http://www.perl.com
URL 2: http://www.microsoft.com
URL 3: http://www.sun.com
URL 4: http://www.apple.com
URL 5: http://www.hp.com
How can I, using one general regex expression match all URL's except
for one, for example http://www.hp.com.
Is there some kind of 'not' command that I can use?
Thanks
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 28 Oct 2000 14:45:41 -0500
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: Help with regex
Message-Id: <m3r950px5m.fsf@dhcp11-177.support.tivoli.com>
espenei@my-deja.com writes:
> I am a perl rookie and hope someone can help me with my small problem.
> I have a number of URL's that I would like to filter using regex.
>
> Here is an example:
>
> URL 1: http://www.perl.com
> URL 2: http://www.microsoft.com
> URL 3: http://www.sun.com
> URL 4: http://www.apple.com
> URL 5: http://www.hp.com
>
> How can I, using one general regex expression match all URL's except
> for one, for example http://www.hp.com.
>
> Is there some kind of 'not' command that I can use?
Well, you can use !~ in place of =~ to indicate a negated match. Or,
you can use a negative look-ahead within the regex:
m{^http://www\.(?!hp)\w+.com$}
Obviously, this isn't a very generic URL pattern -- lots of URLs will
fail to match. Still, it should give you the basic idea. Depending
on your specific need, negating the result of the match (either via !~
or by various other means) would probably be better.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Sat, 28 Oct 2000 15:25:10 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Help with regex
Message-Id: <slrn8vma0m.mao.tadmc@magna.metronet.com>
On Sat, 28 Oct 2000 18:21:10 GMT, espenei@my-deja.com
<espenei@my-deja.com> wrote:
>I am a perl rookie and hope someone can help me with my small problem.
>I have a number of URL's that I would like to filter using regex.
>
>Here is an example:
>
>URL 1: http://www.perl.com
>URL 2: http://www.microsoft.com
>URL 3: http://www.sun.com
>URL 4: http://www.apple.com
>URL 5: http://www.hp.com
>
>How can I, using one general regex expression match all URL's except
>for one, for example http://www.hp.com.
I think you are approaching the problem wrong.
I suggest using a regex to match all URLs, the filtering
the resulting list with Perl's grep() instead of trying
to get it done with a regex (because that could easily
become a maintenance nightmare).
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 28 Oct 2000 17:30:45 -0400
From: "Bill" <Bill@net.com>
Subject: How to call a perl script from HTML
Message-Id: <39fb44e0_1@news.newsfeeds.com>
Can anyone tell me how to call a Perl script from an HTML page other than by
a FORM?
I'd like to replace a URL in a link with a Perl script.
Thanks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
------------------------------
Date: 28 Oct 2000 21:47:33 GMT
From: kevin metcalf <xzrgpnys@yvtugubhfrovm.pbz>
Subject: Re: How to call a perl script from HTML
Message-Id: <39FB49FA.EEEDEB4E@yvtugubhfrovm.pbz>
Bill wrote:
> Can anyone tell me how to call a Perl script from an HTML page other than by
> a FORM?
> I'd like to replace a URL in a link with a Perl script.
You mean like:
<a
href="/cgi-bin/myfile.pl?firstvalue=something&secondvalue=something">Execute
the script!</a>
Is that what you wanted? A little more info might get you a better
response.
Kevin
--
email: xzrgpnys@yvtugubhfrovm.pbz
Huh? http://www.flactem.com/utils/rot13.html
------------------------------
Date: Sat, 28 Oct 2000 21:27:11 GMT
From: mr_potato_head@my-deja.com
Subject: how to find oldest file in a directory
Message-Id: <8tfgb9$8j8$1@nnrp1.deja.com>
HI,
I have a script using the find module looking for files that have
today's date in the filename. Out of the list returned, how can I find
out which file is the oldest? (most current file) thanks in
advance...
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 28 Oct 2000 21:52:18 GMT
From: clay@panix.com (Clay Irving)
Subject: Re: how to find oldest file in a directory
Message-Id: <slrn8vmiki.1qb.clay@panix3.panix.com>
On Sat, 28 Oct 2000 21:27:11 GMT, mr_potato_head@my-deja.com
<mr_potato_head@my-deja.com> wrote:
> I have a script using the find module looking for files that have
>today's date in the filename. Out of the list returned, how can I find
>out which file is the oldest? (most current file) thanks in
>advance...
The stat function provides last modify time in seconds since the epoch.
Once you have a list of files, use the stat function to determine the
oldest file.
--
Clay Irving <clay@panix.com>
It is forbidden to kill; therefore all murderers are punished unless they
kill in large numbers and to the sound of trumpets.
- Voltaire
------------------------------
Date: Sat, 28 Oct 2000 20:54:25 GMT
From: 7brankin9@my-deja.com
Subject: How to loop in the background
Message-Id: <8tfedu$73q$1@nnrp1.deja.com>
Hello,
I need to launch a series of commands that will start again if they
die, i.e.:
for $thisarg (@that) {
system (myapp $thisarg); #launch the command, in a separate
looping process that will loop if the app dies
}
print "scripts successfully launched\n";
So: I need to wrap "system" in some sort of Perl loop (a for
statement?) that goes into the background...any ideas greatly
appreciated.
Thanks --Brian
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Sat, 28 Oct 2000 18:05:07 GMT
From: "What A Man !" <whataman@home.com>
Subject: Re: Installing HTML module
Message-Id: <39FB15A4.16A87A41@home.com>
0149@my-deja.com wrote:
>
> I downloaded the HTML module 0.6 from cpan.org. How do I install it?
> (There is no Makefile.PL)
>
> Thanks in advance
I hope someone answers this because I'd like to know too.
I tried to install Mirror and there was no Makefile.PL in
that one either, just a makefile. So how do we install a
module without a Makefile.PL? Anyone know?
Kind Regards,
Dennis
------------------------------
Date: Sat, 28 Oct 2000 14:35:11 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: No such file or directory (NT)
Message-Id: <slrn8vm72v.lt6.tadmc@magna.metronet.com>
On Sat, 28 Oct 2000 15:24:43 GMT, Mario <diab.lito@usa.net> wrote:
>I wrote a script using flat files.It was running properly on a Unix
>server.Now I had to install it on an NT machine.
A form of "reverse progress".
>It gives me the "No such file or directory" error even if the files are
>in the same directory of the script opening it.
Where the script is does not matter.
What your current working directory is is what matters,
if you are going to use relative paths to the files.
>Is the server working bad or does NT requires something different to
^^^^^^
>specify the path?
If you are using Perl apart from its usual environment, you should
state that, as the environment may be (and is, for your current problem)
the cause of your problem.
From your use of "server" I assume you're using Perl in a CGI environment?
Most Perl runs from the command line, you should mention your
application if it is different from that.
Perl is not CGI.
No the server and your "OS" are most likely working fine.
What is also working fine, though not working as you might expect,
is that the current directory for CGI scripts is not specified
in the CGI spec, so it can be anywhere.
It appears that the Unix server was setup to have the cwd be
the same dir that the script was in, and the NT server was
setup some other way.
So, you don't have a Perl question at all. You have a server
setup question. Please ask those in a server newsgroup in
the future.
comp.infosystems.www.servers.ms-windows
comp.infosystems.www.servers.unix
To solve your problem, either use absolute rather than relative paths,
or chdir() to the right directory before using the relative paths.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 28 Oct 2000 15:03:42 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: okay i tried ftp but... i dont get it.
Message-Id: <slrn8vm8oe.lt6.tadmc@magna.metronet.com>
On Sat, 28 Oct 2000 13:09:41 GMT, ronald <yilias@hotmail.com> wrote:
>I found the site about het net::ftp module,
Might you mean the Net::FTP module instead?
Case is significant.
>but... where can i get it,
The Comprehensive Perl Archive Network (CPAN).
perldoc -q CPAN
"What modules and extensions are available for Perl?
What is CPAN? What does CPAN/src/... mean?"
The answer to that Perl FAQ tells how to get stuff from CPAN.
>how
>do i install it
perldoc -q install
"How do I install a module from CPAN?"
>and how do i huse it.
After it is installed:
perldoc Net::FTP
>There arent any tutorials about this subject
^^^^^^^^^^^^
[ Which "this" subject? Tutorials about how to install modules,
or tutorials about the Net::FTP module?
]
That seems an overly bold statement.
Just because you haven't found one does not imply that one
does not exist...
:-)
But the fact that you have not found one does seem to imply that
you didn't look very hard...
Searching for "Net::FTP tutorial" (with the quotes) at google.com
finds 11 hits.
Of course, you need to spell the name of the module correctly
for that to work :-)
Searching for "install Perl module" finds 33 hits...
Searching for
"Perl module" tutorial
finds over 100 hits.
>so i hope someone here could
>help me.
I think the answers are already on your hard drive.
Easily found, once you know to use "CPAN" as a search term.
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 28 Oct 2000 15:07:58 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: perl/cgi programmer in Atlanta
Message-Id: <slrn8vm90e.lt6.tadmc@magna.metronet.com>
[ This is not a job newsgroup.
Advertisements are not welcome here.
Job newsgroups have "jobs" in the newsgroup name.
Please stop abusing Usenet with off-topic posts.
It will cause the most qualified folks to discount you
as too clueless to work with.
]
On Sat, 28 Oct 2000 15:55:26 GMT, Kermit Lowry, III <ultrak@my-deja.com> wrote:
> looking for a perl/cgi programmer with 2+yrs with cgi on Win & Linux.
>$25-30/hr contract
That is an awfully low rate. You are likely to get what you pay for.
A (1998?) salary survey showed that 66% of Perl contract programmers
make $50-90 per hour...
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 28 Oct 2000 22:21:07 +0100
From: Geoff Soper <g.soper@soundhouse.co.uk>
Subject: Sending mails from a Unix webserver
Message-Id: <4a14bfb046g.soper@soundhouse.co.uk>
I'm starting to think about building a mailing list system. I intend it to
be a CGI application running on a Unix webserver running Apache. I see the
part of the FAQ entitled 'How do I send mail?'. This seems simple enough,
is it a good way to send this sort of quantity (<1000) of message? The
messages will all be the same and will be sent to a certain subset of a
larger number of addresses.
Also it says the -odq part means the message get put into the queue as
opposed to being delivered directly, what is this queue?
Cheers
--
Geoff Soper
g.soper@soundhouse.co.uk
Take a look at the Soundhouse page http://www.soundhouse.co.uk/
------------------------------
Date: 28 Oct 2000 21:54:17 GMT
From: clay@panix.com (Clay Irving)
Subject: Re: Sending mails from a Unix webserver
Message-Id: <slrn8vmio9.1qb.clay@panix3.panix.com>
On Sat, 28 Oct 2000 22:21:07 +0100, Geoff Soper
<g.soper@soundhouse.co.uk> wrote:
>I'm starting to think about building a mailing list system. I intend it to
>be a CGI application running on a Unix webserver running Apache. I see the
>part of the FAQ entitled 'How do I send mail?'. This seems simple enough,
>is it a good way to send this sort of quantity (<1000) of message? The
>messages will all be the same and will be sent to a certain subset of a
>larger number of addresses.
>Also it says the -odq part means the message get put into the queue as
>opposed to being delivered directly, what is this queue?
Why don't you use majordomo instead of building a mailing list system with
Perl?
--
Clay Irving <clay@panix.com>
Civilization is just a temporary failure of entropy.
- Christine Nelson
------------------------------
Date: Sat, 28 Oct 2000 21:03:27 GMT
From: peter pilsl <pilsl@goldfisch.atat.at>
Subject: variable in module
Message-Id: <MPG.14655e4f4cba9c709898fa@news.chello.at>
Ok, when building my first module I run into a phenomen I simply dont
understand.
package test;
my $var='bye';
sub a1 {
...
$line='$var=\'hallo\';
eval $line;
...
}
sub a2 { print $var }
The script loads the module, calls a1 and then a2 and the result is
'bye' !!
I expected to get 'hallo' like set in sub a1.
But now the weird part: When using $var in sub a1 again (like a simple
print $var) I get the correct value when calling a2 later. I also get the
correct value when assigning outside the eval-block:
$var='hallo';
, but I need to parse a controlfile. The problem can also be solved when
using 'our' instead of 'my' when declaring $var, but I would prefer to
make $var really private.
Could please anyone give me a hint what namespace-rule makes a fool out of
me here ;)
thanx,
peter
--
pilsl@
goldfisch.at
------------------------------
Date: Sun, 29 Oct 2000 08:50:44 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: variable in module
Message-Id: <slrn8vmihk.ac5.mgjv@martien.heliotrope.home>
On Sat, 28 Oct 2000 21:03:27 GMT,
peter pilsl <pilsl@goldfisch.atat.at> wrote:
>
> Ok, when building my first module I run into a phenomen I simply dont
> understand.
>
> package test;
>
> my $var='bye';
>
> sub a1 {
> ...
> $line='$var=\'hallo\';
> eval $line;
> ...
> }
>
> sub a2 { print $var }
>
>
> The script loads the module, calls a1 and then a2 and the result is
> 'bye' !!
No, the script does not load this module, because perl will refuse to
compile the above pseudo-code. Please, and especially when you are
talking about problems like these, make sure you submit _working_ code.
At least compileable.
> I expected to get 'hallo' like set in sub a1.
Ok, I'll fix up your code to compile, run under strict, and then run it:
#!/usr/local/bin/perl -w
use strict;
package Foo;
my $var='bye';
sub a1
{
my $line='$var="hallo"';
eval $line;
}
sub a2 { print $var }
package main;
Foo::a1();
Foo::a2();
This spits out 'hallo'. If I leave out all the packages, same thing
happens. Since you don't tell us how exactly you do all this, I had to
do both.
Now, what I want you to do, instead of just wildly typing in some stuff,
You go off, write a short piece of code that compiles, runs under strict
with the -w flag, and demonstrates the problem. If you haven't fixed it
yourself by then, post that here.
> Could please anyone give me a hint what namespace-rule makes a fool out of
> me here ;)
None. Unless you are doing something other than what you are telling us.
evals can invoke local scoping, but I don't think in this case.
And please, for next time: Real code only.
Martien
--
Martien Verbruggen |
Interactive Media Division | This matter is best disposed of from
Commercial Dynamics Pty. Ltd. | a great height, over water.
NSW, Australia |
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 4751
**************************************