[10751] in Perl-Users-Digest
Perl-Users Digest, Issue: 4351 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 3 11:07:32 1998
Date: Thu, 3 Dec 98 08:01:32 -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 Thu, 3 Dec 1998 Volume: 8 Number: 4351
Today's topics:
Re: usage of $_ within nested loops dave@mag-sol.com
Re: usage of $_ within nested loops <uri@sysarch.com>
Re: usage of $_ within nested loops (Larry Rosler)
Re: When does CLOSE not FLUSH? <rra@stanford.edu>
Re: Why is "... @foo ..." occasionally a syntax error? (Bart Lateur)
Re: Why is "... @foo ..." occasionally a syntax error? <jc@eddie.mit.edu>
Re: Why is "... @foo ..." occasionally a syntax error? <jc@eddie.mit.edu>
wwwboard question - file locking <nospam23_skidoo@geocities.com>
Re: wwwboard question - file locking (Steve Linberg)
Re: wwwboard question - file locking <nospam23_skidoo@geocities.com>
Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 03 Dec 1998 14:46:01 GMT
From: dave@mag-sol.com
Subject: Re: usage of $_ within nested loops
Message-Id: <746878$v5h$1@nnrp1.dejanews.com>
In article <36666565.4C2CFA71@bbn.hp.com>,
Thomas Janke <thomas_janke@bbn.hp.com> wrote:
>
> --------------F383E47B48D95E36768FDB60
> Content-Type: text/plain; charset=iso-8859-1
> Content-Transfer-Encoding: 8bit
Please stop your news client posting MIME messages. Usenet is a text medium.
> How will Perl handle the usage of "$_" within nested loops.
>
> An example:
>
> while(<FILEHANDLE>)
> {
> # Do something with $_
>
> foreach $i (@another_list)
> {
>
> # Do something else with $_
>
> }
> }
Did you try it? What did it do?
The example you give will work fine. You would, however, have a problem if you
tried to do this:
while (<FILE>)
{
foreach (@array)
{
# Value for $_ from while loop has been lost.
}
}
hth,
Dave...
--
Magnum Solutions Ltd: <http://www.mag-sol.com/>
London Perl M[ou]ngers: <http://london.pm.org/>
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: 03 Dec 1998 10:07:50 -0500
From: Uri Guttman <uri@sysarch.com>
Subject: Re: usage of $_ within nested loops
Message-Id: <x73e6xxnzt.fsf@sysarch.com>
>>>>> "d" == dave <dave@mag-sol.com> writes:
d> The example you give will work fine. You would, however, have a
d> problem if you tried to do this:
d> while (<FILE>)
d> {
d> foreach (@array)
d> {
d> # Value for $_ from while loop has been lost.
d> }
d> }
another reason why i generally don't use $_ as a default variable
(except where i must in map and grep). it leaves too much dangling. i
like explicit vars so i know what i am munging. it also helps with the
dcoumentation as you can refere to that var and not wonder which $_ or
where is $_ set.
uri
--
Uri Guttman ----------------- SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire ---------------------- Perl, Internet, UNIX Consulting
uri@sysarch.com ------------------------------------ http://www.sysarch.com
The Best Search Engine on the Net ------------- http://www.northernlight.com
------------------------------
Date: Thu, 3 Dec 1998 07:17:22 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: usage of $_ within nested loops
Message-Id: <MPG.10d04b614dfb374698993c@nntp.hpl.hp.com>
[Posted to comp.lang.perl.misc and copy mailed.]
In article <36666565.4C2CFA71@bbn.hp.com> on Thu, 03 Dec 1998 11:18:14
+0100, Thomas Janke <thomas_janke@bbn.hp.com> says...
> How will Perl handle the usage of "$_" within nested loops.
$_ is a 'global' variable. Its value is not affected by nested blocks,
or other scopes such as subroutine bodies, for example.
You can create a local copy of $_ within a block, by declaring:
local $_; #And you can initialize it here also.
This new $_ is accessible in the block, or nested blocks, or
subroutines called within the block or nested blocks. Then when the
block is exited the previous value is restored.
See "Temporary Values via local()" in perlsub.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 03 Dec 1998 03:58:54 -0800
From: Russ Allbery <rra@stanford.edu>
Subject: Re: When does CLOSE not FLUSH?
Message-Id: <yld861v3lt.fsf@windlord.stanford.edu>
Bart Lateur <bart.lateur@skynet.be> writes:
> Russ Allbery wrote:
>> That means that if you have two simultaneous writers, it's still
>> possible for their data to be intermixed although each will write to
>> the end of the file when it does its write.
> Aha!
> That means that indeed we do need the lock.
Yup, unless you're sure that your data will only be one write, and you
don't mind other stuff being written around it. syswrite() can help there
if one wants to go that route.
But the seek() definitely isn't needed, regardless of whether the lock is.
It doesn't hurt; it's just a no-op.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: Thu, 03 Dec 1998 11:49:03 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: Why is "... @foo ..." occasionally a syntax error?
Message-Id: <366f7a90.11999904@news.skynet.be>
Ala Qumsieh wrote:
>% cat >> temp.pl
>$a = "@a";
>print "A is $a.\n";
>__END__
>
>% perl temp.pl
>In string, @a now must be written as \@a at temp.pl line 1, near "@a"
>Execution of temp.pl aborted due to compilation errors.
>
>Maybe you didn't mean it, but your above statement gave me the
>impression that Perl assumes that @ is literal and continues running
>the program. It doesn't.
It did in Perl 4.
Bart.
------------------------------
Date: Thu, 03 Dec 1998 09:42:11 -0500
From: John Chambers <jc@eddie.mit.edu>
Subject: Re: Why is "... @foo ..." occasionally a syntax error?
Message-Id: <3666A343.27A52936@eddie.mit.edu>
Mark Dominus wrote:
>
> >As far as I can tell, this bug/feature isn't documented in the man pages.
>
> What, other than the detailed explanation of the error message that's
> listed in `perldiag' that other people already quoted?
Um, Here's the relevant passage in its entirety:
In string, @%s now must be written as \@%s
(F) It used to be that Perl would try to guess whether you wanted an
array interpolated or a literal @. It did this when the string was
first used at runtime. Now strings are parsed at compile time, and
of @ must be disambiguated, either by prepending a
backslash to indicate a literal, or by declaring (or using) the array
within the program before the string (lexically). (Someday it will
simply assume that an unbackslashed @ interpolates an array.)
This doesn't contain the slightest hint as to what constitutes an
"ambiguous instances". Nor does it contain any sort of clue that
the compiler wants some (physically) earlier instance of the array.
There is nothing here that can be used to understand why one case
of "... @foo ..." gets this error when a zillion others don't.
> What else do
> you want? The error message took you by surprise; that's a shame; the
> next step should have been to look up the error message in the
> documentation, and if you had done that, you would have found the
> explanation you wanted in just the place it should be.
Oh, <expletive deleted>. So far nobody has pointed to any passage
anywhere in TFM that says (or even implies) that the compiler requires
some earlier use of @foo before it will accept "@foo". As near as
I can tell, this is the rule (though I suppose you're going to tell
me what an idiot I am for drawing such a stupid conclusion, when it's
very obviously something entirely different. OK, if my inference is
not correct, what *is* going here and where is it documented?)
> Most of the time, people program happily without finding out about
> this bizarre and obscure error, because Perl usually guesses correctly
> what you want. Burdening the basic documentation with a detailed
> explanation of a complicated special case that rarely occurs is not
> going to improve the manual, in my opinion.
I'd agree that it rarely occurs. I use "... @foo ..." all over in
my code, and it usually works. Then, every once in a while, it bites
me with this weird error message. The "man perldiag" has an "explanation"
that hasn't ever explained why it decided to bite me. There's no hint
as to why one particular instance is ambiguous while all the others
aren't. So after once again wasting much of a day hunting down the
reason for a background daemon's sudden disappearance, finding this
message in a logfile, and again not making any sense of it, I asked in
the newsgroup. Maybe I should have known better, considering how nasty
c.l.p.m has become in recent years, but I was curious. So now I know
the answer, and I'll go away. (Well, maybe I'll stick around for a day
or two and give answers to a few questions, then go away. I do consider
that the appropriate price for asking questions. ;-)
> >Well, I guess it's one more entry in my archive of perversities in various
> >languages.
>
> If you don't want perversity, you're using the wrong language.
> Consider:
>
> $string =~ /$foo[0-9]/;
I've always loved that one. Isn't that why \d was invented?
> > But the perl gang is way behind.
>
> Maybe you're not looking hard enough?
Nah; I sorta collect language perversities as a hobby. They're fun.
And for a language whose supporters openly describe as a "kitchen sink"
language, you'd expect perl to be full of them. I've been disappointed
at how small my set of perl perversities is, despite my being on the
constant lookout for them, and having used perl rather heavily in the
past few years.
(If you want a real winner of a language, perversity-wise, take a good
look at PL/I. Though if you treasure your sanity, it's probably wiser
to just say "No".)
Bye, bye.
------------------------------
Date: Thu, 03 Dec 1998 10:46:11 -0500
From: John Chambers <jc@eddie.mit.edu>
Subject: Re: Why is "... @foo ..." occasionally a syntax error?
Message-Id: <3666B243.6CDC6AD5@eddie.mit.edu>
Ala Qumsieh wrote:
>
> John Chambers <jc@eddie.mit.edu> writes:
> hmmm... if you had been using the correct pragmas, then you would've
> been forced to declare all your variables.
>
> Remeber these:
> 1) ALWAYS use the -w switch.
> 2) ALWAYS "use strict;"
Well, I use them a lot during debugging, but tend to turn them off for
"production" use. The main reason is that -w causes the web server's
error log to grow rapidly, with every CGI script producing a flood of
"used only once: possible type" and "Use of uninitialized value" messages.
If I knew a way to turn off such spurious message, I'd do so, but if
it's possible, I haven't yet stumbled across it. I consider perl's use
of null/zero default values a very valuable feature. It's true that I
could rid of such messages by inserting code to check for undefined
values and assign a value, and by making sure that there were (dummy)
uses of all variables even when they're not needed. But this would make
my code much larger and less readable. It's an aesthetic judgement,
of course, and my personal aesthetics say that I'd rather have smaller
and more readable code. So I use perl's defaults a lot, and delete
the -w after a pass to check the warnings for anything significant.
> when you declare a variable, you have to define it's nature and its
> scope. The nature is defined by using either "$", "%", "@" before the
> variable name. Its scope is defined by declaring the variables as a
> my() or local() variables.
It's still not clear what constitutes a "declaration" in perl. In the
case at issue, I've found that the problem can be fixed by merely adding
@foo;
at the start of the module. Is this a "declaration"? Not according
to any definition I know of in any other language, and I can't seem
to find a proper definition of "declaration" in perl's man pages,
except for subs and formats. local() and my() seem somewhat like
declarations, but they also seem to be run-time functions. In any
case, I thought that one of the nice things about perl was that it
didn't have declarations.
> Corollary of Rule #2:
> 2a) (almost) ALWAYS use my() variables. They are faster and make the
> most sense (most probably).
>
> So, the way to declare an array is:
>
> my @foo;
But this would shoot down the whole body of code that I started this
with. The variable in question must be global; adding a my declaration
would hide it from the routine that's trying to get at the data left
there by another routine. There seems to be no "global @foo" sort
of statement in perl. (Or did I miss it?)
> You don't have to allocate memory .. most of the times you also don't
> need to declare. But you should start using Perl the Proper Way (tm).
What about the Perl Mantra, huh? Has it been deprecated?
------------------------------
Date: Thu, 03 Dec 1998 11:56:33 +0000
From: 23_skidoo <nospam23_skidoo@geocities.com>
Subject: wwwboard question - file locking
Message-Id: <36667C6A.7609@geocities.com>
hi,
i've got wwwboard running and am having occasional crashes. after a bit
of testing i found that it crashes when a few people all post at exactly
the same time.
i'm relatively new to perl but have learned quite a lot, haven't crossed
file locking though and i think this may be what i need, if i could lock
access to the main data file and/or the data file with the current
message number in it then i think i could avoid this problem. can anyone
explain how the file lock command works or point me to an online
reference please? what happens to person 2 if person 1 running the
script has locked neccessary files, will person 2 get an error or will
the script just wait until the needed file is unlocked again?
thanks v.much
-23
p.s. remove nospam from my address if you want to mail me direct
------------------------------
Date: Thu, 03 Dec 1998 10:29:00 -0500
From: linberg@literacy.upenn.edu (Steve Linberg)
Subject: Re: wwwboard question - file locking
Message-Id: <linberg-0312981029000001@ltl1.literacy.upenn.edu>
In article <36667C6A.7609@geocities.com>, nospam23_skidoo@geocities.com wrote:
> hi,
>
> i've got wwwboard running and am having occasional crashes. after a bit
> of testing i found that it crashes when a few people all post at exactly
> the same time.
wwwboard does this, and I believe that the problem is more complicated
than simply locking files. It has no facility for handling simultaneous
accesses. However, the documentation for flock() is sitting in your perl
installation (perldoc -f flock).
_____________________________________________________________________
Steve Linberg National Center on Adult Literacy
Systems Programmer &c. University of Pennsylvania
linberg@literacy.upenn.edu http://www.literacyonline.org
------------------------------
Date: Thu, 03 Dec 1998 15:49:51 +0000
From: 23_skidoo <nospam23_skidoo@geocities.com>
Subject: Re: wwwboard question - file locking
Message-Id: <3666B309.1B1@geocities.com>
Steve Linberg wrote:
>
> In article <36667C6A.7609@geocities.com>, nospam23_skidoo@geocities.com wrote:
>
> > hi,
> >
> > i've got wwwboard running and am having occasional crashes. after a bit
> > of testing i found that it crashes when a few people all post at exactly
> > the same time.
>
> wwwboard does this, and I believe that the problem is more complicated
> than simply locking files. It has no facility for handling simultaneous
> accesses. However, the documentation for flock() is sitting in your perl
> installation (perldoc -f flock).
i'm editing my scripts on bbedit on a mac + uploading them to a
webserver, any ideas how i'd get hold of that documentation?
how would one handle simultaneous accesses? i assumed that file locking
would force people to queue or send them back a 'please try again' type
message. if this isn't the case, how do you get perl to run a high
hitrate messageboard? i guess ideally i'd have a database plugged in
somehow but i'd be out of my depth there, can it be done with flat
files?
-23
------------------------------
Date: 12 Jul 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 Mar 98)
Message-Id: <null>
Administrivia:
Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.
If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu.
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 4351
**************************************