[12046] in Perl-Users-Digest
Perl-Users Digest, Issue: 5646 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu May 13 03:13:30 1999
Date: Thu, 13 May 99 00:04:09 -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 Thu, 13 May 1999 Volume: 8 Number: 5646
Today's topics:
Re: PERLFUNC: eval - catch exceptions or compile code (Bart Lateur)
Re: PERLFUNC: eval - catch exceptions or compile code (Randal L. Schwartz)
Re: Problem with Push (Greg Bacon)
Re: Problem with Push (Larry Rosler)
Re: Problem with Push (Tad McClellan)
Questions on Win32::Process module <jalil@corp.home.net>
Re: reading a text file backwards (Larry Rosler)
reading data file <eisengre@atx.com>
REGEXP HELP! <myszewsk@cse.buffalo.edu>
Re: REGEXP HELP! <t-armbruster@ti.com>
SLOC <nicolae_giurescu@srtelecom.com>
Sorry if this is FAQ-ing obvious <caf0013@my-dejanews.com>
Re: Sorry if this is FAQ-ing obvious <swarren@www.wwwdotorg.org>
Re: Sorting is too slow for finding top N keys... - BEN (Gregory Snow)
Re: Sorting is too slow for finding top N keys... (Gregory Snow)
Re: string concatination ? <swarren@www.wwwdotorg.org>
Re: string concatination ? <ebohlman@netcom.com>
Re: sub return undef @array ? <aqumsieh@matrox.com>
Re: un esprit lisp dans un corps C <d-edwards@uchicago.edu>
Re: URGENT perldoc Sybase::* does nothing <mpeppler@peppler.org>
Re: What am I doing wrong? <gary@rdss.com>
Re: What am I doing wrong? (Larry Rosler)
Re: What am I doing wrong? <jdporter@min.net>
Re: What am I doing wrong? (I R A Aggie)
Re: What am I doing wrong? <swarren@www.wwwdotorg.org>
Re: What am I doing wrong? (Larry Rosler)
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 12 May 1999 19:52:13 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: PERLFUNC: eval - catch exceptions or compile code
Message-Id: <373dda99.5141199@news.skynet.be>
[posted and mailed]
Tom Christiansen wrote:
>NAME
> eval - catch exceptions or compile code
I feel that something is missing from this title. eval() does not only
compile code, it *executes* it too.
Plus, exceptions are caught even when code is compiled.
Bart.
------------------------------
Date: 12 May 1999 14:33:31 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: PERLFUNC: eval - catch exceptions or compile code
Message-Id: <m1zp3aezx0.fsf@halfdome.holdit.com>
>>>>> "Bart" == Bart Lateur <bart.lateur@skynet.be> writes:
Bart> [posted and mailed]
Bart> Tom Christiansen wrote:
>> NAME
>> eval - catch exceptions or compile code
Bart> I feel that something is missing from this title. eval() does not only
Bart> compile code, it *executes* it too.
Oh yeah?
$coderef = eval 'sub { print "This code is not executed, merely compiled" }';
:-)
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me
------------------------------
Date: 12 May 1999 20:05:36 GMT
From: gbacon@itsc.uah.edu (Greg Bacon)
Subject: Re: Problem with Push
Message-Id: <7hcmug$o7u$3@info2.uah.edu>
In article <7hcgs3$70j$1@goblin.uunet.ca>,
"Elliot Slater" <eslater@frinc.com> writes:
: #!/usr/local/bin/perl
:
: # Check Perl version
: require 5.003;
:
: ($AuthLoc) = @ARGV;
:
: #This stuff converts [ back to space because the path may contain spaces and
: #replaces \ with \\ for the push
: $_ = "$AuthLoc";
: s/\[/ /g;
: s/\\/\\\\/g;
: $AuthLoc = $_;
:
: BEGIN {
: push(@INC, $AuthLoc);
: }
:
: AuthLoc is getting passed as a path. eg: c:\test\temp\
: It works fine if I replace the push with: push(@INC, 'c:\\test\\temp\\');
BEGIN blocks execute at compile time, so it executes *before* the code
that computes $AuthLoc (even though it's lexically after). Move the
code to compute $AuthLoc inside your BEGIN block, and you should be
happy.
Greg
--
Why do people give each other flowers? To celebrate various important
occasions, they're killing living creatures? Why restrict it to plants?
"Sweetheart, let's make up. Have this deceased squirrel."
-- Jerry Seinfeld
------------------------------
Date: Wed, 12 May 1999 13:26:21 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Problem with Push
Message-Id: <MPG.11a383ccc2d214b2989a49@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <7hcgs3$70j$1@goblin.uunet.ca> on Wed, 12 May 1999 14:21:48 -
0400, Elliot Slater <eslater@frinc.com> says...
...
> #This stuff converts [ back to space because the path may contain spaces and
> #replaces \ with \\ for the push
> $_ = "$AuthLoc";
Don't quote simple variables!
> s/\[/ /g;
> s/\\/\\\\/g;
> $AuthLoc = $_;
I learned this trick from TomC:
for ($Authloc) {
tr/[/ /; # Faster and cleaner than s/// for single characters.
s/\\/\\\\/g;
}
> BEGIN {
> push(@INC, $AuthLoc);
> }
>
> AuthLoc is getting passed as a path. eg: c:\test\temp\
> It works fine if I replace the push with: push(@INC, 'c:\\test\\temp\\');
The problem is tht the push() takes place before the rest of the code.
That's what 'BEGIN { }' means.
You'll make your life simpler and your code cleaner if you just use
forward slashes instead of those damned backslashes. ("But I didn't
know that worked!" Best=kept secret of Windows/DOS!).
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 12 May 1999 12:31:14 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Problem with Push
Message-Id: <icach7.dog.ln@magna.metronet.com>
Elliot Slater (eslater@frinc.com) wrote:
: I am having a problem getting a push to work correctly. Here is the code
: snippet:
: #!/usr/local/bin/perl
You should have enabled warnings with -w there...
: ($AuthLoc) = @ARGV;
That is a strange way to get the first argument.
The "usual ways" are:
$AuthLoc = $ARGV[0]; # get first arg
or
$AuthLoc = shift; # get first arg and remove it from @ARGV
: #This stuff converts [ back to space because the path may contain spaces and
: #replaces \ with \\ for the push
: $_ = "$AuthLoc";
The double quotes serve no purpose, other than to confuse people.
: s/\[/ /g;
: s/\\/\\\\/g;
: $AuthLoc = $_;
You don't need to copy, change, copy back. You can change it
where it is:
$AuthLoc =~ s/\[/ /g; # tr/// is Much Better for this
$AuthLoc =~ s/\\/\\\\/g;
: BEGIN {
: push(@INC, $AuthLoc);
: }
: AuthLoc is getting passed as a path. eg: c:\test\temp\
The BEGIN block happens at _compile_ time.
Your other code happens at _run_ time.
$AuthLoc has not been given any value when you use it in
the BEGIN block.
You are pushing undef into @INC.
: It works fine if I replace the push with: push(@INC, 'c:\\test\\temp\\');
Because that does not require any runtime computations.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 12 May 1999 20:57:46 GMT
From: "Jalil Feghhi" <jalil@corp.home.net>
Subject: Questions on Win32::Process module
Message-Id: <926542665.966616@zeppelin.svr.home.net>
I could not find much info on this module in the help pages. Does anybody
know:
1. Is it possible to get more information from the process object returned
from the Create method? For example, is there a way to get the process ID
from this object.
2. If the answer to 1 is no, is there anyway to save the process object away
to disk and read it back later?
Any info/help is much appreciated.
-Jalil
------------------------------
Date: Wed, 12 May 1999 12:32:38 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: reading a text file backwards
Message-Id: <MPG.11a3773260774b82989a43@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <7hca27$bs4$1@nnrp1.deja.com> on Wed, 12 May 1999 16:25:49
GMT, vinh.bui@medstat.com <vinh.bui@medstat.com> says...
> I am using the following to read a text file from the top line down:
> open (x, $file);
> while ($line=<x>) {
> print $line; }
> close (x);
>
> Can I read the text file from the last line up?
Sure. Use the reverse() function if the file fits conveniently into
memory, as others have said.
But for the huge log files that people want to read from the end, there
are better ways ('better' in that they work :-). TomC has published
them here and in the Ram, section 8.4. Uri Guttman is on vacation, or
he would tell you about his Backwards.pm module. Look in DejaNews if
these are necessary to solve your problem.
PS: A few comments on your tiny snippet:
Don't use all-lower-case filehandles. Use '-w' to find out why. Always
check the result of 'open()'. Defaults are neat:
print while <FILE>;
Can't get any neater than that! (Some might have said 'print <FILE>;',
but that reads the whole file into memory first.)
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 12 May 1999 16:56:29 -0400
From: "Peter Eisengrein" <eisengre@atx.com>
Subject: reading data file
Message-Id: <SVl_2.4539$IE2.169051@news15.ispnews.com>
How do I process data in a file that is constantly incrementing (another
program is regularly writing to this file) without re-reading the entire
file each time, i.e. how do I process only the new data?
Thanks.
------------------------------
Date: Wed, 12 May 1999 15:24:13 -0400
From: "Jaroslaw M. Myszewski" <myszewsk@cse.buffalo.edu>
Subject: REGEXP HELP!
Message-Id: <Pine.GSO.3.96.990512152228.25928B-100000@yeager.cse.Buffalo.EDU>
Howdy, I need some help with parsing and regexps
I'm parsing text file and putting whole line of text into an array slot.
So whole array holds text
from each text line I need to extract and store all (one or more)
substings
that begin with __ and end with ' or " or > or =
how do I do that?
I tried something like that
foreach $variab (@Hold)
{
$variab !~ s/__(.*)(\"|\'|\=|\>)
}
but instead of returing that substring it reuturned its complement
(all that are NOT like regexp )
I tried using =~ operator with same result
Thanx
Jarek
------------------------------
Date: Wed, 12 May 1999 16:46:58 -0500
From: "Tim Armbruster" <t-armbruster@ti.com>
Subject: Re: REGEXP HELP!
Message-Id: <qJm_2.21$401.2304@dfw-service1.ext.raytheon.com>
Jaroslaw M. Myszewski wrote in message ...
>
>Howdy, I need some help with parsing and regexps
Post your actual code, but keep it to 40 lines or less.
Try explaining your problem better.
------------------------------
Date: Wed, 12 May 1999 21:06:43 GMT
From: "Nicolae Giurescu" <nicolae_giurescu@srtelecom.com>
Subject: SLOC
Message-Id: <01be9cbb$51ffd250$430314ac@dv-677>
I am looking for a Perl script to count the Source Lines of Code in C++
files that are organized into hierarchical directory structures. Should
anybody can give me some hints on where to find such a script or what would
be a correct approach to develop one, I would greatelly appreciate it. When
it comes to Perl, I'm a newbee :)
Thanks a lot,
Nicolae Giurescu
------------------------------
Date: Wed, 12 May 1999 21:00:43 GMT
From: caf <caf0013@my-dejanews.com>
Subject: Sorry if this is FAQ-ing obvious
Message-Id: <7hcq5n$poc$1@nnrp1.deja.com>
Hi,
Sorry if this is in the FAQ, or obvious, or a usual topic of discussion
but I write heaps of shell scripts with a little reporting ( but not a
lot ), but a lot of accessing/running binary programs.
I'd like to learn perl via practice, but most of my pattern matching,
replacing, reporting I do by 'sed' or 'awk'.
What I'd like to know is the overhead of using the perl 'system',
rather than running a program from within a shell script, the script
has to fork and exec the program, does perl do the same thing ( hence
no overhead, as a shell script does the same thing ) or something
different ....
--
All thought welcome,
caf
com.yahoo@caf0013
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: Wed, 12 May 1999 21:15:52 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: Sorry if this is FAQ-ing obvious
Message-Id: <ccm_2.610$6x6.631@news.rdc1.sfba.home.com>
caf <caf0013@my-dejanews.com> wrote in message
news:7hcq5n$poc$1@nnrp1.deja.com...
> Hi,
>
> Sorry if this is in the FAQ, or obvious, or a usual topic of discussion
> but I write heaps of shell scripts with a little reporting ( but not a
> lot ), but a lot of accessing/running binary programs.
>
> I'd like to learn perl via practice, but most of my pattern matching,
> replacing, reporting I do by 'sed' or 'awk'.
>
> What I'd like to know is the overhead of using the perl 'system',
> rather than running a program from within a shell script, the script
> has to fork and exec the program, does perl do the same thing ( hence
> no overhead, as a shell script does the same thing ) or something
> different ....
It depends how you system. Perl might fork/exec off an entire new shell to
process your request, or might just fork/exec the command directly. It
depends on whether you have shell meta-characters in you command and how
you call system...
In any case, it's much the same efficiency as a shell.
Read 'perldoc perlfunc' and search for 'system LIST' for the full details.
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: 12 May 1999 20:12:28 GMT
From: snow@biostat.washington.edu (Gregory Snow)
Subject: Re: Sorting is too slow for finding top N keys... - BENCH
Message-Id: <7hcnbc$mu6$1@nntp6.u.washington.edu>
In article <wk1zgmwevp.fsf@turangalila.harmonixmusic.com>,
Dan Schmidt <dfan@harmonixmusic.com> wrote:
>I'm glad to see that Marko sped up his quickselect by switching to
>median-of-three to deal with degenerate input; intuitively, quickselect
>seems like it should be The Right Way, so it was upsetting to see it
>performing badly.
>From some quick reading that I did, it seems that the quick select
method does well if you are interested in sorting a large portion of
the array (though still small compared to the total length), but for
just the portion at the extreemes, it is not as efficient because it
spends a lot of time partitioning the area that we are not interested
in. If we become interested in larger portions of the array, I expect
that quicke select will start doing better compared to larry's
method. (there may be some other good optimizations, like replacing
the median of 3 starting point with the second largest of 10).
--
-------------------------------------------------------------------------------
Gregory L. Snow | MATZ'S RULE REGARDING MEDICATIONS:
(Greg) | A drug is that substance which, when injected
snow@biostat.washington.edu | into a rat, will produce a scientific report.
------------------------------
Date: 12 May 1999 20:03:19 GMT
From: snow@biostat.washington.edu (Gregory Snow)
Subject: Re: Sorting is too slow for finding top N keys...
Message-Id: <7hcmq7$v02$1@nntp6.u.washington.edu>
While the comparisons of qselect and extreme, sorting are interesting,
and probably best for general cases, here is one more idea to throw
into the ring:
If I remember right, you said somewhere that your array has a bunch of
duplicate values. What if you first loaded them into another hash
with your values as keys and the counts as values, i.e.
for ( values %hash ) { $newhash{$_}++ }
then sort the keys of the newhash (lot simpler sorting problem)
@sorted = sort keys %newhash;
Then convert from that result back to your original hash,
below is an example script that useses of hash of hashes to also keep
the associated keys:
#!/home/bin/perl -w
@hash{'a'..'zz'} = map {sprintf "%d", $_ / 10} 1..702;
$n=25; # how many to return
while (($key, $value) = each %hash) {
push @{$newhash{$value}{'keys'}}, $key;
$newhash{$value}->{'count'}++
}
@sorted = sort {$b <=> $a} keys %newhash;
for $val (@sorted) {
for ( @{$newhash{$val}->{'keys'}}) {
last if ++$count > $n;
print "$_: $val\n";
}
}
I would expect this method to do better when there is quite a bit of
duplication in your values (but, the overhead of creating the newhash
may overshadow the gains, I haven't benchmarked it).
--
-------------------------------------------------------------------------------
Gregory L. Snow | MATZ'S RULE REGARDING MEDICATIONS:
(Greg) | A drug is that substance which, when injected
snow@biostat.washington.edu | into a rat, will produce a scientific report.
------------------------------
Date: Thu, 13 May 1999 06:24:26 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: string concatination ?
Message-Id: <ueu_2.705$6x6.1053@news.rdc1.sfba.home.com>
<tvn007@my-dejanews.com> wrote in message
news:7hdp7h$kd6$1@nnrp1.deja.com...
> Problem:
> $line[1] = 010;
> Want the output to be:
> $line[1] = xxx010;
>
> for ($i = 0 ; $i < $number_of_x ; $i++){
> $line[1] = x.$line[1]
> }
The inner line of the loop probably should be:
> $line[1] = 'x'.$line[1]
i.e. you need to quote the string. I assume you're using 'perl -w' and
'use strict'?
But, what you want is the x operator (confusingly enough!!!)
'string' x $count
gives 'string' repeated $count times all concatenated together.
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: Thu, 13 May 1999 06:21:00 GMT
From: Eric Bohlman <ebohlman@netcom.com>
Subject: Re: string concatination ?
Message-Id: <ebohlmanFBnqz1.Jtu@netcom.com>
tvn007@my-dejanews.com wrote:
: Would someone help me with this problem ?
: Problem:
: $line[1] = 010;
Since there are no quotes around 010, it will be interpreted as a number,
an octal number in fact.
: Want the output to be:
: $line[1] = xxx010;
: My solution:
: for ($i = 0 ; $i < $number_of_x ; $i++){
: $line[1] = x.$line[1]
: }
If you had used the -w flag and 'use strict;' perl would have complained
about your bareword x.
: ------------------------------------
: since $number_of_x could be thousand and I do not want goes
: through the for loop thousand of times.
: Would someone give be a better or more elegant solution ?
Take a look in perlop for an operator named, believe it or not, 'x'. It
replicates strings for you.
------------------------------
Date: Wed, 12 May 1999 13:53:47 -0400
From: Ala Qumsieh <aqumsieh@matrox.com>
Subject: Re: sub return undef @array ?
Message-Id: <x3ywvyenphw.fsf@tigre.matrox.com>
Eric The Read <emschwar@rmi.net> writes:
> Well, I'm 0 for 2 so far this week. I coulda sworn perldoc perlsub said
> it was an array. Checking again, I see it doesn't. Oh well. All die.
> How embarassing.
I made the same mistake one day. That's why I remember it ;-)
> > No. The array really looks like (undef).
>
> I thought you said it was a list. :^)
I was talking about the contents of @x, which is an array.
> > my @x = retundef() || ();
>
> Well, several other people pointed out that you can just "return;" or
> "return ();". Is it Friday yet?
TMTOWTDI.
Ala
------------------------------
Date: Wed, 12 May 1999 19:55:05 GMT
From: Darrin Edwards <d-edwards@uchicago.edu>
Subject: Re: un esprit lisp dans un corps C
Message-Id: <tg4slinjvq.fsf@noise.bsd.uchicago.edu>
Tom Christiansen <tchrist@mox.perl.com> writes:
[slight snips]
> (in a Monty Pythonesque voice from the Holy Grail...)
> Bring out your closures!
> Bring out your closures!
> Bring out your closures!
"I'm not out of scope yet..." "Yes you are."
Darrin
------------------------------
Date: Wed, 12 May 1999 21:40:44 +0000
From: Michael Peppler <mpeppler@peppler.org>
Subject: Re: URGENT perldoc Sybase::* does nothing
Message-Id: <3739F55C.BF4943D5@peppler.org>
Laurent de Lasteyrie wrote:
>
> I have nothing when i use "perldoc Sybase::Sybperl". I should have a doc
> umentation
> but the only one that works is "perldoc Sybase::BCP". Does anybody got a
> ny information
> about the content of this documentation, or at least where i can find so
> me informations
> and example about Sybperl.
The documentation is in pod/sybperl.pod.
This will be fixed in a future release...
Michael
--
Michael Peppler -||- Data Migrations Inc.
mpeppler@peppler.org -||- http://www.mbay.net/~mpeppler
Int. Sybase User Group -||- http://www.isug.com
Sybase on Linux mailing list: ase-linux-list@isug.com
------------------------------
Date: Wed, 12 May 1999 16:19:55 -0400
From: Gary Ebert <gary@rdss.com>
To: Jared Hecker <jared@pandora.planet.net>
Subject: Re: What am I doing wrong?
Message-Id: <3739E264.C1A42DA2@rdss.com>
Jared Hecker wrote:
>
> Hi, all -
>
> I am writing a script that must, in part, get the date of a file's last
> 'touch'. I wrote the following:
>
> #!/usr/bin/perl
>
> @filelist=<>;
> $counter == 0;
> while(@filelist) {
> chop($filelist[$counter]);
> print STDOUT ($filelist[$counter],"\n");
> open(LIOGFILE,$filelist[$counter]) || die("aarrgghh!");
> while($input=<LIOGFILE>) {
> $position =index($input,"PS");
> chop($position);
> $output = substr($input,$position-13,12);
> print STDOUT ($output,"\n");
> }
> }
> close(LIOGFILE);
>
> Running, it chokes on the "open(LIOGFILE)" line:
>
> [HDV01]: ls -l PSPPY*.log |testdate.pl
> -rw-r--r-- 1 oracle dba 1502 May 11 09:03 PSPPYBLD_.log
> aarrgghh! at testdate.pl line 9, <> chunk 94.
>
> Can anyone tell me why?
yes you are trying to open a file named:
"-rw-r--r-- 1 oracle dba 1502 May 11 09:03 PSPPYBLD_.log"
I tried to figure out what you were doing in your script but I was
unsuccessful. I have no idea why you are even trying to open the file at all
especially when you apparently have the data you need via <>.
Regardless here is a script that will do what you want (or at least what I
think you want :-).
#!/usr/bin/perl
use strict;
my $logDir = "/usr/home/gary"; # make sure that you put the correct path here
# to the directory that contains the log files
my @time;
opendir (DIR, $logDir) or # open the directory that contains the log files
die "Could not open $logDir\n";
while ($_ = readdir DIR) { # get the first file
if (/PSPPY\S*\.log/) { # if this file matches PSPPY*.log
@time = stat $_; # get the file stats
print "Last touched date for $_ is:\t$time[9] seconds since the epoch\n";
# the ninth element that stat returns is the last modify time of the file
# in seconds since the epoch
}
}
# converting seconds since the epoch to a Month Day Year Hour Min etc. is
# an exercise left for the student. (Hint read the FAQ)
Hope this helps!
Gary
>
> TIA -
>
> Regards,
> jh
--
Gary Ebert Operations Administrator
Voice: (301) 428-2115 Comtech Mobile Datacom Corporation
Fax: (301) 428-1004 19540 Amaranth Drive
Pager: (800) 777-4681 PIN: 3981842 Germantown, MD 20875-2126
------------------------------
Date: Wed, 12 May 1999 13:17:22 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: What am I doing wrong?
Message-Id: <MPG.11a381af14d04db2989a47@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <7hcfld$895@jupiter.planet.net> on 12 May 1999 18:01:17 GMT,
Jared Hecker <jared@pandora.planet.net> says...
...
> open(LIOGFILE,$filelist[$counter]) || die("aarrgghh!");
...
> Running, it chokes on the "open(LIOGFILE)" line:
>
> [HDV01]: ls -l PSPPY*.log |testdate.pl
> -rw-r--r-- 1 oracle dba 1502 May 11 09:03 PSPPYBLD_.log
> aarrgghh! at testdate.pl line 9, <> chunk 94.
>
> Can anyone tell me why?
None of us can tell you why, but I'll wager that printing the $!
variable in your diagnostic will help Perl tell you why.
Other comments on your code (besides lack of '-w' and 'use strict;', of
course):
> @filelist=<>;
> $counter == 0;
> while(@filelist) {
> chop($filelist[$counter]);
> print STDOUT ($filelist[$counter],"\n");
> open(LIOGFILE,$filelist[$counter]) || die("aarrgghh!");
$counter is set to 0 and never incremented. This is wrong, as well as
very unperlish! Use 'foreach my $file (@filelist) {' or similar.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 12 May 1999 20:43:51 GMT
From: John Porter <jdporter@min.net>
Subject: Re: What am I doing wrong?
Message-Id: <7hcp67$ong$1@nnrp1.deja.com>
In article <7hcfld$895@jupiter.planet.net>,
jared@pandora.planet.net (Jared Hecker) wrote:
>
> I am writing a script that must, in part, get the date of a file's
last
> 'touch'. I wrote the following:
>
> #!/usr/bin/perl
>
> @filelist=<>;
> $counter == 0;
> while(@filelist) {
> chop($filelist[$counter]);
> print STDOUT ($filelist[$counter],"\n");
> open(LIOGFILE,$filelist[$counter]) || die("aarrgghh!");
> while($input=<LIOGFILE>) {
> $position =index($input,"PS");
> chop($position);
> $output = substr($input,$position-13,12);
> print STDOUT ($output,"\n");
> }
> }
> close(LIOGFILE);
>
> Running, it chokes on the "open(LIOGFILE)" line:
>
> [HDV01]: ls -l PSPPY*.log |testdate.pl
> -rw-r--r-- 1 oracle dba 1502 May 11 09:03 PSPPYBLD_.log
> aarrgghh! at testdate.pl line 9, <> chunk 94.
>
> Can anyone tell me why?
Sure: you're doing everything wrong that can possibly be done wrong.
my $date_starts_at = 41; # on my system it's 42
for ( `ls -l` ) {
chomp;
length > $date_starts_at or next;
my $date_and_file = substr( $_, $date_starts_at );
my $date = substr( $date_and_file, 0, 12 );
my $filename = substr( $date_and_file, 13 );
print "'$filename' was last updated at '$date'\n";
}
There are better ways, but this is pretty close to what you already had.
Here's a similar way which indexes into an array of ls -l lines, rather
than taking each one directly into $_.
my @lines = `ls -l`;
chomp @lines; # kill the newlines
shift @lines; # discard the header.
for my $index ( 0 .. $#lines ) {
my $date_and_file = substr( $lines[$index], $date_starts_at );
my $date = substr( $date_and_file, 0, 12 );
my $filename = substr( $date_and_file, 13 );
print "'$filename' was last updated at '$date'\n";
}
Here's an even better way, which uses regular expressions to figure
out where the date and filename fields are, although it does assume
the date field is 12 characters wide:
for ( `ls -l` ) {
if ( my( $date, $filename ) = /(.{12}) (\S+)$/ ) {
print "'$filename' was last updated at '$date'\n";
}
}
--
John Porter
Put it on a plate, son. You'll enjoy it more.
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: 12 May 1999 20:48:24 GMT
From: fl_aggie@thepentagon.com (I R A Aggie)
Subject: Re: What am I doing wrong?
Message-Id: <slrn7jjqdl.pj1.fl_aggie@stat.fsu.edu>
On 12 May 1999 18:01:17 GMT, Jared Hecker <jared@pandora.planet.net>, in
<7hcfld$895@jupiter.planet.net> wrote:
+ #!/usr/bin/perl
^^
add a ' -w' to the end of that...
+ open(LIOGFILE,$filelist[$counter]) || die("aarrgghh!");
+ Running, it chokes on the "open(LIOGFILE)" line:
+ aarrgghh! at testdate.pl line 9, <> chunk 94.
+ Can anyone tell me why?
Probably for a very good reason, perhaps "no such file or directory"
or "permission denied". Maybe for a very stupid reason. Unfortunately,
I don't have the ESP module installed, so I can't say for certain. Something
you can do is to re-write your open thusly [originally on one line, chopped
to observe an 80 column formatting, but it should work OK as-is]:
open(LIOGFILE,$filelist[$counter]) || die "Failed to
open:\n$filelist[$counter] at $counter\n\tbecause of $!";
And see what the results of $! are...
James
------------------------------
Date: Wed, 12 May 1999 20:57:32 GMT
From: "Stephen Warren" <swarren@www.wwwdotorg.org>
Subject: Re: What am I doing wrong?
Message-Id: <0Xl_2.605$6x6.652@news.rdc1.sfba.home.com>
John Porter <jdporter@min.net> wrote in message
news:7hcp67$ong$1@nnrp1.deja.com...
> In article <7hcfld$895@jupiter.planet.net>,
> jared@pandora.planet.net (Jared Hecker) wrote:
> >
> > I am writing a script that must, in part, get the date of a file's
> last
> > 'touch'. I wrote the following:
Depending on whether you actually want the formatted text from ls, or
whether you just want to know when the last modification was, you could
just stat the file:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($filename);
print "$filename last modified at $mtime\n" ;
Of course, the data format is different, so you'll have to manipulate it
into a string if you need it that way...
perldoc perlfunc
search for "stat EXPR"
--
Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
mailto:swarren@techhouse.com http://www.techhouse.com/
mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
MIME, S/MIME and HTML mail are acceptable
------------------------------
Date: Wed, 12 May 1999 15:04:31 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: What am I doing wrong?
Message-Id: <MPG.11a39aca48d98fd2989a4f@nntp.hpl.hp.com>
[Posted and a courtesy copy mailed.]
In article <0Xl_2.605$6x6.652@news.rdc1.sfba.home.com> on Wed, 12 May
1999 20:57:32 GMT, Stephen Warren <swarren@www.wwwdotorg.org> says...
> > In article <7hcfld$895@jupiter.planet.net>,
> > jared@pandora.planet.net (Jared Hecker) wrote:
> > >
> > > I am writing a script that must, in part, get the date of a file's
> > last
> > > 'touch'. I wrote the following:
>
> Depending on whether you actually want the formatted text from ls, or
> whether you just want to know when the last modification was, you could
> just stat the file:
>
> ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
> $atime,$mtime,$ctime,$blksize,$blocks)
> = stat($filename);
$mtime = (stat $filename)[9];
is lots easier to type.
> print "$filename last modified at $mtime\n" ;
That's useful if a result like 'last modified at 987654321' is useful.
> Of course, the data format is different, so you'll have to manipulate it
> into a string if you need it that way...
perlfunc -f localtime
perlfunc -f sprintf
The -M function gives a useful result immediately (days before now).
> perldoc perlfunc
> search for "stat EXPR"
perlfunc -f stat
> --
> Stephen Warren, Snr Systems Engineer, Technology House, San Francisco
> mailto:swarren@techhouse.com http://www.techhouse.com/
> mailto:swarren@wwwdotorg.org http://www.wwwdotorg.org/
> MIME, S/MIME and HTML mail are acceptable
If you put an invisible trailing space after the two dashes, newsreaders
won't automatically include your signature in the reply. Isn't that an
odd convention?
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
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 5646
**************************************