[18198] in Perl-Users-Digest
Perl-Users Digest, Issue: 366 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Feb 27 11:10:27 2001
Date: Tue, 27 Feb 2001 08:10:13 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <983290212-v10-i366@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 27 Feb 2001 Volume: 10 Number: 366
Today's topics:
newbie question. <remalone@sympatico.ca>
Re: newbie question. <roger.foskett@icl.com>
obtaining keys from values - hashes <rdobson@MailAndNews.com>
Re: obtaining keys from values - hashes <tony_curtis32@yahoo.com>
Re: OT unwanted exclamation marks (Tad McClellan)
Re: Perl program review request <Jerome.Abela@free.fr>
Re: Perl program review request (Tad McClellan)
Re: Perl/HP-UX - Odd $ENV Behavior <bmb@ginger.libs.uga.edu>
Re: Please Help with 10 line programm -- beginner quest (Tad McClellan)
Problem with Win32::EventLog <webmaster@gastenboek.net.is.nog.net.nief.af>
Sendmail / Windows Question (Shawn Ryder)
Server Push <philip.shean@uwe.ac.uk>
Re: shell script to perl (NEWBIE) <mjcarman@home.com>
Sorting by date <sorryno@email.at.all>
Re: Sorting by date (Rafael Garcia-Suarez)
Re: Sorting by date <Gary.Karcz@nau.edu>
Re: timeout connect() with select? <perlCHEESE@CHEESEatlaswebmail.com>
Re: timeout connect() with select? <perlCHEESE@CHEESEatlaswebmail.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 27 Feb 2001 14:18:22 GMT
From: "Rick M" <remalone@sympatico.ca>
Subject: newbie question.
Message-Id: <OGOm6.310634$f36.11243576@news20.bellglobal.com>
Greeting good folks,
I am just finishing chapter 6 of Learning Perl and simultaneously trying to
rewrite a script from sh to perl.
Regarding this readdir example of Programming Perl 2nd Ed. pp. 202
opendir THISDIR, "." or die "serious dainbramage: $!";
@allfiles = readdir THISDIR;
closedir THISDIR;
print "@allfiles\n";
How can I get the output of the print "@allfiles\n"; statement to look the
same as system "ls";
ie. formatted in nice columns
Here is what I have tried and the closest I have come without success.
$MY_DIR = "/home/rickm/projects";
chdir $MY_DIR;
system "ls";
print "\n";
opendir THISDIR, "." or die "serious dainbramage: $!";
@allfiles = grep !/^\.\.?$/, readdir THISDIR;
closedir THISDIR;
foreach (@allfiles) {
printf "%-15s", $_;
}
No when I run this the output of ls looks like this:
file1 file2 file3 file4 file5
file6 file7 file8 file9 file10
and the output of the printf statement looks like this
file1 file2 file3 file4 file5
file6
file7 file8 file9 file10
Thanks
Rick Malone
------------------------------
Date: Tue, 27 Feb 2001 14:38:41 -0000
From: "rog" <roger.foskett@icl.com>
Subject: Re: newbie question.
Message-Id: <97geea$1il4$1@news.icl.se>
> Here is what I have tried and the closest I have come without success.
>
> $MY_DIR = "/home/rickm/projects";
> chdir $MY_DIR;
> system "ls";
> print "\n";
> opendir THISDIR, "." or die "serious dainbramage: $!";
> @allfiles = grep !/^\.\.?$/, readdir THISDIR;
> closedir THISDIR;
> foreach (@allfiles) {
> printf "%-15s", $_;
> }
your printf function probably needs a newline "\n" char in it
foreach (@allfiles) {
printf "%-15s\n", $_;
}
might be easier to do something like:
$dir = "/home/rickm/projects";
@arr = `ls $dir`;
print @arr;
which is essentially:
bash$ perl -e 'print `ls`'
rog
------------------------------
Date: Tue, 27 Feb 2001 09:41:38 -0500
From: richard dobson <rdobson@MailAndNews.com>
Subject: obtaining keys from values - hashes
Message-Id: <3AAE6756@MailAndNews.com>
Basic question, but, does anyone know a quick way of obtaining a hash key if
I
know the value?
Thanks in advance
Rich
------------------------------------------------------------
Get your FREE web-based e-mail and newsgroup access at:
http://MailAndNews.com
Create a new mailbox, or access your existing IMAP4 or
POP3 mailbox from anywhere with just a web browser.
------------------------------------------------------------
------------------------------
Date: 27 Feb 2001 08:54:34 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: obtaining keys from values - hashes
Message-Id: <87lmqsrxat.fsf@limey.hpcc.uh.edu>
>> On Tue, 27 Feb 2001 09:41:38 -0500,
>> richard dobson <rdobson@MailAndNews.com> said:
> Basic question, but, does anyone know a quick way of
> obtaining a hash key if I know the value?
Consider this example:
my %hash = ( a => 41, b => 42, c => 42 );
Given "42", which is the correct key?
There's generally no unique key as the answer. If you're
happy with a set of possible keys, then you can iterate
over the keys of the hash and push the matching ones into
an array.
hth
t
--
The avalanche has already started.
It is too late for the pebbles to vote.
------------------------------
Date: Tue, 27 Feb 2001 09:29:12 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: OT unwanted exclamation marks
Message-Id: <slrn99nedo.acd.tadmc@tadmc26.august.net>
Bernard El-Hagin <bernard.el-hagin@lido-tech.net> wrote:
>On Tue, 27 Feb 2001 12:52:45 -0000, Terry <dcs@ntlworld.com> wrote:
>>Hi
>>
>>I don't think this is actually a perl\cgi problem but hopefully somebody has
>>come across this before and can help:-)
>
>Well I've got a nasty itch on my back *just* where I can't reach it. I'm
>hoping that maybe someone has had a similar experience and could help
>me.
Since you must read all of Perl's standard documentation to post
here anyway, you should print it all out for easy reference.
Then stack it up until it reaches the height of the affected back
area, turn around, and rub your back against the stack.
If you are tall, you may need to download some or all of the
modules from CPAN so you will have even more valuable
documentation to print, useful for solving all of life's
problems one way or another.
Use the docs, Luke!
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 27 Feb 2001 15:42:43 GMT
From: Jerome Abela <Jerome.Abela@free.fr>
Subject: Re: Perl program review request
Message-Id: <3A9BC9E6.8A3AECCA@free.fr>
Bill Feidt a wrote:
> If folks would care to review and comment on my most
> recent Perl programming effort, I'd be pleased to have
> the input.
>
> http://www.cpcug.org/dialup/shdisk.txt
It contains a strange loop structure:
open (INFIL, "/homec/wfeidt/tbin2/diskts.ts")
or die "Could not open diskts.ts for read: $!";
foreach (@elements) {
foreach $line (<INFIL>) {
open (INFIL, "/homec/wfeidt/tbin2/diskts.ts")
or die "Could not open diskts.ts for read: $!";
}
}
This code works as follow:
- INFIL is opened before any loop
- the foreach(<INFIL>) reads the whole file before entering the loop,
and iterates over this list
- in each iteration of the inside foreach(), INFILE is opened again and
again, for nothing.
I suggest the following, instead:
foreach (@elements) {
open (INFIL, "/homec/wfeidt/tbin2/diskts.ts")
or die "Could not open diskts.ts for read: $!";
while(<INFIL>) {
my $line = $_;
}
}
Jerome.
------------------------------
Date: Tue, 27 Feb 2001 09:56:42 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Perl program review request
Message-Id: <slrn99ng1a.acd.tadmc@tadmc26.august.net>
Bill Feidt <wfeidt@his.com> wrote:
>If folks would care to review and comment on my most
>recent Perl programming effort, I'd be pleased to have
>the input.
I see one major problem, one minor problem, and a bunch of
style nitpiks. See below.
>Program:
>
> http://www.cpcug.org/dialup/shdisk.txt
The "meat" of the program is only 30 lines, you should have
included that part in your post.
Have you seen this Perl FAQ from part 6?
"How do I efficiently match many regular expressions at once?"
> foreach (@elements) {
> my $object = $_;
No need to put it into $_ only to copy somewhere else, you can just
put it somewhere else to begin with:
foreach my $object ( @elements ) {
> my ($line, $prevpct);
> $prevpct = "";
> foreach $line (<INFIL>) {
You can combine the my() with the first use of a variable most times,
no need to declare them in one big list ahead of time:
my $prevpct = "";
foreach my $line (<INFIL>) {
Minor problem: reads *all* of the file into memory,
what if file is really big?
Use a while() instead of a foreach() so there is only one line
at a time in memory:
while ( my $line = <INFIL> ) {
> if ($line =~ $object) {
A pattern match should *look like* a pattern match:
if ( $line =~ /$object/ ) {
> my ($date2, $volume, $percent);
> ($date2, $volume, $percent) = split (/\|/, $line);
my($date2, $volume, $percent) = split (/\|/, $line); # again
> $percent =~ /(\d+)/;
Major problem: using "dollar-digit variables" without first testing
for a successful match.
The dollar-digit vars are set only on a *successful* pattern match.
If the match fails, they retain whatever old values they had, leading
to a very Hard To Find Bug.
Never use the dollar-digit vars without first checking to see if the
match succeeded.
die "'$percent' contains no digit chars!" unless $percent =~ /(\d+)/;
Since they get changed on successful matches, and writing regexs is
a common thing to do, you should immediately copy the variables
in case you happen to end up throwing in another pattern match
somewhere before you use the variable. Meaningful variable names
help document your code as an added bonus :-)
my $percent_digits = $1;
> if ($1 > 89) {
if ( $percent_digits > 89 ) {
> }
> open (INFIL, "/homec/wfeidt/tbin2/diskts.ts") or die "Could not open diskts.ts for read: $!";
You could open() the file once at the top of the loop instead of once
before the loop together with at the end of the loop.
But I would consider rethinking your algorithm so that you can read
the file only one time, and search for all of the @elements at once.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 27 Feb 2001 09:37:59 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Perl/HP-UX - Odd $ENV Behavior
Message-Id: <Pine.A41.4.21.0102270936220.12368-100000@ginger.libs.uga.edu>
On Mon, 26 Feb 2001, Bill Border wrote:
> On most servers, this works OK. However, I
> have found a server where apparently the env
> does not get set by the $ENV{'ORACLE_SID'} = "SID1";
> and when I try the $os = $ENV{"ORACLE_SID"}; I get
> the error message:
>
> Can't locate object method "ENV" via package "ORACLE_SID" at /opt/dbamon/bin/dbamon_ora_sumupdate.pl line 18.
>
> Huh??? What is happening. Could this be that the Perl on this server
> where it does not work does not recognize the $ENV associate array
> name for the ENV? I'm corn-fused.
1 #!/usr/local/bin/perl
2 use warnings;
3 use strict;
4
5 my $os = ENV {"X"};
6
7 __END__
8 Can't locate object method "ENV" via package "X" at ./qt line 5.
You didn't by any chance forget the '$' did you?
Brad
------------------------------
Date: Tue, 27 Feb 2001 08:38:22 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Please Help with 10 line programm -- beginner questions
Message-Id: <slrn99nbee.aa1.tadmc@tadmc26.august.net>
Gwyn Judd <tjla@guvfybir.qlaqaf.bet> wrote:
>I was shocked! How could Thoren Johne <thoren@southern-division.com>
>say such a terrible thing:
>>In article <3A9ACCD1.C1DEA364@ll.mit.edu>, Shane McDaniel aka
>>shanem@ll.mit.edu says...
>>
>>> Cool. It's points like these that I like to tell people that "Every
>>> programming language will let you shoot yourself in the foot, however
>>> with perl you're using an automatic."
>>
>>yeah - and logo is no shooting, but bondage ;)
>
>While Java is like having someone nail a loaded gun to your foot.
I can't make deja/google find the darn thread we had here about
foot shooting in Perl, so here's my followup taken from my archives:
----------------------------------------------------------
From: tadmc@metronet.com (Tad McClellan)
Newsgroups: comp.lang.perl.misc
Subject: Re: shooting yourself in the foot ...
References: <391A2049.39E05465@citr.com.au> <958035739.18315@itz.pp.sci.fi>
Message-Id: <slrn8hlfpr.k1b.tadmc@magna.metronet.com>
User-Agent: slrn/0.9.5.7 (UNIX)
Date: Thu, 11 May 2000 10:10:03 -0400
On 11 May 2000 09:10:45 GMT, Ilmari Karonen <iltzu@sci.invalid> wrote:
>In article <391A2049.39E05465@citr.com.au>, James Beard wrote:
>>I was just trolling through some old jokes, and I came across
>>http://www.users.csbsju.edu/~lziegler/ShootingInFoot.html
>>
>>Does anyone have a "shooting yourself in the foot with perl"?
>
>"You grab the gun and pull the trigger without aiming. The bullet
> hits your foot, which is what Perl assumed you wanted."
Make a trigger pulling motion, and Perl will:
autovivify a gun for you
initialize it with bullets
fire a bullet
move your foot so as to coincide with the bullet's trajectory
say "Ouch!" for you
----------------------------------------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 27 Feb 2001 15:47:47 +0100
From: "WichersWeb" <webmaster@gastenboek.net.is.nog.net.nief.af>
Subject: Problem with Win32::EventLog
Message-Id: <97geoh$48o$1@porthos.nl.uu.net>
I've got he following problem.
When I try to connect the local computer with
new Win32::EventLog("System", "\\\\WS28") || die "dan niet";
everything goes fine.
But when I really try to connect a remote computer it doesn't work.
any ideas?
------------------------------
Date: Tue, 27 Feb 2001 15:39:46 GMT
From: sryder@webryder.com (Shawn Ryder)
Subject: Sendmail / Windows Question
Message-Id: <3a9bc91f.13475030@news>
I have the following script created to simply submit an email form
from Flash to the script and send it to the designated email address
which is set inside the Flash file - but I realize that it only works
with UNIX, how do I get it to work correctly in a windows enviroment ?
#!/usr/bin/perl
use CGI;
$query = new CGI;
for ($query->param) { $in{$_} = $query->param($_); }
unless(open(MAIL,"|/usr/lib/sendmail -t")) {
print "Content-type: text/html\n\n";
print "Unable to open sendmail binary.";
exit;
}
print MAIL "To: $in{mail_to}\n";
print MAIL "Subject: $in{mail_subject}\n";
print MAIL "From: $in{mail_from}\n\n";
print MAIL $in{mail_message};
close(MAIL);
print "Location: http://www.webryder.com\n\n";
Thanks for any advice - please feel free to contact me directly !
Shawn Ryder
sryder@webryder.com
http://www.webryder.com
Webryder Internet Design and Consulting
------------------------------
Date: Tue, 27 Feb 2001 14:14:11 GMT
From: "Phil Shean" <philip.shean@uwe.ac.uk>
Subject: Server Push
Message-Id: <G9F67n.DLH@bath.ac.uk>
On my IIS 5 server, when I do a server push in perl, the webpage just gets
appended with the new page.
Does anyone know why this is happening?
Can anyone provide me with a script that will perform a server push on an
IIS 5 server running perl 5.6?
Cheers in advance
Phil.
------------------------------
Date: Tue, 27 Feb 2001 08:17:32 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: shell script to perl (NEWBIE)
Message-Id: <3A9BB6FC.1A7C5623@home.com>
egwong@netcom.com wrote:
>
> terminalsplash <shino_korah@yahoo.com> wrote:
> >
> > I was trying to convert a shell script *.sh to perl script.
> > In the shell script the statement
> > setenv THENAME /dir1/file1
> > in perl i used system("setenv THENAME /dir1/file1);
> > It says can't execute setenv..no such file or directory
>
> To do something similar to "setenv" in perl, use the %ENV hash. In your
> case, it will be
>
> $ENV{THENAME}='/dir1/file1';
>
> see the perlvar manpage for details on %ENV.
Note: Perl runs as a child process of the shell -- changes to your
environment will *not* be reflected back in the parent. If your script
is setting things up for later use (i.e. after it has finished) you will
probably want to leave it as a shell script. If you only need THENAME
defined for the duration of your script, you're fine.
-mjc
------------------------------
Date: Tue, 27 Feb 2001 14:38:29 -0000
From: "Brian J" <sorryno@email.at.all>
Subject: Sorting by date
Message-Id: <3a9bbbf0_1@news2.uncensored-news.com>
I'm writing a forum where each topic is stored as a .txt file, and would
like to know if there is a way to sort or read the files into the order
they were last modified so that they could be displayed in such a way.
I'm currently using -
@topics = grep (/^.+\.txt$/, readdir (TOPICS));
- to make a list of the files, but this stores them in the order in
which the files were created.
--
Brian
______________________________________________________________________
Posted Via Uncensored-News.Com - Still Only $9.95 - http://www.uncensored-news.com
With Servers In California, Texas And Virginia - The Worlds Uncensored News Source
------------------------------
Date: Tue, 27 Feb 2001 14:53:36 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Sorting by date
Message-Id: <slrn99nfqq.tfv.rgarciasuarez@rafael.kazibao.net>
Brian J wrote in comp.lang.perl.misc:
> I'm writing a forum where each topic is stored as a .txt file, and would
> like to know if there is a way to sort or read the files into the order
> they were last modified so that they could be displayed in such a way.
>
> I'm currently using -
>
> @topics = grep (/^.+\.txt$/, readdir (TOPICS));
>
> - to make a list of the files, but this stores them in the order in
> which the files were created.
This is a side effect. You cannot rely on the order of the list given by
readdir.
The -M operator, described in perlfunc, gives you the age of file in
days. You can use it to sort the list. -- It's a good idea to use the
Schwartzian transform (described in perlfaq4, "How do I sort an array by
(anything)?") to perform the sort, to avoid quering the modification
time many times for a same file.
--
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
------------------------------
Date: Tue, 27 Feb 2001 08:56:41 +0700
From: "Gary E. Karcz" <Gary.Karcz@nau.edu>
Subject: Re: Sorting by date
Message-Id: <97ginq$1i0$1@usenet.nau.edu>
Brian
Try a Schwartzian Transform like (most recent modification date last):
@topics = map{$_->[0]}
sort{$a->[1] <=> $b->[1]}
map{[$_, ((stat($_))[9])]}
grep{/\.txt$/}
readdir(TOPICS);
sort{$b->[1] <=> $a->[1]}
will reverse the sort order.
Hope this helps!
--Gary
In article <3a9bbbf0_1@news2.uncensored-news.com>, "Brian J"
<sorryno@email.at.all> wrote:
> I'm writing a forum where each topic is stored as a .txt file, and would
> like to know if there is a way to sort or read the files into the order
> they were last modified so that they could be displayed in such a way.
>
> I'm currently using -
>
> @topics = grep (/^.+\.txt$/, readdir (TOPICS));
>
> - to make a list of the files, but this stores them in the order in
> which the files were created.
>
>
> --
>
> Brian
>
>
>
> ______________________________________________________________________
> Posted Via Uncensored-News.Com - Still Only $9.95 -
> http://www.uncensored-news.com
> With Servers In California, Texas And Virginia - The Worlds Uncensored
> News Source
>
------------------------------
Date: Mon, 26 Feb 2001 23:14:10 +1100
From: "h." <perlCHEESE@CHEESEatlaswebmail.com>
Subject: Re: timeout connect() with select?
Message-Id: <3a9bb98c@nexus.comcen.com.au>
Anno Siegel wrote in message <97dc0n$i65$1@mamenchi.zrz.TU-Berlin.DE>...
>Here you're using $eout in the position where the file numbers
>to be checked for errors are expected. Also, you're setting it
>to the integer 1, but it must be a bit string of file numbers.
>You should acquaint yourself with select by reading perldoc select
>and man select.
Ok. I read the man pages, they are written for C, of course.
In C, the 3rd argument to select has something to do with
"exceptions", not "errors". - eg. signals as well as errors.
The if the connect() call fails for a socket handle, then the
socket immediately becomes readable and writable.
Seems weird to me, but that's what the documentation says.
That's why I've been putting the file descriptor (whatever
that is) in the third argument for the select call.
I could not find the select perl doc, could someone please let me know where
I can find it. Also, if someone could please let me know what perldoc
explains
what a file number is and what a file descriptor is, I'd appreciate it. All
the documentation I've read for select() mentions file numbers, file
descriptors, et. al.
I fear I'll never be able to understand select() until I can get a solid
handle on what
a file descriptor is, and what it looks like and how it functions on the bit
level.
p.s. Please note that my main goal is to understand, not just to find a way.
>Anno
Thanks for your help, Anno.
------------------------------
Date: Mon, 26 Feb 2001 23:14:10 +1100
From: "h." <perlCHEESE@CHEESEatlaswebmail.com>
Subject: Re: timeout connect() with select?
Message-Id: <3a9bc0c4@nexus.comcen.com.au>
Anno Siegel wrote in message <97dc0n$i65$1@mamenchi.zrz.TU-Berlin.DE>...
>Here you're using $eout in the position where the file numbers
>to be checked for errors are expected. Also, you're setting it
>to the integer 1, but it must be a bit string of file numbers.
>You should acquaint yourself with select by reading perldoc select
>and man select.
Ok. I read the man pages, they are written for C, of course.
In C, the 3rd argument to select has something to do with
"exceptions", not "errors". - eg. signals as well as errors.
The if the connect() call fails for a socket handle, then the
socket immediately becomes readable and writable.
Seems weird to me, but that's what the documentation says.
That's why I've been putting the file descriptor (whatever
that is) in the third argument for the select call.
I could not find the select perl doc, could someone please let me know where
I can find it. Also, if someone could please let me know what perldoc
explains
what a file number is and what a file descriptor is, I'd appreciate it. All
the documentation I've read for select() mentions file numbers, file
descriptors, et. al.
I fear I'll never be able to understand select() until I can get a solid
handle on what
a file descriptor is, and what it looks like and how it functions on the bit
level.
p.s. Please note that my main goal is to understand, not just to find a way.
>Anno
Thanks for your help, Anno.
------------------------------
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 V10 Issue 366
**************************************