[31252] in Perl-Users-Digest
Perl-Users Digest, Issue: 2497 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 2 18:18:28 2009
Date: Thu, 2 Jul 2009 11:51:10 -0700 (PDT)
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, 2 Jul 2009 Volume: 11 Number: 2497
Today's topics:
Effect of redo on m//g <google@markginsburg.com>
Re: Effect of redo on m//g <blgl@hagernas.com>
Re: Effect of redo on m//g <ben@morrow.me.uk>
Re: Effect of redo on m//g <derykus@gmail.com>
Re: Effect of redo on m//g <ben@morrow.me.uk>
Re: Effect of redo on m//g <google@markginsburg.com>
Re: Effect of redo on m//g <ben@morrow.me.uk>
Re: Effect of redo on m//g <derykus@gmail.com>
Re: Effect of redo on m//g <derykus@gmail.com>
Re: Effect of redo on m//g <ben@morrow.me.uk>
Re: FAQ 4.57 What happens if I add or remove keys from <ben@morrow.me.uk>
Re: FAQ 4.57 What happens if I add or remove keys from <uri@stemsystems.com>
how to write a script to only process one depth directo <robertchen117@gmail.com>
Re: how to write a script to only process one depth dir <josef.moellers@ts.fujitsu.com>
Re: how to write a script to only process one depth dir <jurgenex@hotmail.com>
imshow equivalent function <benjamin.couillard@gmail.com>
Multi threading in perl. <prathap.bgl@gmail.com>
Re: Perl WTF <sandy@atuc.net.removethistld>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 30 Jun 2009 13:14:48 -0700 (PDT)
From: Mark <google@markginsburg.com>
Subject: Effect of redo on m//g
Message-Id: <d94c0c24-c240-4659-a09a-770d2e4ca2b7@z14g2000yqa.googlegroups.com>
Can someone please explain to me how the redo is affecting the match
results?
Case 1 - without redo:
use strict;
use warnings;
my $bail = 0;
while () {
'ab' =~ /(.)/g;
print "\$1=", defined($1) ? $1 : 'undef', "\n";
last if $bail++ > 5;
# redo;
}
Output:
$1=a
$1=b
$1=b
$1=a
$1=b
$1=b
$1=a
Case 2 - with redo:
use strict;
use warnings;
my $bail = 0;
while () {
'ab' =~ /(.)/g;
print "\$1=", defined($1) ? $1 : 'undef', "\n";
last if $bail++ > 5;
redo;
}
Output:
$1=a
$1=b
$1=undef
$1=a
$1=b
$1=undef
$1=a
It seems to me that redo should have no affect in the second case
below. It appears that when pos is past the end of the string, $1 is
not set in Case 1 but it is reset to undef in Case 2.
------------------------------
Date: Wed, 01 Jul 2009 00:53:22 +0200
From: Bo Lindbergh <blgl@hagernas.com>
Subject: Re: Effect of redo on m//g
Message-Id: <h2e51b$hua$1@aioe.org>
In article <d94c0c24-c240-4659-a09a-770d2e4ca2b7@z14g2000yqa.googlegroups.com>,
Mark <google@markginsburg.com> wrote:
> use strict;
> use warnings;
>
> my $bail = 0;
>
> while () {
> 'ab' =~ /(.)/g;
> print "\$1=", defined($1) ? $1 : 'undef', "\n";
> last if $bail++ > 5;
> # redo;
> }
An unsuccessful match doesn't modify $1 and friends, so you should
in general only look at their values after a successful match.
/Bo Lindbergh
------------------------------
Date: Wed, 1 Jul 2009 00:14:30 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Effect of redo on m//g
Message-Id: <mriqh6-6a01.ln1@osiris.mauzo.dyndns.org>
Quoth Bo Lindbergh <blgl@hagernas.com>:
> In article <d94c0c24-c240-4659-a09a-770d2e4ca2b7@z14g2000yqa.googlegroups.com>,
> Mark <google@markginsburg.com> wrote:
>
> > use strict;
> > use warnings;
> >
> > my $bail = 0;
> >
> > while () {
> > 'ab' =~ /(.)/g;
> > print "\$1=", defined($1) ? $1 : 'undef', "\n";
> > last if $bail++ > 5;
> > # redo;
> > }
>
> An unsuccessful match doesn't modify $1 and friends, so you should
> in general only look at their values after a successful match.
While this is correct, it doesn't answer the question 'why, then, does
redo appear to be resetting $1 to undef?'. I think it's a bug in perl,
and IIRC it's a known (and probably won't-fix) bug. 'redo' causes perl
to forget which pattern was the last one you matched, and since the $N
are actually stored in the pattern this causes $1 to revert to the last
successful pattern match *before* the loop that was redone. Try adding
"f" =~ /(f)/;
before the loop to see what I mean.
Ben
------------------------------
Date: Tue, 30 Jun 2009 16:42:00 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Effect of redo on m//g
Message-Id: <01347fb3-2001-4341-a23b-3309db279cee@y7g2000yqa.googlegroups.com>
On Jun 30, 4:14=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Bo Lindbergh <b...@hagernas.com>:
>
>
>
> > In article <d94c0c24-c240-4659-a09a-770d2e4ca...@z14g2000yqa.googlegrou=
ps.com>,
> > =A0Mark <goo...@markginsburg.com> wrote:
>
> > > use strict;
> > > use warnings;
>
> > > my $bail =3D 0;
>
> > > while () {
> > > =A0 =A0 'ab' =3D~ /(.)/g;
> > > =A0 =A0 print "\$1=3D", defined($1) ? $1 : 'undef', "\n";
> > > =A0 =A0 last if $bail++ > 5;
> > > # =A0 redo;
> > > }
>
> > An unsuccessful match doesn't modify $1 and friends, so you should
> > in general only look at their values after a successful match.
>
> While this is correct, it doesn't answer the question 'why, then, does
> redo appear to be resetting $1 to undef?'. I think it's a bug in perl,
> and IIRC it's a known (and probably won't-fix) bug. 'redo' causes perl
> to forget which pattern was the last one you matched, and since the $N
> are actually stored in the pattern this causes $1 to revert to the last
> successful pattern match *before* the loop that was redone. Try adding
>
> =A0 =A0 "f" =3D~ /(f)/;
>
> before the loop to see what I mean.
>
Interesting. Maybe, it's considered a feature :)
(And just to be pedantic, the OP shouldn't try to
get the "full monty" anyway if the match fails)
--
Charles DeRykus
------------------------------
Date: Wed, 1 Jul 2009 01:54:50 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Effect of redo on m//g
Message-Id: <qnoqh6-to01.ln1@osiris.mauzo.dyndns.org>
Quoth "C.DeRykus" <derykus@gmail.com>:
> On Jun 30, 4:14 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Bo Lindbergh <b...@hagernas.com>:
> > > In article
> <d94c0c24-c240-4659-a09a-770d2e4ca...@z14g2000yqa.googlegroups.com>,
> > > Mark <goo...@markginsburg.com> wrote:
> >
> > > > use strict;
> > > > use warnings;
> >
> > > > my $bail = 0;
> >
> > > > while () {
> > > > 'ab' =~ /(.)/g;
> > > > print "\$1=", defined($1) ? $1 : 'undef', "\n";
> > > > last if $bail++ > 5;
> > > > # redo;
> > > > }
> >
> > > An unsuccessful match doesn't modify $1 and friends, so you should
> > > in general only look at their values after a successful match.
> >
> > While this is correct, it doesn't answer the question 'why, then, does
> > redo appear to be resetting $1 to undef?'. I think it's a bug in perl,
> > and IIRC it's a known (and probably won't-fix) bug. 'redo' causes perl
> > to forget which pattern was the last one you matched, and since the $N
> > are actually stored in the pattern this causes $1 to revert to the last
> > successful pattern match *before* the loop that was redone. Try adding
> >
> > "f" =~ /(f)/;
> >
> > before the loop to see what I mean.
> >
>
> Interesting. Maybe, it's considered a feature :)
Hmm... kind-of treating 'redo' as though it's going back in time? I
suspect if it is a feature it's accidental.
> (And just to be pedantic, the OP shouldn't try to
> get the "full monty" anyway if the match fails)
Both you and Bo have said that, and it *simply* *isn't* *true*. The
match variables being preserved across failing matches is an entirely
supported feature, useful in situations like
s/(foo)/bar/;
s/(baz)/quux/;
my $replaced = $1;
In any case, the failing match is a red herring. 'redo' causes the same
'forgetting' of successful matches during the redone section:
"f" =~ /(f)/;
my $last;
while (1) {
"a" =~ /(a)/ unless $last;
say $1;
last if $last++;
redo;
}
Both matches there succeed, but the redo causes the 'last successful
match' to revert to the /f/ before the loop.
Ben
------------------------------
Date: Tue, 30 Jun 2009 18:31:51 -0700 (PDT)
From: Mark <google@markginsburg.com>
Subject: Re: Effect of redo on m//g
Message-Id: <9a383dc7-44bc-4b91-8bd8-b7d9bc0660c6@y7g2000yqa.googlegroups.com>
And.. the execution doesn't even need to reach the redo for it to have
an effect.
"f" =~ /(f)/;
my $last;
while (1) {
"a" =~ /(a)/ unless $last;
say $1;
last if $last++;
next; # <<----------
redo;
}
------------------------------
Date: Wed, 1 Jul 2009 03:42:49 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Effect of redo on m//g
Message-Id: <92vqh6-8r31.ln1@osiris.mauzo.dyndns.org>
Quoth Mark <google@markginsburg.com>:
> And.. the execution doesn't even need to reach the redo for it to have
> an effect.
>
> "f" =~ /(f)/;
> my $last;
>
> while (1) {
> "a" =~ /(a)/ unless $last;
> say $1;
> last if $last++;
> next; # <<----------
> redo;
> }
<sigh> I'm just being stupid here :). The match variables are
*dynamically scoped* (each successful match effectively 'local's them):
"a" =~ /(.)/;
# $1 eq "a"
{
"b" =~ /(.)/;
# $1 eq "b"
}
# $1 eq "a"
What's odd here is not that they are being reset, but that just going
round a loop again *doesn't* reset them. It seems anything out of the
ordinary, even just a 'continue { 1 }' block, is enough to bring the
normal scoping back.
Ben
------------------------------
Date: Tue, 30 Jun 2009 20:36:54 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Effect of redo on m//g
Message-Id: <978d69e0-cae3-49c5-8da8-c7e39458bef9@k15g2000yqc.googlegroups.com>
On Jun 30, 5:54=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "C.DeRykus" <dery...@gmail.com>:
> ...
> > (And just to be pedantic, the OP shouldn't try to
> > =A0 get the "full monty" anyway if the match fails)
>
> Both you and Bo have said that, and it *simply* *isn't* *true*. The
> match variables being preserved across failing matches is an entirely
> supported feature, useful in situations like
>
> =A0 =A0 s/(foo)/bar/;
> =A0 =A0 s/(baz)/quux/;
>
> =A0 =A0 my $replaced =3D $1;
>
Sigh... the OP was actually pointing out that redo
didn't preserve the failed matches. And I see your
subsequent post exposes the real scoping issue
mentioned in perlre:
The numbered match variables ($1, $2, $3, etc.)
and the related punctuation set ($+, $& ,$`, $',
and $^N ) are all dynamically scoped until the end
of the enclosing block or until the next successful
match, whichever comes first. (See ""Compound
Statements"" in perlsyn.)
--
Charles DeRykus
------------------------------
Date: Wed, 1 Jul 2009 14:28:25 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Effect of redo on m//g
Message-Id: <eb3498ef-7e7c-4b20-a3ba-133b9835b9d7@k8g2000yqn.googlegroups.com>
On Jun 30, 5:54=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "C.DeRykus" <dery...@gmail.com>:
>
> ...
> Both you and Bo have said that, and it *simply* *isn't* *true*. The
> match variables being preserved across failing matches is an entirely
> supported feature, useful in situations like
>
> =A0 =A0 s/(foo)/bar/;
> =A0 =A0 s/(baz)/quux/;
>
> =A0 =A0 my $replaced =3D $1;
>
I haven't used this feature but, it occurs
to me, with the scoping issues you found,
the replacement might be a bit dodgy:
"f" =3D~ /(f)/;
..
$_ =3D "one two three";
s/(foo)/bar/;
s/(baz)/quux/;
my $replaced =3D $1; # =3D "f"
> ...
--
Charles DeRykus
------------------------------
Date: Wed, 1 Jul 2009 23:50:56 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Effect of redo on m//g
Message-Id: <gr5th6-r691.ln1@osiris.mauzo.dyndns.org>
Quoth "C.DeRykus" <derykus@gmail.com>:
> On Jun 30, 5:54 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth "C.DeRykus" <dery...@gmail.com>:
> > ...
> > Both you and Bo have said that, and it *simply* *isn't* *true*. The
> > match variables being preserved across failing matches is an entirely
> > supported feature, useful in situations like
> >
> > s/(foo)/bar/;
> > s/(baz)/quux/;
> >
> > my $replaced = $1;
> >
>
> I haven't used this feature but, it occurs
> to me, with the scoping issues you found,
> the replacement might be a bit dodgy:
>
> "f" =~ /(f)/;
> ..
> $_ = "one two three";
> s/(foo)/bar/;
> s/(baz)/quux/;
> my $replaced = $1; # = "f"
Yes, you can only use this when you are sure *one* of the set will
match.
Another example would be
perl -ne'/section: (.*)/ or print "$1: $_"'
Ben
------------------------------
Date: Wed, 1 Jul 2009 14:05:55 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 4.57 What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <ji3sh6-d661.ln1@osiris.mauzo.dyndns.org>
Quoth PerlFAQ Server <brian@stonehenge.com>:
>
> 4.57: What happens if I add or remove keys from a hash while iterating over it?
>
> (contributed by brian d foy)
>
> The easy answer is "Don't do that!"
I thought mjd'd fixed this entry?
> If you iterate through the hash with each(), you can delete the key most
> recently returned without worrying about it. If you delete or add other
> keys, the iterator may skip or double up on them since perl may
> rearrange the hash table. See the entry for "each()" in perlfunc.
Deleting *any* key is safe, and has the obvious result. If you've seen
it already, you won't see it again; if you haven't, you won't see it at
all. Deleting an entry never causes Perl hashes to rehash, so it won't
affect any other keys.
Adding a key is not safe. It may cause the hash to rehash, which will
change the order of all the keys.
Ben
------------------------------
Date: Wed, 01 Jul 2009 11:53:30 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 4.57 What happens if I add or remove keys from a hash while iterating over it?
Message-Id: <87r5x0wgat.fsf@quad.sysarch.com>
>>>>> "BM" == Ben Morrow <ben@morrow.me.uk> writes:
BM> Quoth PerlFAQ Server <brian@stonehenge.com>:
>>
>> 4.57: What happens if I add or remove keys from a hash while iterating over it?
>>
>> (contributed by brian d foy)
>>
>> The easy answer is "Don't do that!"
BM> I thought mjd'd fixed this entry?
>> If you iterate through the hash with each(), you can delete the key most
>> recently returned without worrying about it. If you delete or add other
>> keys, the iterator may skip or double up on them since perl may
>> rearrange the hash table. See the entry for "each()" in perlfunc.
BM> Deleting *any* key is safe, and has the obvious result. If you've seen
BM> it already, you won't see it again; if you haven't, you won't see it at
BM> all. Deleting an entry never causes Perl hashes to rehash, so it won't
BM> affect any other keys.
BM> Adding a key is not safe. It may cause the hash to rehash, which will
BM> change the order of all the keys.
minor nit: it MAY change the order of all the keys. it could rehash the
keys to the same order but you can't expect that. your point of deleting
any key is valid. it would make no sense to rehash on that anyway. you
could be deleting a key from an overflow list in a bucket so why rehash
then?
so i agree this FAQ needs to be edited to clarify the safety of deleting
keys in a loop while emphasizing the error of adding keys in a loop.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 2 Jul 2009 01:14:47 -0700 (PDT)
From: "robertchen117@gmail.com" <robertchen117@gmail.com>
Subject: how to write a script to only process one depth directories
Message-Id: <3258e70f-1933-414a-815c-7bf317189f88@j32g2000yqh.googlegroups.com>
my directory /server has many servers under it, each /server/server1/
is a directory. I only want to find files like and add/change some
contents:
/server/server1/Defaults
/server/server2/Defaults
......
/server/servern/Defaults
and process it, for any other sub directories just ignore them. How to
do this in perl?
Please help me, thanks.
------------------------------
Date: Thu, 02 Jul 2009 10:35:06 +0200
From: Josef Moellers <josef.moellers@ts.fujitsu.com>
Subject: Re: how to write a script to only process one depth directories
Message-Id: <h2hrfq$u8n$1@nntp.fujitsu-siemens.com>
robertchen117@gmail.com wrote:
> my directory /server has many servers under it, each /server/server1/
> is a directory. I only want to find files like and add/change some
> contents:
>
> /server/server1/Defaults
> /server/server2/Defaults
> ......
> /server/servern/Defaults
>
> and process it, for any other sub directories just ignore them. How to
> do this in perl?
> Please help me, thanks.
The obvious question is: What have you tried so far and where did your
code not meet your expectations?
Hint:
foreach (</server/server*/Defaults>) {
# ...
}
Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
------------------------------
Date: Thu, 02 Jul 2009 05:19:07 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: how to write a script to only process one depth directories
Message-Id: <b69p45pfcoh33khsa35mbqkmltr25c8ak0@4ax.com>
"robertchen117@gmail.com" <robertchen117@gmail.com> wrote:
>my directory /server has many servers under it, each /server/server1/
>is a directory. I only want to find files like and add/change some
>contents:
>
>/server/server1/Defaults
>/server/server2/Defaults
>......
>/server/servern/Defaults
>
>and process it, for any other sub directories just ignore them. How to
>do this in perl?
One way:
glob() do get the file list, for() to loop through it, open() to access
the file, <> to get the content, print() to write the processed content
back, close() to, well, close the file again.
Which area do you have problems withj?
------------------------------
Date: Sat, 27 Jun 2009 13:49:09 -0700 (PDT)
From: Benjamin Couillard <benjamin.couillard@gmail.com>
Subject: imshow equivalent function
Message-Id: <c919dcb2-6161-4d4e-8e25-a6057109ee9f@3g2000yqk.googlegroups.com>
Hi,
I'd like to know if there's an equivalent function to Matlab's imshow
in Perl modules? Basically imshow allows you to plot 2d arrays as
images, either RGB or grayscale. I was able to save a 2d-array as a
PNG and then open it with Perl, but I think it's a little cumbersome.
So do you guys know any modules that could perform this function as
easy as in Matlab?
Best regards.
Benjamin
------------------------------
Date: Sun, 28 Jun 2009 11:23:12 -0700 (PDT)
From: Prathap <prathap.bgl@gmail.com>
Subject: Multi threading in perl.
Message-Id: <df06bb16-e30a-42fe-802a-1fe1000dddd9@h11g2000yqb.googlegroups.com>
Hi :
I want to learn multi threading in Perl. Can someone point to good
referances for the same?
Thanks a lot in advance.
Regards,
Prathap
------------------------------
Date: Fri, 26 Jun 2009 20:01:07 GMT
From: "SandyTipper" <sandy@atuc.net.removethistld>
Subject: Re: Perl WTF
Message-Id: <7O91m.32351$Db2.31986@edtnps83>
"Ben Morrow" <ben@morrow.me.uk> wrote in message
news:jb7qg6-l9q.ln1@osiris.mauzo.dyndns.org...
>
> Quoth Lech <newsy-lech@totrzebawyciac.gazeta.sky.pl>:
>>
>> I am collecting perl WTFs (What the f***)- not poorly written code, by
>> WTFs
>> in perl itself.
>>
>> Did you have that experience, when you programmed in perl and something
>> worked other way than you thought it should? Or other perl strange
>> things?
>
> STDOUT->autoflush(1);
>
> seems pretty WTF to me, especially when you compare it with
>
> sub STDOUT { ["WTF"] }
>
> STDOUT->[0];
>
> Also
>
> use IO::Handle;
>
> sub foo { 1; }
>
> foo IO::Handle->new;
>
> and every other situation where dative-object syntax sticks its nose in
> where it's not wanted.
>
> Ben
>
OK. I'll bite. I've been teaching myself perl for 9 minths by tackling a big
perl-based project from scratch by myself with no mentors, so I only know
what I need to use. I understand the STDOUT->autoflush(1) (just barely), but
all the rest:: WTF??
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 2497
***************************************