[8407] in Perl-Users-Digest
Perl-Users Digest, Issue: 2025 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 4 19:07:20 1998
Date: Wed, 4 Mar 98 16:01:34 -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 Wed, 4 Mar 1998 Volume: 8 Number: 2025
Today's topics:
Re: Q: How to <lsj@bnl.gov>
Re: Q: How to (Matthew Cravit)
regexp bourhis@qcd.th.u-psud.fr
remote registry on NT <elynt@ml.com>
safe file modification strategy (w/flock, new_tmpfile, (O'Shaughnessy Evans)
Re: Summing Up Array Values - How Do I? (Tom Rokicki)
Re: Summing Up Array Values - How Do I? <merlyn@stonehenge.com>
Re: The -w switch (was Re: better way to do this?) <jdporter@min.net>
Re: use system or backticks?? (Mike Ellwood)
Variable not returning values (Monee C. Kidd)
Re: Variable not returning values <uri@sysarch.com>
Re: What is PERL? Learn JAVA instead? <evan@garrett.hpl.hp.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 4 Mar 1998 17:17:09 -0500
From: "Lance" <lsj@bnl.gov>
Subject: Re: Q: How to
Message-Id: <6dkjt7$qhg$1@sun20.ccd.bnl.gov>
This data file is like this, they are all small files:
.1 14.650 .2155123 .0
.1 14.70001 .2140399 .0
.1 14.75001 .2124197 .0
.1 14.80001 .2106409 .0
.1 14.85001 .20869 .0
.1 14.90001 .2065667 .0
.1 14.95001 .2042442 .0
.1 15.00001 .2017127 .0
As you can see, the length is varying. I have no idea what the record size's
going to be. So how to get the last record is still a problem. What I do now
is:
open(FF,$input) || die "Couldn't open $input: $!\n";
$i=0;
while (<FF>) {
($a1[$i],$a2[$i],$a3[$i],$a4[$i])= split(" ",$_);
$$i++;
}
print "third field of last record is $a3[$i]\n";
But this seems awkward and it generates three useless
variables:a1[],a2[],a4[].
Any ideas?
...Lance
lsj@bnl.gov
John Porter wrote in message <34FDB857.73AD@min.net>...
>Lance wrote:
>>
>> open(FF,+ACQ-input) +AHwAfA- die +ACI-Couldn't open +ACQ-input:
+ACQAIQBc-n+ACIAOw-
>> +ACQ-i+AD0-0+ADs-
>> while (+ADw-FF+AD4-) +AHs-
>>[etc.]
>
>Now this is just way too much.
>Microsoft Outlook Express? MimeOLE?
>plonk.
>
>John Porter
------------------------------
Date: 4 Mar 1998 15:12:26 -0800
From: mcravit@shell3.ba.best.com (Matthew Cravit)
Subject: Re: Q: How to
Message-Id: <6dkn4q$9p5$1@shell3.ba.best.com>
In article <6dkjt7$qhg$1@sun20.ccd.bnl.gov>, Lance <change AT into @> wrote:
> This data file is like this, they are all small files:
>.1 14.650 .2155123 .0
>.1 14.70001 .2140399 .0
>.1 14.75001 .2124197 .0
>.1 14.80001 .2106409 .0
>.1 14.85001 .20869 .0
>.1 14.90001 .2065667 .0
>.1 14.95001 .2042442 .0
>.1 15.00001 .2017127 .0
>As you can see, the length is varying. I have no idea what the record size's
>going to be. So how to get the last record is still a problem.
Something like the following should work (TMTOWTDI, of course, but this is
certainly one solution):
open (DATFILE, "datafile") or die "Could not open data file: $!\n";
while (<DATFILE>) {
chomp;
my @entries = split /\s+/;
push @data, \@entries;
}
print "Third field of last record is: ", $data[$#data]->[2], "\n";
exit 0;
Hope this helps.
/MC
--
Matthew Cravit, N9VWG | Experience is what allows you to
E-mail: mcravit@best.com (home) | recognize a mistake the second
mcravit@net.com (work) | time you make it.
------------------------------
Date: Wed, 04 Mar 1998 16:34:53 -0600
From: bourhis@qcd.th.u-psud.fr
Subject: regexp
Message-Id: <6dkkt5$4bc$1@nnrp1.dejanews.com>
I want to remove the sequence '\n', five spaces and one character which is not
whitespace in a string. I tried the regexp /\n {5}\S/ but it does not work.
Strangely (for me), the regexp / {5}\S/ and /\n {5}/ works.
Luc Bourhis
Laboratoire de Physique Theorique et des Hautes Energies
(Theoretical Physics and High Energies)
Universite de Paris XI, France
e-mail : bourhis@qcd.th.u-psud.fr
tel : 33 (0)1 69 15 82 15
fax : 33 (0)1 69 15 82 87
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
------------------------------
Date: Wed, 04 Mar 1998 16:38:44 -0500
From: Ely Mansour <elynt@ml.com>
Subject: remote registry on NT
Message-Id: <34FDC9E3.81494503@ml.com>
Has anybody been able to connect to a remote registry on NT ?
Ely
------------------------------
Date: 4 Mar 1998 23:10:50 GMT
From: shaug@gromit.callamer.com (O'Shaughnessy Evans)
Subject: safe file modification strategy (w/flock, new_tmpfile, and syscopy?)
Message-Id: <6dkn1q$7rt$1@ha1.rdc1.occa.home.com>
Keywords: flock, new_tmpfile, syscopy, open
Hi, I've got a question on the safest strategy for modifying a file.
I need to edit a RADIUS users file; it's critical for authentication
of PPP users, so I want to minimize the risk of hosing it, even in
case of a system crash while the program is running. My current
algorithm is like this:
open $file in seekable append mode
exlusively flock $file
make backup of $file w/syscopy
rewind $file
create $tmp w/new_tmpfile
read $file, process, write to $tmp
rewind and truncate $file
rewind and flush $tmp
read from $tmp -> write to $file
close $file
close $tmp
The syscopy is done in case of failure between the truncation of $file
and the writing of $tmp to $file.
This pseudocode reflects the safest way I can think of to modify $file.
I'm no expert on Unix's or Perl's I/O systems, so I'm wondering if there
are any problems people see with this, or any places where I'm being
horribly inefficient.
Since the functions that implement the pseudocode are pretty short, I'll
append them, in case it makes things any easier.
sub _setup_files
{
my ($self, $file) = @_;
my $backup = "$file.bak";
my $existed = -f $file;
# Readable, writeable; create it if necessary
my $IN = new IO::File "+>>$file"
or carp("Couldn't open $file: $!"), return 0;
# Lock our input file
flock($IN, LOCK_EX)
or carp("Couldn't lock $file: $!"), close $IN, return 0;
# Create a backup file, in case of catastrophic system failure
# while we're having all this fun. But only if the file existed
# before we started.
if ($existed) {
File::Copy::syscopy($file, $backup)
or carp("Couldn't copy $file to $backup: $!"), close $IN, return 0;
}
# Now that we have exclusive control of the input file, make sure we're
# at the beginning.
seek $IN, 0, 0;
my $TMP = IO::File::new_tmpfile
or carp("Couldn't create temp file: $!"), close $IN, return 0;
return ($IN, $TMP);
}
# We should have new content in $TMP, and old content in $IN. So remove
# the old content and copy $TMP's stuff there, then remove $TMP.
sub _cleanup_files
{
my ($file, $IN, $TMP) = @_;
seek $IN, 0, 0; truncate $IN, 0;
seek $TMP, 0, 0; $TMP->flush;
print $IN $_ while <$TMP>;
close $IN or carp("Error closing `$file': $!"), return 0;
close $TMP or carp("Error closing temp file: $!"), return 0;
return 1;
}
Regards, and thanks for any input :^)
--
- O'Shaughnessy Evans
- http://www.callamerica.net/shaug/
------------------------------
Date: 4 Mar 1998 12:46:01 -0800
From: rokicki@cello.hpl.hp.com (Tom Rokicki)
Subject: Re: Summing Up Array Values - How Do I?
Message-Id: <6dkei9$hmg@cello.hpl.hp.com>
Of course, after I posted I found a few improvements and typos:
#!/usr/local/bin/perl -w
use strict ;
print &{
# Note: no eval, map, foreach, or names visible outside the expression!
# Our array summation function expression:
&{sub{my $x=$_[0];
my $z=sub{my($p)=$_[0];&{$x}(sub{&{&{$p}($p)}(@_)})};
&{$z}($z)}} # Y combinator
(sub{my $f=$_[0];sub{@_&&(shift @_)+(&{$f}(@_))}}) # summer
# applied to:
} (99, 34, 110, 33) , "\n" ;
-tom
------------------------------
Date: 04 Mar 1998 16:35:12 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: Uri Guttman <uri@sysarch.com>
Subject: Re: Summing Up Array Values - How Do I?
Message-Id: <8c4t1e58sv.fsf@gadget.cscaper.com>
>>>>> "Uri" == Uri Guttman <uri@sysarch.com> writes:
Uri> unless you enforce the correct use of this trademarked name it will get
Uri> abused by the public. have your lawyers (and i KNOW you have some)
Uri> contact all misusers of the term and threaten them with massive legal
Uri> action :-)
No thanks. I've had all the legal action I want for *ten* lifetimes.
(no smiley)
print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 180 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details
--
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: Wed, 04 Mar 1998 17:12:24 -0500
From: John Porter <jdporter@min.net>
Subject: Re: The -w switch (was Re: better way to do this?)
Message-Id: <34FDD1C8.1182@min.net>
Jon Drukman wrote:
>
> Abigail <abigail@fnx.com> wrote:
>
> : As for the speed hit, I hardly believe
> : the hit is significant (show me extended Benchmark results to prove me
> : wrong). And if an insignificant speed hit is important for your users,
> : you probably shouldn't write Perl anyway.
>
> i haven't benchmarked it, but even if it's only .001% we get enough
> traffic per day that that would be a worthwhile thing to eliminate.
I will reiterate what Abigail said:
If an insignificant speed hit is important for your users,
you probably shouldn't write Perl anyway.
%.001 is insignificant.
If it matters anyway, Perl is the wrong choice.
John Porter
------------------------------
Date: 4 Mar 1998 20:50:55 GMT
From: mwe@unixfe.cc.rl.ac.uk (Mike Ellwood)
Subject: Re: use system or backticks??
Message-Id: <6dkerf$ti0@newton.cc.rl.ac.uk>
Martien Verbruggen (mgjv@comdyn.com.au) wrote:
: Depends. Do you need the output from the command? Then use backticks.
: Do you need the return value, use system.
: > explain the difference between the backticks and using "system". I looked
: > in the Blue camel book but did not get as clear an answer as I needed.
: perldoc -f system
: perldoc perlop
: (look for "`STRING`")
: The difference is clearly discussed in those parts of the
: documentation.
One of the reasons I prefer Korn shell to Bourne shell is that
it offers an alternative to backticks (namely $(STRING) for
command output capture;
the problem with backticks is that (on some terminals/charactersets
at least, and when printed) they can often be indistinguishable
from single quotes, especially when tired eyes have been poring
over the screen/output all night looking for bugs. The Korn shell
method may be slightly more long-winded, but it is very obvious
what the code is trying to do. (and you can still use backticks if you
insist).
I was therefore a little disappointed to find them cropping up in
Perl for this purpose, and much relieved to find an alternative,
namely:
qx/STRING/
Not sure yet if there are any gotchas with this..
--
Mike.Ellwood@rl.ac.uk
------------------------------
Date: Wed, 04 Mar 1998 17:24:38 -0500
From: MCK@CH0CO.COM (Monee C. Kidd)
Subject: Variable not returning values
Message-Id: <MCK-0403981724390001@as1-15.apk.net>
OK, I've tried to read through this newsgroup before I asked my
question (hard to do when there are +2k posts, and you're not exactly sure
what you're looking for), and I have looked through the FAQ (see above
concern). What I did get from reading through earlier posts is that folks
are ... uh... less than thrilled with newbies asking FAQs. Being a FAQ
maintainer in another life, I understand the frustration, but I hope my
question either a) isn't a FAQ, or b) isn't met with the fires of hell.
I've tried to RTFM first.
With that said....I have a (stupid) little script that takes a file,
finds a specifc (non blank) line and puts it into a separate variable,
then prints out that line. My problem is if I put the print line inside
the while(), I get a result. If I put it outside the while(), I get
nothing back.
This is the script. It's probably not the most efficient way to do what
I want, but hey, it works:
$Count = 1;
$TheFile = '<sample.txt';
open(INFILE, $TheFile);
while(<INFILE>) {
$Line = $_ ;
chomp($Line);
if($Line eq '') {
$Count--;
}
if($Count == 2) {
$Yes = $Line;
for ($Yes) {
s/^\s+//;
s/\s+$//;
}
$Yes =~ s/(\w+)/\u\L$1/g;
print $Yes # This line prints the correct value
}
$Count++;
}
print $Yes; # This line prints nothing
print $Count; # I put this here to test, it works as expected
close(INFILE);
I need the $Yes variable to stay set, because this script will eventually
become a subroutine in a larger script. What have I missed in my readings?
Why does $Count stay set while $Yes does not?
--
Monee C. Kidd - mck@choco.com - http://www.choco.com
What matters is not what you call me, but what I answer to
(When replying, make sure my address contains no numbers.)
------------------------------
Date: 04 Mar 1998 18:19:44 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Variable not returning values
Message-Id: <x7btvmxcvj.fsf@sysarch.com>
MCK@CH0CO.COM (Monee C. Kidd) writes:
> I've tried to RTFM first.
that is why i will answr you query, my child.
> This is the script. It's probably not the most efficient way to do what
> I want, but hey, it works:
if it works why are you asking for help? :-)
>
> $Count = 1;
> $TheFile = '<sample.txt';
> open(INFILE, $TheFile);
always check the result of open
> while(<INFILE>) {
> $Line = $_ ;
> chomp($Line);
> if($Line eq '') {
> $Count--;
i think you should put a next here or the counter will go down and then
up. this is probably your bug. you should also show the sample data for
scripts like this which are dependent on exact file structures
> }
> if($Count == 2) {
$Count could equal 2 on a blank line due the bug above.
> $Yes = $Line;
> for ($Yes) {
> s/^\s+//;
> s/\s+$//;
> }
this part may work but is VERY bad code. why not just do the
substitutions like you do on the next line? all this did was obscure the
fact that $Yes was being trimmed.
also your caps in variables is driving me crazy!
> $Yes =~ s/(\w+)/\u\L$1/g;
> print $Yes # This line prints the correct value
> }
> $Count++;
> }
again should you count up after you counted down? it looks like more of
an algorithmic problem than a perl problem.
> print $Yes; # This line prints nothing
> print $Count; # I put this here to test, it works as expected
> close(INFILE);
>
>
> I need the $Yes variable to stay set, because this script will eventually
> become a subroutine in a larger script. What have I missed in my readings?
> Why does $Count stay set while $Yes does not?
$Yes is set. it is set to the null string (most probably).
hope this helps.
read more perl stuff to improve your coding style.
uri
--
Uri Guttman SYStems ARCHitecture and Software Engineering
uri@sysarch.com Have Perl, Will Hack
http://www.sysarch.com (781) 643-7504 x*2 FAX: (781) 643-2710
Try the Best Search Engine on the Net --------> http://www.northernlight.com
------------------------------
Date: 04 Mar 1998 13:57:44 -0800
From: Evan Kirshenbaum <evan@garrett.hpl.hp.com>
Subject: Re: What is PERL? Learn JAVA instead?
Message-Id: <v9hogzmm84n.fsf@garrett.hpl.hp.com>
John Porter <jdporter@min.net> writes:
> Frank wrote:
> >
> > There's stuff you can do in PERL that would be difficult, or
> > impossible, to do with JAVA. And vice versa.
>
> (I bet you knew this was coming:)
> Name two things that you can do in Java, which would be
> impossible, or even difficult (by general agreement), to
> do in Perl.
I do a lot more Perl programming than Java programming (and both are
dwarfed recently by C++), but I think I've done enough of both to be
qualified to hold an opinion.
That being said, two things that are easy to do in Java but
exceedingly difficult to do in Perl include:
- Work with cyclic structures. (AFAIK, Perl still uses
reference counting rather than true garbage collection, and so
cannot reclaim them automatically.)
- Anything involving multiple threads of control.
I understand that this last will *finally* be addressed in Perl in the
near future..
There are a number of things that are more convenient in Java but
doable in Perl (reflection, exception handling, switch statements,
...). And, of course, Perl wins hands down on *many* fronts (regular
expressions, anonymous subroutines/closures, hashes, resizable arrays,
variable-argument functions, ...), which is why it's generally my
first choice, especially for small programs that need to be developed
quickly.
--
Evan Kirshenbaum +------------------------------------
HP Laboratories |When you rewrite a compiler from
1501 Page Mill Road, Building 1U |scratch, you sometimes fix things
Palo Alto, CA 94304 |you didn't know were broken.
| Larry Wall
kirshenbaum@hpl.hp.com
(650)857-7572
http://www.hpl.hp.com/personal/Evan_Kirshenbaum/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 2025
**************************************