[9267] in Perl-Users-Digest
Perl-Users Digest, Issue: 2862 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 12 23:07:45 1998
Date: Fri, 12 Jun 98 20:00:25 -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 Fri, 12 Jun 1998 Volume: 8 Number: 2862
Today's topics:
Re: A Simple Question! <tchrist@mox.perl.com>
BOOK REVIEWS updated <tchrist@mox.perl.com>
Re: Certified Perl Programmers (Mark-Jason Dominus)
CGI/Perl Error <warchie@att.net.hk>
Re: DBM concurrent writing (James Martino)
Re: diff-like utility in Perl? <ljz@asfast.com>
Re: diff-like utility in Perl? (Michael J Gebis)
Re: Have we got a good free Perl manual? <ngouah@erols.com>
Re: Have we got a good free Perl manual? (Christopher Browne)
Re: Have we got a good free Perl manual? (Christopher Browne)
Re: Have we got a good free Perl manual? (Christopher Browne)
Re: Have we got a good free Perl manual? <rra@stanford.edu>
Help a newbie with arrays! (I think ... ) (Ryan Baghdad)
Re: Is this insane? <sdh1@anchor.hotmail.com>
Re: New module/pragma "enum.pm" (was "fields.pm") <zenin@bawdycaste.org>
Re: New module/pragma "enum.pm" (was "fields.pm") <ljz@asfast.com>
Re: new to this - simple if statement question <tchrist@mox.perl.com>
Re: new to this - simple if statement question (Mark-Jason Dominus)
perl script emulates "last" command <wschow@Comp.HKBU.Edu.HK>
Re: Prob in FAQ: Week of the year (Abigail)
Re: Puzzle challenge <igor.k@usa.net>
REVIEW: Perl CGI Programming - No Experience Required <tchrist@mox.perl.com>
Re: REVIEW: Perl CGI Programming - No Experience Requir (Rahul Dhesi)
Two-way sockets (William Burrow)
unencoding email attachments with perl?? (-)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 13 Jun 1998 00:01:20 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: A Simple Question!
Message-Id: <6lsfgg$806$2@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Gellyfish@btinternet.com (Jonathan Stowe) writes:
:There is no real need to spawn the external command
Perl is supposed to be a glue language you know -
a language for getting your job done. Don't fear
the backticks, my son.
--tom
--
"Just because you're screwed *up* doesn't mean you're screwed." --Larry Wall
------------------------------
Date: 13 Jun 1998 02:22:51 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: BOOK REVIEWS updated
Message-Id: <6lsnpr$nq7$1@csnews.cs.colorado.edu>
Updates at
http://www.perl.com/perl/critiques/index.html
There are many good, new books out there. There are of course
many more bad ones, but I didn't review most of those. :-)
--tom
--
Holism permits reductionism, but reductionism does not reciprocate.
--Larry Wall in <1994Nov10.185030.16615@netlabs.com>
------------------------------
Date: 12 Jun 1998 21:20:52 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Certified Perl Programmers
Message-Id: <6lsk5k$hba$1@monet.op.net>
In article <1998061223230300.TAA21653@ladder01.news.aol.com>,
Bbirthisel <bbirthisel@aol.com> wrote:
>You should make it downloadable from CPAN and indicate where to find
>it in the FAQ. That would establish a suitable skill set - since anyone who
>couldn't do that probably shouldn't have one.
But I want everyone to have one!
------------------------------
Date: Fri, 12 Jun 1998 23:41:44 +0800
From: Walter Archie <warchie@att.net.hk>
Subject: CGI/Perl Error
Message-Id: <35814C37.FCD9E4C3@att.net.hk>
If anyone does actually read this message please take the time to help
me.
Goto: http://www.thealienz.com/cgi-bin/board/board.cgi
Please can someone help me understand these errors of my script. If
someone requires my script please let me know.
thank you,
Justin Archie
------------------------------
Date: 11 Jun 1998 13:27:38 GMT
From: jrm@chow.mat.jhu.edu (James Martino)
Subject: Re: DBM concurrent writing
Message-Id: <6lom0a$jil@news.jhu.edu>
I have used a file locking scheme derived from the method in the
passwd script in Programming Perl (1e), pp 286-302. The application
was for writing to databases through a CGI script, so stopping
concurrent writes was an issue.
Jim
In article <6ld5ki$kql$1@halcyon.com> blm@halcyon.com (Brian L. Matthews) writes:
>In article <3573B79A.428175F8@spanishbanner.com>,
>webmaster <webmaster@spanishbanner.com> wrote:
>|I had tryed to lock the file by using flock, but DBM changes on
>|differents Perl versions an I think is not an orthodox method to do it
>
>It is somewhat hackish, but if you specify a set of DBM routines to use
>(instead of doing a dbmopen or using AnyDBM_File), the file or files used
>shouldn't change, so you can flock one of the underlying files. This is
>what I use, although I'd be happy to hear suggestions of other ways to
>lock a DBM.
>
>Brian
--
James R. Martino * VOICE: (410) 516-7881
Director of Undergraduate Studies in Mathematics * FAX: (410) 516-5549
Department of Mathematics * EMAIL:James.Martino@jhu.edu
The Johns Hopkins University *
------------------------------
Date: 12 Jun 1998 21:24:53 -0400
From: Lloyd Zusman <ljz@asfast.com>
Subject: Re: diff-like utility in Perl?
Message-Id: <lt7m2mayai.fsf@asfast.com>
Tom Christiansen <tchrist@mox.perl.com> writes:
> [courtesy cc of this posting sent to cited author via email]
>
> In comp.lang.perl.misc,
> Lloyd Zusman <ljz@asfast.com> writes:
> :I know of no such Perl-based `diff', but perhaps someone out there has
> :invented such a thing without having posted it to CPAN.
>
> Is there a Perl-based netstat? Or ps? Or vi?
Probably not, but there is a Perl-based `find', a Perl-based `grep', a
Perl-based `tail', a Perl-based `whois', a Perl-based `lex', a
Perl-based `telnet', a Perl-based `ftp', a Perl-based `mv', a
Perl-based `cp', a Perl-based `rm' ... etc. ...
Therefore, it's perfectly reasonable for someone to wonder if perhaps
there might also be a Perl-based `diff' out there somewhere.
> Sigh.
--
Lloyd Zusman ljz@asfast.com
perl -e '$n=170;for($d=2;($d*$d)<=$n;$d+=(1+($d%2))){for($t=0;($n%$d)==0;
$t++){$n=int($n/$d);}while($t-->0){push(@r,$d);}}if($n>1){push(@r,$n);}
$x=0;map{$x+=(($_>0)?(1<<log($_-0.5)/log(2.0)+1):1)}@r;print"$x\n"'
------------------------------
Date: 13 Jun 1998 01:56:54 GMT
From: gebis@albrecht.ecn.purdue.edu (Michael J Gebis)
Subject: Re: diff-like utility in Perl?
Message-Id: <6lsm96$q2g@mozo.cc.purdue.edu>
Lloyd Zusman <ljz@asfast.com> writes:
}Probably not, but there is a Perl-based `find', a Perl-based `grep', a
}Perl-based `tail', a Perl-based `whois', a Perl-based `lex', a
}Perl-based `telnet', a Perl-based `ftp', a Perl-based `mv', a
}Perl-based `cp', a Perl-based `rm' ... etc. ...
}Therefore, it's perfectly reasonable for someone to wonder if perhaps
}there might also be a Perl-based `diff' out there somewhere.
I can think of plenty of reasons to want a perl-based diff.
Have you seen the source code to gnu diff? First of all, it's in C,
which I don't mind, can cause trauma in those who aren't used to it.
Second of all, it's #ifdefed from here to eternity, in search of
portablitity. It's not exactly clear what's going on.
If I wanted to understand the algorithm used by diff, or wanted to
extend diff some way, I sure as hell would look for a version
written in perl. (For the same reasons I would want to use perl if
I had to write diff from scratch, in fact. Leverage, baby,
leverage.)
AND I THINK THAT THE READERS OF THIS NEWSGROUP OWE ME SUCH A VERSION
OF DIFF, AND I WANT IT RIGHT NOW. (That's not _really_ how I feel, of
course. Nobody owes me nuttin'. I'm just trying to point out that
there are valid reasons to desire such a thing. I'd also like a Dodge
Viper. It sure would be cool. I don't NEED or expect either.
But either from laziness or malice, there's a chance someone will
misunderstand me and think I'm making demands. I figured I might as well
give them some text to quote out of context, too.)
--
Mike Gebis gebis@ecn.purdue.edu mgebis@eternal.net
------------------------------
Date: Fri, 12 Jun 1998 22:06:57 -0400
From: Ngouah A Nguiamba <ngouah@erols.com>
Subject: Re: Have we got a good free Perl manual?
Message-Id: <3581DEC1.3A21@erols.com>
Todd Lehman wrote:
>
> rms@gnu.org (Richard Stallman) writes:
> > Once upon a time, I thought I would learn Perl. I got a copy of a
> > free manual, but I found it simply unreadable, and gave up. Perl
> > users told me that there were better manuals, but they were not free.
>
> It seems odd to deny oneself the pleasure of learning a new language for
> several years simply because someone tells you that the best documentation
> isn't freely available.
>
It's not odd, it's cheap and I can't believe that this thread is starting
all over again.
If you can't invest in a book to learn from, stay dumb.
> Was the decision a personal monetary issue or a moral issue?
None of the above, just a way to start this thread all over again.
Why ? We had this already.
>
> For serious work, a Perl library of about 10 good titles costs around $300.
> But you can get started for a small investment of $30 or $40, depending on
> which book you choose. Alternatively, perhaps a friend or relative could buy
> you _Programming_Perl_ or _Learning_Perl_ as a gift.
I know hundreds of real poor people from poor countries who would
gladly buy a book if they could get it. And they make in a month not more
than the cost of two O'Reilly books. Get lost counting the dollars. This whole
reasoning stinks and is fake.
My mother in law raised ten kids on a $90.00/month
budget and was always able to get her kids the books they needed.
She wouldn't have cared if the manual was free in the FSF sense, T.C.'s
sense or free in the no-cost sense. She would though care, if the darn
book or manual her kids had to learn from were high quality or not and
up to date.
And so do I. Everything I find, the manual distributed with Perl, the FAQ
and the O'Reilly books, CPAN, TPJ is all high quality and no- or low cost.
Why isn't that good enough ? Why can't you stop bickering about this ?
What kind of community are you, after all ?
Arrgh!
I vote for c.l.p.moderated to not get upset again about this.
I post this in the name of my mother in law, who was illiterate but
had a courageous mind and knew when to talk and when to shut up.
Birgitt Funk
------------------------------
Date: 13 Jun 1998 01:54:58 GMT
From: cbbrowne@news.hex.net (Christopher Browne)
Subject: Re: Have we got a good free Perl manual?
Message-Id: <6lsm5i$14p$25@blue.hex.net>
On 12 Jun 1998 15:10:35 GMT, Chris Howard <choward@intellistor.com> wrote:
>David Kastrup <dak@mailhost.neuroinformatik.ruhr-uni-bochum.de> writes:
>>lehman@visi.com (Todd Lehman) writes:
>>> rms@gnu.org (Richard Stallman) writes:
>>> > Once upon a time, I thought I would learn Perl. I got a copy of a
>>> > free manual, but I found it simply unreadable, and gave up. Perl
>>> > users told me that there were better manuals, but they were not free.
>I'm sorry, I just can't work up much ire for O'Reilly and Assoc.
>Twelve years ago I started buying and reading their nutshell
>handbooks. At the time, they were about the only source of
>useful information apart from vendor manuals. They were particularly
>good at comparing different versions of similar software/commands
>on various Unix systems. They've helped me tremendously. Many times.
>Where would we be without their books on Sendmail, DNS, UUCP?
Certainly the O'Reilly books are *useful.*
They have, furthermore, had value in providing "respectable"
documentation for both proprietary and "libre" software systems.
This has even been useful in allowing extended adoption of "libre"
software.
For instance, I have successfully used the commercial availability of
the book "Programming Perl" as an argument for the adoption of Perl for
use in environments where they were worried about it being some sort of
"scary" package that "was built by hackers on the Internet."
>On the philosophical side: Isn't all *good* documentation
>written from a stand of knowledge and authority? Doesn't
>that mean that you don't want every little dofus making
>changes to the docs? So *good* documentation is
>==NOT FREE== in the FSF sense.
Indeed it is important to have some degree of control over one's own
works.
If I write the definitive reference on the Frobozz system, it is not
appropriate for every person out there to be able to make modifications
to my "literary work."
There's little disagreement with the notion that directly applying the
GPL to written "literary" works is not necessarily appropriate. The
original owner has some degree of "rights" to have their works presented
as written.
Note that Donald Knuth has licensed Stanford Graphbase code such that
you can use it, and even use and (re)distribute "patch" files to change
the deployed behaviour; those "patches" are nonetheless required to
remain separate from Knuth's code. That functions in much the way I'd
expect "free documentation" to work.
It would be valuable to have decent documentation available for free to
go along with the free software. It is not unreasonable for the
"permissible transformations" of the documentation to somewhat differ
from the "permissible transformations" of the software.
--
"All language designers are arrogant. Goes with the territory..."
(By Larry Wall)
cbbrowne@hex.net - <http://www.hex.net/~cbbrowne/lsf.html>
------------------------------
Date: 13 Jun 1998 01:55:10 GMT
From: cbbrowne@news.hex.net (Christopher Browne)
Subject: Re: Have we got a good free Perl manual?
Message-Id: <6lsm5u$14p$30@blue.hex.net>
On 12 Jun 1998 22:29:27 GMT, Paul David Fardy <pdf@morgan.ucs.mun.ca>
wrote:
>Odd... Why don't they need "a good free manual for TeX"?
An interesting question.
Until the recent proliferation of "Yet Another Thick Book About The
Internet for Thick People", there was *considerably* more literature out
there on the TeX family of document processors than there was for Perl.
Subtract the "Thick Books for Thick People" entrants, and the Perl
literature set is probably smaller than the TeX literature set.
Mind you, Knuth *does* distribute the TeXBook in TeXable form as an
example of a TeX work of significant size and complexity, albeit with
certain restrictions...
--
"All language designers are arrogant. Goes with the territory..."
(By Larry Wall)
cbbrowne@hex.net - <http://www.hex.net/~cbbrowne/lsf.html>
------------------------------
Date: 13 Jun 1998 01:55:04 GMT
From: cbbrowne@news.hex.net (Christopher Browne)
Subject: Re: Have we got a good free Perl manual?
Message-Id: <6lsm5o$14p$28@blue.hex.net>
On 12 Jun 1998 23:36:33 +0200, David Kastrup
<dak@mailhost.neuroinformatik.ruhr-uni-bochum.de> wrote:
>lehman@visi.com (Todd Lehman) writes:
>> [dak:]
>> > This is nonsense. It is a loss to the community period. If they were
>> > under some cosmic obligation, the community would not encounter any
>> > loss because it could sue them for providing the stuff. As they are
>> > not under an obligation, it is a loss to the community for good.
>>
>> Aren't you confusing loss with non-gain? Was the documentation sold
>> to O'Reilly once freely available? If so, then you could consider it a
>> loss. If not, then to call it a loss is to presume that it would have
>> and should have been contributed to the community all along, and that is
>> up to the authors and -no one- else.
>
>No, that it *could* have been contributed to the community. Once you
>have signed over your marketing rights exclusively to some publisher,
>this option is gone.
The unstated assumption is that the process of writing the book would
have been funded had there *not* been a publisher involved.
In order for documentation to be written, somebody has got to take the
time to write it.
If this is to happen using "quality" time such that proper concentration
can be given to it, this *likely* requires that the writer receive some
funding to spend time that might otherwise be spent (oh, say) working.
Writing a book takes considerable effort. That effort deserves to be
recompensed. It is not unlikely that the time required to write a book
like Programming Perl consumes time that could otherwise be used to earn
on the order of $50,000.
The FSF hasn't been offering that kind of money to people for the
service of writing documentation; I don't have any knowledge of what
Larry Wall or other Perl luminaries get for writing their books, but
evidently O'Reilly Books has been offering enough "filthy lucre" that
they feel it more benefits them to sell the rights...
--
"All language designers are arrogant. Goes with the territory..."
(By Larry Wall)
cbbrowne@hex.net - <http://www.hex.net/~cbbrowne/lsf.html>
------------------------------
Date: 12 Jun 1998 19:18:53 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Have we got a good free Perl manual?
Message-Id: <m3btry2gdu.fsf@windlord.Stanford.EDU>
In comp.lang.perl.misc, Paul David Fardy <pdf@morgan.ucs.mun.ca> writes:
> Odd... Why don't they need "a good free manual for TeX"?
Because TeX's documentation is distributed under something functionally
equivalent to the Artistic License, and the documentation that comes with
TeX or is available via other places such as CTAN is honestly nearly
sufficient. It's at least much better than the other listed items except
for Perl.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 13 Jun 1998 01:52:06 GMT
From: tpitb@iwaynet.net (Ryan Baghdad)
Subject: Help a newbie with arrays! (I think ... )
Message-Id: <6lsm06$qi0$1@news.iwaynet.net>
Hi - I work at a law school, and am faced with the unbearable task of
sorting and analyzing survey results. So I've decided to take a stab at
Perl and have it do the work for me. Unfortunately, I think my lack of
understanding with arrays has me at a roadblock.
I have no idea if this is good form, methodology, or what. Please
critique me with no mercy! (I can take it.) Basically the idea is this:
each survey is 14 questions with answers that can be represented
numerically. So I put the answers to all the surveys (numbering over
200) into a text file, in this format:
1 => $recnum (record number)
4
2
...
3
endofrec
2 => $recnum
4
3
Anyway, the goal of this is that the professor can say to me, "Find me
all the surveys that answered 'yes' [1] to question 13, 'no' [2] to
question 4, and answered '3' to question 6."
The way I'm doing it is trying to read *all* the survey answers into
one big array, and then going through that array again trying to match
up the specific questions, and incrementing a counter if it finds a
match. My problem is the method I'm using to store each of these
variables. There will be like 500+ active variables at once, after I've
read them all in. So, for instance, this is how I'm reading the records
now:
sub read_recfile {
$count = 0;
print "enter recordbase: ";
$fname = <STDIN>;
open(readbuf,"<$fname");
do {
$count++;
read(readbuf,$recnum,3);
read(readbuf,$position[$count],2); # experimental form
read(readbuf,$experience,2);
read(readbuf,$race,2);
read(readbuf,$sex,2);
read(readbuf,$complaint,2);
read(readbuf,$lawsuit,2);
read(readbuf,$counseling,2);
read(readbuf,$unfair,2);
read(readbuf,$upward,2);
read(readbuf,$promotion,2);
read(readbuf,$counanother,2);
read(readbuf,$insuff,2);
read(readbuf,$marker,6);
} until eof(readbuf);
close(readbuf);
$count--; # Does it make sense why I do this?
print "record count: " . $count;
exit;
Anyway, see how I have $position[$count] above? The idea is that
somehow I can create an array, based on the $count (which increments
each loop) so that elsewhere in the program, I could reference
$position[4] and it would give me the fourth person's survey response
to the $position question. I think this is totally wrong. But I also
think I'm on the right track.
And then, to search, for, say, everyone you answered 1 to $position, I
could just do:
for ($x=1, $x=$count, $x++) {
if ($position[$x] == 1) {
$people_who_answered_yes++;}
}
Basically I need the fastest, easiest method of temporarily storing and
searching through all those variables, which sounds like a case for
arrays. But after scouring several online documents, I have yet to put
together how to actually assign the values to the variables, and
individually identify them. I'm guessing this is something really easy.
I understand that you can do @survey=($recnum,$position,$race...); but
how does this help me search through each of the individually?
Anything you could do to help out would be greatly appreciated, and if
this is totally incoherent, I truly apologize.
As my news server doesn't always catch all responses, please cc: them
to tpitb@iwaynet.net.
Thanks a lot!
ryan
------------------------------
Date: Thu, 11 Jun 1998 22:47:20 -0400
From: "Scott" <sdh1@anchor.hotmail.com>
Subject: Re: Is this insane?
Message-Id: <6lspc8$gij$1@camel21.mindspring.com>
Steve Vertigan wrote in message
<199806121531.XAA08685@opera.iinet.net.au>...
>Hi. I've just written a spiffy (cgi/linux) application that my boss wants
>me to get running on an NT server that doesn't have a Perl interpreter
>installed. It occurred to me that one way would be to compile the Perl
code
>into C using the compiler and then to compile that C using the GNU compiler
>for win32. 10 hours and one miserly test.c file that wont compile later
I'm
>starting to wonder if it was such a brilliant idea after all. Has anyone
>managed to do something like this successfully or is there an easier way to
>do things?
>
>Any help/advice/career changes, much appreciated!
>
>--Steve
Park ranger is a nice, relaxing career. I'm applying for that position
sometime
in December, 1999.. :-)
Other than that...
I have compiled the perl interpreter into C in HP/UX. It's kind of
obtuse the
way you have to pass parameters and such.
AND, in version 5.004, there the destructor function doesn't destruct, so
there
is a tad of a memory leak, at least under HP/UX 10.2. I know 10.X is
flakey.
AND, your perl code inside C code isn't exactly easy to read and write.
Install perl. You'll thank yourself for it.
------------------------------
Date: 13 Jun 1998 00:31:26 GMT
From: Zenin <zenin@bawdycaste.org>
Subject: Re: New module/pragma "enum.pm" (was "fields.pm")
Message-Id: <897698370.910313@thrush.omix.com>
Brendan O'Dea <bod@compusol.com.au> wrote:
>snip<
: The prefix and `..' ideas are pretty cool.
Actually, I still would like to see a single RealWorld use of the
.. syntax, but it was in the spec I used.
: I would prefer to be able to do this at times:
: use enum Foo => 1, Bar => 2;
Hmm, I question your use of enum. Are you trying to get a shorter
use constant syntax? This is not what enum is intended for, either
in C or here.
>snip<
: rather than only:
: use enum qw(Foo=1 Bar=2);
: since I tend to put spaces around `='.
Since it's intention is to allow one to reset the index there
shouldn't be much use of '=' anyway. If you want arbitrary
constants, use constant.
: The non-quoted format also allows the use of arbitrary expressions to
: set values:
: use enum q(:OPT_), foo => 0x1, bar => 0x2, baz = 0x4;
: use enum OPT_mask => OPT_foo|OPT_bar;
Blagh... This is /not/ a "multi"-constant module. Personally
I think the constant (or manybe 'constants' (with an 's')?) module
should allow for multiple assignments in one block. A enum module
however, is not the place for this.
It seems to me you're also looking for something close to enum but
that does enumerated bitmasks instead. If so, you might be
interested in this module I'm working on (not released to CPAN yet).
It's nearly the same as enum.pm, but it creates single bit masks.
ftp://thrush.omix.com/pub/perl/modules/bitmask-1.003.tar.gz
Speaking of which, anyone know an efficient way to see if a number
is a power of 2 without needing a lookup table?
--
-Zenin
zenin@archive.rhps.org
------------------------------
Date: 12 Jun 1998 22:58:03 -0400
From: Lloyd Zusman <ljz@asfast.com>
Subject: Re: New module/pragma "enum.pm" (was "fields.pm")
Message-Id: <ltwwamc8jo.fsf@asfast.com>
Zenin <zenin@bawdycaste.org> writes:
> [ ... ]
>
> Speaking of which, anyone know an efficient way to see if a number
> is a power of 2 without needing a lookup table?
Here's one that works on integers:
sub isPowerOfTwo {
use integer;
my $number = shift;
if ($number == 0) {
return (undef);
}
elsif ($number < 0) {
# Assumes that you will be testing negative numbers to see
# if they're negative powers of two. If not, change this
# to suit your needs ...
return (isPowerOfTwo(-$number));
}
else {
my $n = 0;
while (($number &= ($number - 1)) != 0) {
$n++;
}
# at this point, `$n' contains one less than the count of the
# number of `1' bits in the original value of `$number'.
return ($n == 0);
}
}
This can be Perl-ized to make the source code more cryp^H^H^H^Hcompact.
A couple months ago, I benchmarked a group of Perl algorithms to
calculate something similar involving powers of two, and my submission
contained a variation on this particular algorithm. While it was the
fastest when I originally benchmarked it in C, it turned out to be one
of the slower ones in Perl. You can find the other algorithms and the
accompanying discussion in Deja News in this newsgroup under the
following subject:
Fastest "smallest power of 2 >= N" in Perl?
The fastest algorithm back then is one of the algorithms that I'm
using within my signature, below, although I'm sure that it will be
clearer to go to Deja News and see how it was originally stated by its
author. This isn't exactly the same algorithm you're looking for, as
I mentioned above, but it still might be adaptable to your needs.
--
Lloyd Zusman ljz@asfast.com
perl -e '$n=170;for($d=2;($d*$d)<=$n;$d+=(1+($d%2))){for($t=0;($n%$d)==0;
$t++){$n=int($n/$d);}while($t-->0){push(@r,$d);}}if($n>1){push(@r,$n);}
$x=0;map{$x+=(($_>0)?(1<<log($_-0.5)/log(2.0)+1):1)}@r;print"$x\n"'
------------------------------
Date: 12 Jun 1998 23:57:38 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: new to this - simple if statement question
Message-Id: <6lsf9i$806$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Kerrie Etter <kerrie@home.com> writes:
:if ($state == California) {
: print "Location:/newpublic/investors/ca.html\n\n";
Run with use strict.
Run with -w.
Check the perlop manpage.
Don't leave home without manpages. And FAQS.
Check out virtually any existing perl script.
--tom
--
I think I'm likely to be certified before Perl is... :-)
--Larry Wall in <1995Feb12.061604.6008@netlabs.com>
------------------------------
Date: 12 Jun 1998 21:17:13 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: new to this - simple if statement question
Message-Id: <6lsjup$ha6$1@monet.op.net>
In article <3581E8F0.C0D2DA2C@home.com>, Kerrie Etter <kerrie@home.com> wrote:
>if ($state == California) {
You need to use `eq'.
== means to interpret the values as numbers, and when Perl does that
they both come out to be zero.
------------------------------
Date: 12 Jun 1998 23:52:01 GMT
From: "Mr. Chow Wing Siu" <wschow@Comp.HKBU.Edu.HK>
Subject: perl script emulates "last" command
Message-Id: <6lsev1$cak$1@power42t.hkbu.edu.hk>
Hi,
I would like to know any perl script can emulate the "last"
command in UNIX.
Please mail. Thanks for help.
- --
PGP PUBLIC KEY: https://www.comp.hkbu.edu.hk/~wschow/pgp.html
Key fingerprint = 15 C4 36 D6 EC CF 1D A4 7F D8 F9 EF 2E D7 32 A6
Version: 2.6.3i
Charset: noconv
iQCVAwUBNYHBcL3ixeOqBhAdAQH8HwP9HGUmunToD/lpTDHNmuxWVy2rxkSQ9S+r
6PHwqw+IpgOnHT0F4wvBF5CheCFM/gNZWoluakQitQ/d+2YXSm0YMwiQ+1VCTzqd
87EjA9j/7Lcy7v+kDR8eNz/tMHMlYi+hpP1Ok4o7q14dHLTy4kz2PlxCK6flULn1
R09ogq7GdIk=
=iBX4
-----END PGP SIGNATURE-----
------------------------------
Date: 13 Jun 1998 02:04:20 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Prob in FAQ: Week of the year
Message-Id: <6lsmn4$b93$1@client3.news.psi.net>
Tom Christiansen (tchrist@mox.perl.com) wrote on MDCCXLVI September
MCMXCIII in <URL: news:6ls39i$feb$2@csnews.cs.colorado.edu>:
++ [courtesy cc of this posting sent to cited author via email]
++
++ In comp.lang.perl.misc,
++ abigail@fnx.com writes:
++ :Given that 7 doesn't divide 365, so you always end up with
++ :weeks not beginning and/or ending on year boundaries, the
++ :only silly thing to do is to ignore a standard.
++
++ Welcome to America, where Common Law and established practice are
++ far more important than Napoleonic Codes. :)
I doubt Napolean came with the ISO definitions for a week. And we have
ISO definition, just to have a common law - which of course in Europe
with its quilt of countries is more difficult to establish than in a
single large country as the US.
I don't care what kind of method is used - they both make as much
(non)sense to me. It's just silly to claim one of the method is
madness.
Abigail
--
perl -pwle '$_ .= reverse'
------------------------------
Date: Fri, 12 Jun 1998 19:00:27 -0700
From: "Igor Krivokon" <igor.k@usa.net>
Subject: Re: Puzzle challenge
Message-Id: <6lsmj3$ji4$1@news.ncal.verio.com>
Michael J Gebis wrote in message <6ls9hb$m6j@mozo.cc.purdue.edu>...
>Tim.Bunce@ig.co.uk (Tim Bunce) (Tim Bunce) writes:
>
>}The problem is that the messengers quite often, say 70%, miss out one
>}or more names and occasionally, say 10%, get the order wrong.
>
>}Names are never added, repeated or changed, only missed or reordered.
>}The messengers always think they've got it right.
>
>} Original list: foo bar baz boo
>
>} Messenger A says: foo bar boo
>} Messenger B says: bar boo baz
>} Messenger C says: foo bar baz boo
>} Messenger D says: boo foo bar baz
>} Messenger E says: foo bar baz
>} Messenger F says: foo baz boo
>
>Suggested algorithm:
> Find the most common first element. It's the first.
> Remove all occurences from the list.
> Repeat until there's nothing left.
I'd like to suggest a small change to your algorithm.
Let's take into consideration the following fact:
if the order is wrong, we can not trust the messenger anymore.
The algorithm now is:
Find the most common first element. Add it to resulting list.
Remove all occurences from the list; if the element found not in the
first
position, remove the whole row.
Remove empty rows.
Repeat until there's nothing left.
Example:
A says: foo bar boo
B says: bar boo baz
C says: foo bar baz boo
D says: boo foo bar baz
E says: foo bar baz
F says: foo baz boo
Most common first element: foo. Delete row D
The remaining rows are:
A says: bar boo
B says: bar boo baz
C says: bar baz boo
E says: bar baz
F says: baz boo
Most common first element: bar.
The remaining rows are:
A says: boo
B says: boo baz
C says: baz boo
E says: baz
F says: baz boo
Most common first element: baz. Delete rows B, E
The remaining rows are:
A says: boo
C says: boo
F says: boo
Most common first element: boo.
Therefore, the original order is:
foo bar baz boo
Note: this algorithm still seems to be reasonable, but not optimal.
One optimal (and very non-efficient) algorithm I know is:
Find all N unique words. Generate all N! combinations and chose
the most probable one.
(The most probable combination corresponds to the maximum number of
trusted messengers, where trusted messenger means a messenger with
correct order of words)
Igor Krivokon
<igor.k@usa.net>
------------------------------
Date: 13 Jun 1998 01:38:16 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: REVIEW: Perl CGI Programming - No Experience Required
Message-Id: <6lsl68$jtr$1@csnews.cs.colorado.edu>
Review of Perl CGI Programming - No Experience Required
Title: Perl CGI Programming - No Experience Required
ISBN 0-7821-2157-8
Author: Erik Strom
Publisher: Sybex (1998)
Pages: 412pp + xviii
Size: 9" W 7 1/2" W 1"
Price: US $29.99
Rating: Two Camels [So-So]
------------------------------------------------------------------------
Capsule Review
Even though this is really just a CGI book, not one that
actually teaches Perl, this book is one that I would like to
like. The prose reads easily enough and it's not a typesetting
abominations. The author seems to have the right background; he
knows K&R--the true Bible of any good programmer--and knows a lot
of Perl history. Unfortunately, it has too many strange technical
mistakes as is. It needs someone who knows Perl better to fix it.
I think it could make 3 camels.
------------------------------------------------------------------------
Detailed Review
The ``no experience required'' is a give-away that the book hopes to address
rank beginners. In fact, it says you don't have to be a programmer (p xvii).
Then later (p26) it says that we're all ``computer nerds'' so won't mind
using I as a verb. This seems contradictory. (It also forgets that the OED
documents -- and Fowler/Birchfield bless, this usage.)
He suggests acquiring the ActiveState version of Perl for Windows, rather
than the Standard Port. I wonder whether this was written before the
standard port (which is much better at nearly everything) existed.
------------------------------------------------------------------------
Specific Errors and Omissions
Here are the chapters, and their problems.
1. Introducing Perl and CGI
The book mentions ``Perl-CGI'' repeatedly.
This bogus neologism misleads the world into confusing the two. It
shouldn't exist. We don't talk about C-TCP. I feel like there must
be a book out there called Introducing Your Car and the Highway
System, or Teach Yourself Automobile-Interstate in 20,000 Leagues
Around the World.
``All code lines in Perl must end in semicolon.'' [p19]
No, semicolons separate statements.
Claims that a list is a ``number of scalars stored sequentially in one
variable. [p22]
No, that's not true. A list isn't an array. Never was, never will
be. He's thinking of an array, and even then it's wrong due to
things like [1,2,3].
He compounds his transgression on p24 where he claims that ``a Perl
list is the equivalent of an aray in Visual, Basic, C++, and many other
languages. The terms will be used interchangeable in this book.''
The author obviously doesn't understand the difference between
lists and arrays, and misleads the reader into the same iniquity.
His braces are very badly indented. Everywhere.
This flies in the face of the perlstyle(1) manpage and all the
included examples.
2. Bringing Perl to the World Wide Web
print "Content-type: text/html", "\n\n"; # MIME header
Problems: (this occurs many places)
+ Why the comma?
+ That's an HTTP header, not a MIME header.
+ The "\n\n" won't work on a Mac. Oh well. I've done the same
thing myself. :-( For the record, the persnickety correct
thing is "\015\012\015\012", which certainly gets old, fast.
Uses htmlend.pl for a library file, and hellowww.pl for a program file.
[p52]
Don't use the same extension for both! They aren't the same.
Uses "\n\n" after every line output, including regular HTML. [p54]
This is silly. He does it all over the book.
Alfred Packer [p62]
No, that's actually Alferd.
3. Connecting Perl to the World Wide Web
4. Using Perl and CGI in the Real World
``Think, don't code!'' sounds nice.
Pictures of girls. [p90 et al.]
This is gratuitous and annoying. Remind me to put some centerfolds
in my next book.
A hit counter [90-106]
What a useless, stupid, vain, and in fact, evil application!
Didn't anyone ever tell you that you can't count the number of
times your page is viewed? Plus it's full of mistakes. No error
checking is done on the system calls, and worst of all, there's no
file locking. No file locking!? Drivel and dreck, useless to heck.
5. Creating Real-World HTML Forms with Perl and CGI
The sins begin to compound.
o He doesn't explain the real difference between GET and POST
(idempotency, caching, read vs write).
o He teaches people to decode the form by hand. Wicked. Bad.
Naughty. This makes him teach tr and s///g and many inappropriate
things. Then we get lost in regular expressions. BAD!
6. Perl and the Complex Web Page
o More confusion about GET vs POST.
o He decodes the post by hand, and wrongly!. Failing grade for the
whole book here. Why must people keep recreating the wheel, and
wrongly! This is just plain bad programming.
7. Creating a Guest Book for Your Web Site
8. Creating Dynamic Web Pages: More Tools
9. Monitoring Web Site Activity
10. The Language of the Web
11. Platforms of the World Wide Web
12. Advanced Perl-CGI Tricks
13. Security on Your Web Site
Appendices
The mandatory list of all functions in Perl can be found here, but full
of mistakes. He hardcodes AF_UNIX. He makes incorrect assertions about
local.
And he mis-stems supersede (to sit over) as supercede (to pass over).
Sitting and passing are not the same. Get thee to an etymologist - and
I'm not talking about bugs.
------------------------------------------------------------------------
Copyright 1998 Tom Christiansen.
All rights reserved.
--
X-Windows: Let it get in YOUR way.
--Jamie Zawinski
------------------------------
Date: 13 Jun 1998 02:52:06 GMT
From: c.c.eiftj@54.usenet.us.com (Rahul Dhesi)
Subject: Re: REVIEW: Perl CGI Programming - No Experience Required
Message-Id: <6lspgm$ikt$1@samba.rahul.net>
In <6lsl68$jtr$1@csnews.cs.colorado.edu> Tom Christiansen
<tchrist@mox.perl.com> writes:
> Title: Perl CGI Programming - No Experience Required
> Author: Erik Strom
> Claims that a list is a ``number of scalars stored sequentially in one
> variable. [p22]
> No, that's not true. A list isn't an array. Never was, never will
> be. He's thinking of an array, and even then it's wrong due to
> things like [1,2,3].
> He compounds his transgression on p24 where he claims that ``a Perl
> list is the equivalent of an aray in Visual, Basic, C++, and many other
> languages. The terms will be used interchangeable in this book.''
> The author obviously doesn't understand the difference between
> lists and arrays, and misleads the reader into the same iniquity.
He's not the only one who is confused; I am too. 'Programming Perl'
on page 6 has a section that begins:
Arrays: An array is an ordered list of scalars, accessed by the
scalar's position in the list.
This section, which is supposedly describing arrays, uses the words
'list' and 'lists' 12 times on one page of text. After reading that
section there was no doubt in my mind that 'array' and 'list' are
synonymous. And who am I to question what Wall, Christiansen, and
Schwartz write? The only obvious error I see in what Strom wrote
(above) is that a list doesn't always have to be stored in a variable.
But neither does an array, so I'm confused again.
The only difference between a list and an array that I can think of is
that we call a list an array if we use subscript notation on it at least
once in a program. So the difference appears to be what we do, not what
it is.
--
Rahul Dhesi <dhesi@spams.r.us.com>
------------------------------
Date: 13 Jun 1998 02:37:11 GMT
From: aa126@NOSPAM.fan.nb.ca (William Burrow)
Subject: Two-way sockets
Message-Id: <slrn6o3pen.pmk.aa126@fan1.fan.nb.ca>
I'm starting to play with sockets. I need two-way communication between
the server and the Perl script. The example in the perlipc man page works
for one line of output from the server, but then appears to lock up. I
can't send anything to the server as expected. Any tips?
--
--
William Burrow, VE9WIL -- New Brunswick, Canada
Copyright 1997 William Burrow
------------------------------
Date: Fri, 12 Jun 1998 23:17:15 GMT
From: root.noharvest.\@not_even\here.com (-)
Subject: unencoding email attachments with perl??
Message-Id: <3581b468.20363076@news2.cais.com>
I've searched through CPAN, looked all over at various "script archive
sites" and cannot find anything like this. I'm sure someone somewhere
has written a module or library to handle this.....
I want to be able to add functionality to my email filtering scripts
(called by .forward) to capture mime/uuencoded/BinHex attachments and
write them to a file... I don't have control over what encoding
method is being used on the sending side, so it needs to be able to
handle all three (I saw a uuencode/uudecode script in the CPAN, but
that only deals with 1/3 of what I need).
It doesn't necessarily have to be a perl module, I mean it could
simply be executing a linux/unix command, much the same as
`lynx -source http;//blah.blah.com/`;
to get the source of a web page, if that makes any sense.
Any ideas? Any help would be appreciated.
If you want to email an answer to me, send it to
webmaster @ lmnet . com, although sending it to this usenet group is
preferred so others might learn too. :)
------------------------------
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 2862
**************************************