[10953] in Perl-Users-Digest
Perl-Users Digest, Issue: 4554 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 5 14:17:13 1999
Date: Tue, 5 Jan 99 11:00:24 -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 Tue, 5 Jan 1999 Volume: 8 Number: 4554
Today's topics:
Re: $0 as .//programName ? <dgris@moiraine.dimensional.com>
$a = `$cmd` repost <shum@cig.mot.com>
Apache Problem: Can display but not execute marimba@atlcom.net
Re: background server (Eric Hammervold)
Does 'require' run the script automatically? strattner@my-dejanews.com
Re: Dynamic gifs on the fly (brian d foy)
Re: Embedding PERL in C & also C in PERL <ibelgaufts@gfc-net.de>
Re: Embedding PERL in C & also C in PERL (Greg Ward)
Re: getting the dimensions of an Image (Clay Irving)
Re: Help - replacing text in a file (Greg Ward)
Re: Help - replacing text in a file (Abigail)
Re: How do I delete tmp files based on age? (Larry Rosler)
Re: How do I delete tmp files based on age? (Bart Lateur)
How to set primary group in NT? <ldrigger@olemiss.edu>
Insidious S Problem <none@email-intentionally-omitted.com>
Re: Insidious S Problem (brian d foy)
Re: Insidious S Problem <pk@dwc.ch>
Re: Insidious S Problem (Andre L.)
isa_defs.ph dies "ISA not supported" <tom_jenquin@mgic.com>
Lists of Lists in Perl 4 <rp7667@email.sps.mot.com>
Re: Looking for Example PERL plotting/GUI package <tlynch@cisco.com>
Re: New posters to comp.lang.perl.misc (Larry Rosler)
Re: newline <dgris@moiraine.dimensional.com>
Re: newline <ljz@asfast.com>
Re: newline (Greg Ward)
Re: newline <dgris@moiraine.dimensional.com>
Re: OK I give up (After a WEEK!) (Greg Ward)
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 05 Jan 1999 10:34:01 -0700
From: Daniel Grisinger <dgris@moiraine.dimensional.com>
Subject: Re: $0 as .//programName ?
Message-Id: <m390fhaak6.fsf@moiraine.dimensional.com>
NukeEmUp@ThePentagon.com (David Cantrell) writes:
> Odd, I thought this was a perl-oriented newsgroup. Perl is
> cross-platform.
Perl is a cross-platform distillation of the unix mindset.
dgris
--
Daniel Grisinger dgris@moiraine.dimensional.com
perl -Mre=eval -e'$_=shift;;@[=split//;;$,=qq;\n;;;print
m;(.{$-}(?{$-++}));,q;;while$-<=@[;;' 'Just Another Perl Hacker'
------------------------------
Date: Tue, 05 Jan 1999 10:37:49 -0600
From: Edmond Shum <shum@cig.mot.com>
Subject: $a = `$cmd` repost
Message-Id: <36923FDD.B0A4C69C@cig.mot.com>
Just found out that I posted this article in HTML yesterday. I hope this
time
it comes out in plain text.
I was trying to execute something like:
$a = `$cmd`;
Under usual situation, it is ok. However I have encountered the
situation
that if the system out of resources ( swap, process ), it will fail.
If it fails for *many* times, the above statement will fail again for
max open file
handle being reached, even when resource is released.
It turned out to be that when it fails, it just close one end of the
pipe.
So I try the following:
pipe(IN,OUT);
if ( $pid = fork ) {
close OUT;
while (<IN>) { $a .= $_; }
close IN;
wait;
}
elsif ( defined $pid ) {
close IN; close STDIN; close STDOUT;
open (STDOUT,">&OUT");
exec "$cmd";
}
else { close IN; close OUT } # cannot fork
to make sure that both end will close properly upon failed fork.
You see how tedious and heavy is this code ? Anyone has suggestion on
any other solution ?
I tried to modify the perl source code ( util.c ) of perl4.036.
Basically
I added the line in mypopen, which did an extra close when fork failed.
This works good but I am not sure it will break any other things or not
--
Edmond Shum System and Network Administration
Motorola CIG email: shum@cig.mot.com
Arlington Heights, IL phone: 847-632-6702
------------------------------
Date: Tue, 05 Jan 1999 17:19:23 GMT
From: marimba@atlcom.net
Subject: Apache Problem: Can display but not execute
Message-Id: <76thio$ds7$1@nnrp1.dejanews.com>
This is a straight up newbie problem but I've tried the most common fixes for
it. When I enter the URL for the script multiply.cgi, it displays the code
instead of the output.
1) multiply.cgi has #!/usr/local/bin/perl which is an existing shell
2) multiply.cgi's permissions are 777
3) multiply.cgi is located in the cgi-bin directory
4) According to Apache docs, in a new installation, the cgi-bin directory is
ready to go and doesn't need to be ScriptAliased or anything. This is a new
installation of both Apache and Perl
5) Versions are Apache 1.3.0 and Perl 5.004.04
Does anybody have any suggestions other than the first 3 canned suggestions?
Thanks,
CB
m a r i m b a @ a t l c o m . n e t
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 5 Jan 1999 18:04:28 GMT
From: ehammerv@tricity.wsu.edu (Eric Hammervold)
Subject: Re: background server
Message-Id: <76tk7c$bep$1@leopard.it.wsu.edu>
Ok, i took a look at Win32:Service and all it does is manage the
services. Thats not what i want to do, i want to create a service that
will run in the background even though your not loged in, use perl of
course. So has anyone done it? and how? I found the admin information
i needed so all i need now is this.
Any ideas or information would be greatly appreciated!
Thanks again!
Eric,
Martien Verbruggen (mgjv@comdyn.com.au) wrote:
: In article <76rmng$nci$1@leopard.it.wsu.edu>,
: ehammerv@tricity.wsu.edu (Eric Hammervold) writes:
: > I need some information on generating background servers for windows
: > NT. Specificly is it possible for a perl program to be running as a
: > server program while nobody is loged into the machine? If so, how
: > do you do it?
: I believe those things are called 'services' on NT. There's a module
: Win32::Servic available from CPAN (http://www.perl.com/CPAN/) which
: probably does what you want.
: You should also check out www.activestate.com for Win32 specific perl stuff.
: Martien
: --
: Martien Verbruggen |
: Webmaster www.tradingpost.com.au | The gene pool could use a little
: Commercial Dynamics Pty. Ltd. | chlorine.
: NSW, Australia |
------------------------------
Date: Tue, 05 Jan 1999 18:10:19 GMT
From: strattner@my-dejanews.com
Subject: Does 'require' run the script automatically?
Message-Id: <76tkia$gmc$1@nnrp1.dejanews.com>
I am new to Perl, aware of the common reaction to newbie questions in this
newsgroup, and decided to post a question anyway...how's that for unjustified
optimism? (Please keep any snide answers to yourself)
Anyway, here's my question/problem: I have some Perl scripts. Each contain
exactly one subroutine. The subroutines handle the bulk of the processing;
the main program merely presents it in a verbose manner. In general, the
scripts are of the form:
#!/usr/bin/perl
....sub code (returns an array)
....main code
.....call sub
.....print results of sub in a user-friendly format
.end
What I would like to do is to call the subroutines from another script. I
thought it was as simple as adding the path to @INC and using require.
However, when I did that, it appears that the entire script is run, as
opposed to the subroutine only. In other words, I get the 'nice' output
instead of the array. I added '1' to the end of the script files (as per
"Perl by Example", p.217 - see, I did research before posting), but that
didn't help. I have the Perl documentation (in html format), but I can't find
anything of relevance there either. Could someone direct me to where the
answer can be found? Any constructive help will be appreciated.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Tue, 05 Jan 1999 12:39:17 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: Dynamic gifs on the fly
Message-Id: <comdog-ya02408000R0501991239170001@news.panix.com>
In article <76rrhv$276s$1@node17.cwnet.frontiernet.net>, "Leo" <chaimwerz@yahoo.com> posted:
> Does anyone overhere know a perl script that could paste me together 2 gifs
> (a picture frame & a metal greting add on)On the fly according to a users
> input?
this sort of thing is trivial with Image::Magick or some of the other
solutions given to the same question earlier this week.
good luck :)
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
------------------------------
Date: Tue, 05 Jan 1999 15:54:44 +0100
From: "J|rgen Ibelgaufts" <ibelgaufts@gfc-net.de>
Subject: Re: Embedding PERL in C & also C in PERL
Message-Id: <369227B4.1CA39D70@gfc-net.de>
Hi,
Look into your pod directory if you have perl5.005. You will find
perl/5.005/lib/Pod/perlembed.pod (at least I have it there. I built perl under
windows nt from the standard source).
It contains some examples to embed perl into c.
If you want to embed c in perl (most probably this means writing perl extensions
in c) take a look at perl/5.005/lib/Pod/perlxs.pod and
perl/5.005/lib/Pod/perlxstut.pod. They contain a description of the xs meta
language and a tutorial how to write extenxions. Most of the perl extensions are
written with xs.
if you use windows nt, you might want to use the Win32::API module which gives
access to arbitrary dynamic link libraries.
Hope this helps
Juergen Ibelgaufts
--------------------------------------------------------------------
Ramanujam Parthasarathi schrieb:
>
> Hi,
>
> Embedding PERL in C and vice-versa seem interesting. Can anyone suggest
> where I can find some introduction to it - some sort of a book......
>
> Thanks
------------------------------
Date: 5 Jan 1999 18:36:44 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: Embedding PERL in C & also C in PERL
Message-Id: <76tm3s$p5o$4@news0-alterdial.uu.net>
Ramanujam Parthasarathi <partha@mihy.mot.com> wrote:
> Embedding PERL in C and vice-versa seem interesting. Can anyone suggest
> where I can find some introduction to it - some sort of a book......
The book *Advanced Perl Programming* by Sriram Srinivasan (O'Reilly &
Associates, ISBN 1-56592-220-4, http://www.oreilly.com/catalog/advperl/)
covers both of these, but not really exhaustively -- it's more of an
overview. :-(
The real dirt can be found in Perl's online documentation: see the
'perlembed' man page for embedding info, and 'perlxstut', 'perlxs', and
'perlguts' for extending (XSUB) info.
Greg
--
Greg Ward - software developer gward@cnri.reston.va.us
Corporation for National Research Initiatives
1895 Preston White Drive voice: +1-703-620-8990 x287
Reston, Virginia, USA 20191-5434 fax: +1-703-620-0913
------------------------------
Date: 5 Jan 1999 12:05:44 -0500
From: clay@panix.com (Clay Irving)
Subject: Re: getting the dimensions of an Image
Message-Id: <76tgp8$468@panix.com>
In <76t2ve$m2i$1@plug.news.pipex.net> "Artoo" <r2-d2@REMOVEbigfoot.com> writes:
>Does anyone know how you can get perl to tell you the pixel dimensions of a
>Jpeg image on the server?
Perl Modules are your friend.
Image::Size
http://www.perl.com/CPAN/modules/by-module/Image
Image::Size is a library based on the image-sizing code in the
wwwimagesize script, a tool that analyzes HTML files and adds
HEIGHT and WIDTH tags to IMG directives. Image::Size has generalized
that code to return a raw (X, Y) pair, and included wrappers to
pre-format that output into either HTML or a set of attribute
pairs suitable for the CGI.pm library by Lincoln Stein. Currently,
Image::Size can size images in XPM, XBM, GIF, JPEG, TIFF, PNG and
the PPM family of formats (PPM/PGM/PBM).
--
Clay Irving
clay@panix.com
------------------------------
Date: 5 Jan 1999 17:44:00 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: Help - replacing text in a file
Message-Id: <76tj10$mfu$1@news0-alterdial.uu.net>
Dale Sutcliffe <dales@enhanced-performance.com> wrote:
> How do you insert into the middle of a file without having to rewrite
> the whole file. e.g.,
> A file containing:
>
> A|B|C|D
> B|C|E|F
As long as your original and replacement text are the same length, you
can do this by treating the file as though it were binary. Use 'seek'
to position yourself at the right place in the file, then 'read' to read
a chunk of data, change the data, 'seek' back to where you started, and
then 'print' to write the changed data back out. Something like this:
$offset = ... # finding the data you want to change
# left as an exercise for the reader
seek (FILE, $offset, 0); # move relative to start of file
read (FILE, $chunk, $chunk_length);
$chunk = ... # replace $chunk with a string of the same length
seek (FILE, $offset, 0);
print FILE $chunk;
Obviously, you'll have to add error-checking to the seek, read, and
print calls. That's the basic idea though.
And as another poster pointed out, this probably isn't worth it unless
you're dealing with a really big file. The conventional way explained
in the FAQ (section 4, I think) is more flexible.
Greg
--
Greg Ward - software developer gward@cnri.reston.va.us
Corporation for National Research Initiatives
1895 Preston White Drive voice: +1-703-620-8990 x287
Reston, Virginia, USA 20191-5434 fax: +1-703-620-0913
------------------------------
Date: 5 Jan 1999 18:49:47 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Help - replacing text in a file
Message-Id: <76tmsb$d8j$1@client3.news.psi.net>
Dale Sutcliffe (dales@enhanced-performance.com) wrote on MCMLIII
September MCMXCIII in <URL:news:3691655D.173CD98B@enhanced-performance.com>:
&& How do you insert into the middle of a file without having to rewrite the whole file.
&& e.g.,
&& A file containing:
&&
&& A|B|C|D
&& B|C|E|F
&&
&& I wand to replace the C on the first line with Z in the same file.
&& How is this done?
Use the -i switch.
Abigail
------------------------------
Date: Tue, 5 Jan 1999 10:44:47 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How do I delete tmp files based on age?
Message-Id: <MPG.10fbfd79c33da2249898f7@nntp.hpl.hp.com>
In article <m1zp7x4shv.fsf@halfdome.holdit.com> on 05 Jan 1999 08:03:08
-0800, Randal L. Schwartz <merlyn@stonehenge.com> says...
> >>>>> "John" == John Warner <jwarner@tivoli.com> writes:
...
> John> $last_access = stat($file)[8] + hold_time;
>
> missing $ there
To say nothing of the fact that it would be a syntax error in any case!
$last_access = (stat $file)[8] + $hold_time;
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 05 Jan 1999 18:33:19 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: How do I delete tmp files based on age?
Message-Id: <3694595d.1479295@news.skynet.be>
diane6683@my-dejanews.com wrote:
>I've written a perl cgi that requires the use of temp files - a search is
>performed, the results are put in a temp file, then the user accesses the
>temp file to view selected groups of the results. The user might never look
>at this file, or he might access it a dozen times. So, I thought I should
>write something in the cgi to check the time of all the files in the temp
>directory, and delete all that have been in there for more than a set amount
>of time, say, 5 minutes.
Think of ( -M $file ). It returns the age of the files since the start
of this script, in whole days.
unlink $file if -M $file > 5/(24*60);
Somehow, I have doubts if this will still work under mod_perl.
Bart.
------------------------------
Date: Tue, 05 Jan 1999 12:16:05 -0600
From: Les <ldrigger@olemiss.edu>
Subject: How to set primary group in NT?
Message-Id: <369256E5.3C88C295@olemiss.edu>
In User Manager for Domains there is an option to set a user's primary
group.
How can I do that from within perl?
Does it really matter?
Thanks,
Les Driggers
------------------------------
Date: Tue, 5 Jan 1999 12:16:39 -0500
From: "Matt Edwards" <none@email-intentionally-omitted.com>
Subject: Insidious S Problem
Message-Id: <76thdj$g8e$1@ligarius.ultra.net>
I have a string: "?question |pipe"
I want to print: "?*"
Given the following three variables,
$search = "?question |pipe";
$find = "question |pipe";
$replace = "*";
I am wondering if the following logic can be shortened:
$test = &subst($search, $find, $replace);
if ($test != -1)
{
$search = $test;
}
print $search;
sub subst()
{
local($search, $find, $replace) = @_;
local($offset) = index($search, $find);
if ($offset != -1)
{
$search = substr($search, 0, $offset) . $replace . substr($search,
$offset + length($find), length ($search));
return $search;
}
return -1;
}
exit;
Using s or some other command, it seems that the above 18 lines could be
shortened to three lines:
$search =~ s/$find/$replace/;
print $search;
exit;
However, $find is always compiled as a regexp before the match is evaluated,
and hence any regexp tokens in $find will have special meaning. Is it
possible to use s without having $find be evaluated as a regexp? I have been
unable to find any information in the Perl FAQ or the Perl docs.
-Matt Edwards
------------------------------
Date: Tue, 05 Jan 1999 12:44:10 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: Insidious S Problem
Message-Id: <comdog-ya02408000R0501991244110001@news.panix.com>
In article <76thdj$g8e$1@ligarius.ultra.net>, "Matt Edwards" <none@email-intentionally-omitted.com> posted:
> I have a string: "?question |pipe"
> I want to print: "?*"
>
> Given the following three variables,
>
> $search = "?question |pipe";
> $find = "question |pipe";
> $replace = "*";
#!/usr/bin/perl
$search = "?question |pipe";
$find = "question |pipe";
$replace = "*";
$search =~ s/\Q$find/$replace/;
print $search;
notice the use of \Q. you might also look into quotemeta() and
friends.
good luck :)
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
------------------------------
Date: Tue, 05 Jan 1999 18:31:21 +0100
From: Per Kistler <pk@dwc.ch>
Subject: Re: Insidious S Problem
Message-Id: <36924C69.6CE5EBAE@dwc.ch>
Hi Matt
Putting some "\" in front of the regexp chars...
(extralines for posting to go out...)
(extralines for posting to go out...)
(extralines for posting to go out...)
(extralines for posting to go out...)
Per.
> However, $find is always compiled as a regexp before the match is evaluated,
> and hence any regexp tokens in $find will have special meaning. Is it
> possible to use s without having $find be evaluated as a regexp? I have been
> unable to find any information in the Perl FAQ or the Perl docs.
>
> -Matt Edwards
--
Per Kistler
Digital Wire Consulting
pk@dwc.ch (or: kistler@gmx.net)
-----------------------------------------------------------------------
------------------------------
Date: Tue, 05 Jan 1999 13:27:46 -0500
From: alecler@cam.org (Andre L.)
Subject: Re: Insidious S Problem
Message-Id: <alecler-0501991327460001@dialup-515.hip.cam.org>
In article <76thdj$g8e$1@ligarius.ultra.net>, "Matt Edwards"
<none@email-intentionally-omitted.com> wrote:
[...]
> However, $find is always compiled as a regexp before the match is evaluated,
> and hence any regexp tokens in $find will have special meaning. Is it
> possible to use s without having $find be evaluated as a regexp? I have been
> unable to find any information in the Perl FAQ or the Perl docs.
It's right there in perlre and perlop, and it's called \Q.
s/\Q$find/$replace/;
Read the Fine Documentation. It's full of clues and it will save you a lot
of trouble.
HTH,
Andre
------------------------------
Date: Tue, 05 Jan 1999 10:22:47 -0600
From: Tom Jenquin <tom_jenquin@mgic.com>
Subject: isa_defs.ph dies "ISA not supported"
Message-Id: <36923C56.445ECEAD@mgic.com>
I'm having trouble starting one of my perl programs that works under
perl5.004_04.
It fails at startup with a message "ISA not supported at
/.../isa_defs.ph line 74
It looks like it is trying to determine if I am on a thread capable
machine. I looks like
it is checking for 'sparc' (I am running on a Sun sparc machine), but,
it is not defined.
Here is my "perl -V" output:
osname=solaris, osvers=2.5.1, archname=sun4-solaris-thread
uname='sunos xxxxxx 5.5.1 generic_103640-21 sun4m sparc
sunw,sparcstation-20 '
hint=previous, useposix=true, d_sigaction=define
usethreads=define useperlio=undef d_sfio=undef
Compiler:
cc='cc', optimize='-O', gccversion=
cppflags='-D_REENTRANT -I/usr/local/include'
ccflags ='-D_REENTRANT -I/usr/local/include'
stdchar='unsigned char', d_stdstdio=define, usevfork=false
intsize=4, longsize=4, ptrsize=4, doublesize=8
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
alignbytes=8, usemymalloc=y, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib
libs=-lsocket -lnsl -ldl -lm -lposix4 -lpthread -lc -lcrypt -llu62
libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
cccdlflags='-KPIC', lddlflags='-G -L/usr/local/lib'
Characteristics of this binary (from libperl):
Built under solaris
Compiled at Dec 24 1998 09:46:53
%ENV:
PERL_BADFREE="0"
@INC:
/home2/TEST-perl/lib/5.00502/sun4-solaris-thread
/home2/TEST-perl/lib/5.00502
/home2/TEST-perl/lib/site_perl/5.005/sun4-solaris-thread
/home2/TEST-perl/lib/site_perl/5.005
/home2/rdsadm/bin
.
------------------------------
Date: Tue, 05 Jan 1999 10:54:13 -0700
From: Omar Soto <rp7667@email.sps.mot.com>
Subject: Lists of Lists in Perl 4
Message-Id: <369251BD.57FB76DC@email.sps.mot.com>
Hello:
This may seem like a very basic question, but I am trying to
create a List of lists or an array of arrays, as defined in the
Perl 5 book p. 257.
The problem is that the definition works in Perl 5 but not in
Perl 4. Due to the requirements of the job, the on ly Perl
supported is Perl 4 and not Perl 5.
Please Advice,
Omar Soto
------------------------------
Date: Tue, 05 Jan 1999 12:32:55 -0500
From: Tom Lynch <tlynch@cisco.com>
Subject: Re: Looking for Example PERL plotting/GUI package
Message-Id: <36924CC7.97163BCB@cisco.com>
Brian D. Schieber wrote:
>
> Hi,
> I'd like to see what it would take to put together a
> simple plotting package in PERL. Are there any example
> PERL packages out there that have:
>
> * A GUI interface
> * Can plot simple XY plots and/or log-linear line plots
>
Check CPAN, you'll find a few there:
Chart::Chart-Plot-0.07.tar.gz
Chart::Chart-GnuPlot-0.02.tar.gz
ExtUtils::PGPLOT-2.09.tar.gz
Term::Term-Gnuplot-0.54.tar.gz
Good Luck
Tom
--
#-----------------------+--------------------------+
# Tom Lynch | Email: tlynch@cisco.com |
# Cisco Systems | Phone: 978-244-8765 |
# 250 Apollo Drive | FAX: 978-244-8039 |
# Chelmsford MA 01824 | MS: CH1-2LF |
#-----------------------+--------------------------+
------------------------------
Date: Tue, 5 Jan 1999 10:40:45 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: New posters to comp.lang.perl.misc
Message-Id: <MPG.10fbfc8421a6fd319898f6@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and a copy mailed to Greg Bacon.]
In article <3692361E.946AD464@cthulhu.demon.nl> on Tue, 05 Jan 1999
10:56:15 -0500, Erik van Roode <newsposter@cthulhu.demon.nl> says...
> ruben@wynn.com wrote:
> >
> > What is OCR and why am I being picked on here?
>
> In the first paragraph of the article you posted you could have
> read what OCR means.
>
> > - Original Content Rating (OCR) is the ratio of the original content
> > volume to the total body volume.
>
> I bet your OCR will be low in the next monthly post, since you quoted
> a lot, with only a few lines original content ...
But note that the adjective 'original' has two disparate meanings that
could be applied in this context:
2b: constituting that from which a copy, reproduction, or translation is
made
3: independent and creative in thought and action
The original(2b) submission that I have reproduced above makes it clear
that the meaning intended for OCR is Original(3) Content Rating. But it
could easily have been misinterpreted (as I have done until now).
Perhaps Greg Bacon, who is apparently the owner of the program that
generates the reports, might consider a different, less ambiguous
adjective. How about something simple -- New?
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 05 Jan 1999 10:39:02 -0700
From: Daniel Grisinger <dgris@moiraine.dimensional.com>
Subject: Re: newline
Message-Id: <m34sq5aabt.fsf@moiraine.dimensional.com>
Lloyd Zusman <ljz@asfast.com> writes:
> In Perl5, it's better to use `chomp' instead of `chop'. The `chop'
> function strips off the rightmost character of a string no matter
> what, while `chomp' strips it only if it's an end-of-line character.
To be precise, chomp checks if the string ends with the contents
of $/. If it does, it removes $/ from the string. While this is
a newline by default, it can be changed.
> It's best to use `chop' only in special cases where the final character
> of a string needs to be removed irrespective of its value. In the
> "get rid of the trailing newline" case, `chomp' is usually preferred.
It's best to use chop if you know that you need to get rid of the last
character regardless of what it is (if you are reading in paragraph
mode, perhaps, but still need to strip some line endings). But
usually you should use chomp.
dgris
--
Daniel Grisinger dgris@moiraine.dimensional.com
perl -Mre=eval -e'$_=shift;;@[=split//;;$,=qq;\n;;;print
m;(.{$-}(?{$-++}));,q;;while$-<=@[;;' 'Just Another Perl Hacker'
------------------------------
Date: 05 Jan 1999 13:17:04 -0500
From: Lloyd Zusman <ljz@asfast.com>
Subject: Re: newline
Message-Id: <ltpv8t7ffj.fsf@asfast.com>
Daniel Grisinger <dgris@moiraine.dimensional.com> writes:
> Lloyd Zusman <ljz@asfast.com> writes:
>
> [ ... ]
>
> > It's best to use `chop' only in special cases where the final character
> > of a string needs to be removed irrespective of its value. In the
> > "get rid of the trailing newline" case, `chomp' is usually preferred.
>
> It's best to use chop if you know that you need to get rid of the last
> character regardless of what it is (if you are reading in paragraph
> mode, perhaps, but still need to strip some line endings). But
> usually you should use chomp.
I'm curious: how does the meaning of your clarifying paragraph differ
from mine?
--
Lloyd Zusman ljz@asfast.com
perl -le '$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"'
------------------------------
Date: 5 Jan 1999 18:27:25 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: newline
Message-Id: <76tlid$p5o$2@news0-alterdial.uu.net>
Herger Peter <peter.herger@swisslife.ch> wrote:
> I've wrote a script like the following.
[some code deleted]
> `mkdir /usr/net/routers/rtconfigs/$storedir`;
Blech! This is bad style for two reasons: don't use backquotes unless
you need to (ie. you really need to capture the output of a command),
and don't use an external command where Perl has a builtin. The second
worst way to do this is:
system ("mkdir /usr/net/routers/rtconfigs/$storedir");
die "mkdir failed\n" unless $? == 0;
The second best way is
mkdir "/usr/net/routers/rtconfigs/$storedir", 0755
or die "mkdir failed: $!\n";
The best (IMHO) way is:
my $dir = "/usr/net/routers/rtconfigs/$storedir";
mkdir $dir, 0755 or die "$0: couldn't create $dir: $!\n";
(Of course, you might be ignoring a failed mkdir for a reason: you might
want to continue processing if the directory already exists. But what if
it didn't exist and you want to create it, or just march happily on if it
already exists? Try this:
my $dir = "/usr/net/routers/rtconfigs/$storedir";
mkdir $dir, 0755 or die "$0: couldn't create $dir: $!\n"
unless -d $dir and -w $dir;
This will bomb if the directory exists and isn't writeable, which is
probably OK (after all, you'll bomb when you try to put a file into it,
which seems to be what you're doing later on).
Disclaimer: code untested. Parenthesize defensively as necessary.
Greg
--
Greg Ward - software developer gward@cnri.reston.va.us
Corporation for National Research Initiatives
1895 Preston White Drive voice: +1-703-620-8990 x287
Reston, Virginia, USA 20191-5434 fax: +1-703-620-0913
------------------------------
Date: 05 Jan 1999 11:46:41 -0700
From: Daniel Grisinger <dgris@moiraine.dimensional.com>
Subject: Re: newline
Message-Id: <m3lnjh8smm.fsf@moiraine.dimensional.com>
Lloyd Zusman <ljz@asfast.com> writes:
> Daniel Grisinger <dgris@moiraine.dimensional.com> writes:
> > It's best to use chop if you know that you need to get rid of the last
> > character regardless of what it is (if you are reading in paragraph
> > mode, perhaps, but still need to strip some line endings). But
> > usually you should use chomp.
>
> I'm curious: how does the meaning of your clarifying paragraph differ
> from mine?
It doesn't, it just is more explicit. I figure that perhaps someone
will see the more complete description and have it stick. That
way, if they ever do something like-
{
local $/ = '%%some%%weird%%record%%separator%%';
while (<FILE>) {
chomp;
do_stuff($_);
}
}
They might remember that chomp doesn't always remove newlines.
dgris
--
Daniel Grisinger dgris@moiraine.dimensional.com
perl -Mre=eval -e'$_=shift;;@[=split//;;$,=qq;\n;;;print
m;(.{$-}(?{$-++}));,q;;while$-<=@[;;' 'Just Another Perl Hacker'
------------------------------
Date: 5 Jan 1999 18:20:29 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: OK I give up (After a WEEK!)
Message-Id: <76tl5d$p5o$1@news0-alterdial.uu.net>
Marc Austin <placeit@easyad.com> wrote:
> I need help with a script... bad.
Yes, you do. Every followup I've seen so far has been useful, but I'll
see if I can sum up and provide a detailed critique of your code.
> #!/usr/bin/perl
You're not using -w or 'use strict'. You should. Always.
> $email_file = "temp.txt";
> $email = "someone\@xxxxx.com";
If you were using 'use strict', you'd be forced to declare these
variables using 'my', which is a good thing.
> &split_key;
Using subroutine arguments is a good thing: eg. pass $email_file and
$email into split_key, rather than having it rely on them as globals.
Makes your code more modular and more maintainable: yet another good
thing.
> sub split_key {
> $email_found = 0;
>
> open(KEYEMAIL,"$email_file") || die "can't open $email_file\n";
> @keyemail = <KEYEMAIL>;
> chop(@keyemail);
> close(KEYEMAIL);
It's common practice to indent code in subroutines, conditional/loop
statements, etc. Makes your code more readable. Certain languages
(eg. Python) are so keen on readability that they *force* you to indent
in those cases, which is a very good reason for novice programmers to
start with Python... ;-)
You get one point for the "|| die" after "open" -- a distressingly high
number of posters here forget that completely basic point. You would
have gotten another point if you had included the $! special variable in
your error message, so that the error message would include the reason
*why* the "open" failed.
Also, there's no need to quote $email_file in the 'open' statement:
open (KEYMAIL, $email_file) || die ...
is all you need.
> foreach (@keyemail) {
>
> ($field1,$field2) = split(/\\t/);
>
> if ($field2 = $email) { #changed this
> $email_found = 1;
> }
> }
Here's where your problems *really* start. First off, you're splitting
your "email_address password" lines on the string '\t' -- not a tab, but
a backslash followed by the letter t. This would work if the lines in
$email_file looked like
joeblow@cool.com\tMyPasswd
but they don't. My guess is that you really meant to split on
whitespace. Perl makes this extra-super-duper easy when the string to
be split is in $_:
($field1, $field2) = split;
(And it's not much harder when the string to split is in any other
variable: 'perldoc -f split' for the details.)
Second bug: you're using the assignment operator, '=', for a string
comparison. *Bzzzt!* You should be using the string comparison
operator, 'eq'. (Perl inherits C's '=' vs '==' problem, and throws 'eq'
in just for fun.)
Third bug: you're comparing the second field from the file (the
password) with the email address that you're searching for. Should be
'$field1 eq $email'.
Finally, when you do find a hit, you don't exit the loop: you just keep
going. This is the reason that you always see the last line rather than
the matching line. This is what the 'last' statement is for.
> if ($email_found == 1) {
^^^^
Comparing a boolean variable to a true value is redundant. Just use 'if
($email_found) ...'.
Here's how I would write your &split_key routine with fixed bugs,
conventional indentation, subroutine arguments, and declared variables:
sub split_key {
my ($email_file, $email) = @_;
my $email_found = 0;
open(KEYEMAIL,"$email_file") || die "can't open $email_file\n";
my @keyemail = <KEYEMAIL>;
chop(@keyemail);
close(KEYEMAIL);
foreach (@keyemail) {
my ($field1,$field2) = split;
if ($field2 eq $email) {
$email_found = 1;
last;
}
}
if ($email_found) {
print "Content-type: text/html\n";
print "\n";
print "<html><head><title>Success!</title></head>\n";
print "<body>\n";
print "<center>This is a test<p>Field1 - $field1<p>Field2 - $field2" .
"<p></center></body></html>\n";
}
}
Now, if you feel really adventurous -- and ready to embrace the true
Perl spirit -- see if you can grok this as a replacement for your
'foreach' loop and the test after it:
my @found = grep ((split)[0] eq $email, @keyemail);
die "oh no! found too many matches" if @found > 1;
if (@found) {
my ($field1, $field2) = split ('', $found[0]);
# report results
}
Pick this apart a bit at a time. Use the debugger interactively
('perl -de 1') to see what's going on. When you can put it
back together again and figure it out for yourself, then
you're starting to get the hang of it.
Disclaimer: the above code is completely untested and off the top of my
head. And I read all the other followups to see all the bugs other
people caught before writing a detailed critique.
Greg
--
Greg Ward - software developer gward@cnri.reston.va.us
Corporation for National Research Initiatives
1895 Preston White Drive voice: +1-703-620-8990 x287
Reston, Virginia, USA 20191-5434 fax: +1-703-620-0913
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
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 4554
**************************************