[20037] in Perl-Users-Digest
Perl-Users Digest, Issue: 2232 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 28 21:05:39 2001
Date: Wed, 28 Nov 2001 18:05:06 -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: <1006999506-v10-i2232@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 28 Nov 2001 Volume: 10 Number: 2232
Today's topics:
Re: Filehandle Comparing? (Kit)
Re: How do you pronounce "Perl"? (Charles DeRykus)
Re: How do you pronounce "Perl"? <tony_curtis32@yahoo.com>
Re: Must be a better way.. <ahamm@programmer.net>
Re: newbie learning perl (HoboSong)
Re: perl execution ? <bart.lateur@pandora.be>
Re: perl execution ? <flavell@mail.cern.ch>
Re: perl execution ? <bart.lateur@pandora.be>
Re: perl execution ? <flavell@mail.cern.ch>
Re: recursive regexp <ahamm@programmer.net>
Strange behavior in a Perl CGI script (Smiley)
Re: Strange behavior in a Perl CGI script (Randal L. Schwartz)
Re: Strange behavior in a Perl CGI script <invalid_email@www.skeleton-man.f2s.com>
Re: Strange behavior in a Perl CGI script <silentbob@rymon.net>
Re: syswrite on closed socket? (winter7)
Re: syswrite on closed socket? (winter7)
Re: Using CGI.pm to obtain a list of params - Oops! <wsegrave@mindspring.com>
Re: Using CGI.pm to obtain a list of params - Oops! <wsegrave@mindspring.com>
Re: Why isn't grep an l-valued function? <mgjv@tradingpost.com.au>
Re: Why isn't grep an l-valued function? <joe+usenet@sunstarsys.com>
Re: Why isn't grep an l-valued function? <bart.lateur@pandora.be>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 28 Nov 2001 17:34:26 -0800
From: adrook@yahoo.com (Kit)
Subject: Re: Filehandle Comparing?
Message-Id: <e756f30f.0111281734.1e9f8486@posting.google.com>
>
> my @datfiles = glob "$ENV{'BASEFOLDER'}/$ENV{'DATFILES'}/*.dat";
> foreach my $datfile (@datfiles) {
> ...
> }
>
> >Right now I need to compare one variable, $address, to the name of
> >each dat file. How do I do this? I tried:
> >if REGFILE eq $address {
> >#Commands
> >}
> >But perl did not seem to want the filehandle. What can I do?
>
> That doesn't make any sense. What file do you want to read your
> ($email,$password,$name) from? You can compare your $address to each of
> the @datfiles in turn, e.g. with grep(), but I'm sure it won't return
> any positives unless your address includes the full file path and a
> ".dat" extension.
>
> @found = grep { $_ eq $address } @datfiles;
Thanks for the help. I will try working with glob and grep, then use
a pattern match to take out just the name of the dat file to compare
it with the $address.
------------------------------
Date: Thu, 29 Nov 2001 01:08:57 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: How do you pronounce "Perl"?
Message-Id: <GnJF6x.246@news.boeing.com>
In article <m17ksb9c9d.fsf@halfdome.holdit.com>,
Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>..
>Larry says it like "pearl". The other one would sound to me like
>"peril", which amazingly is what FrameMaker always wants to
>spell-correct it as until I tell it "no". :)
There was a Scottish presenter at TPC a couple years
ago who said "peril" instead of "pearl". I had to bite
my tongue to avoid offering a tip about the correct
pronunciation :)
--
Charles DeRykus
------------------------------
Date: Wed, 28 Nov 2001 19:38:00 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: How do you pronounce "Perl"?
Message-Id: <87snayuzk7.fsf@limey.hpcc.uh.edu>
>> On Thu, 29 Nov 2001 01:08:57 GMT,
>> ced@bcstec.ca.boeing.com (Charles DeRykus) said:
> In article <m17ksb9c9d.fsf@halfdome.holdit.com>,
> Randal L. Schwartz <merlyn@stonehenge.com> wrote:
>> .. Larry says it like "pearl". The other one would
>> sound to me like "peril", which amazingly is what
>> FrameMaker always wants to spell-correct it as until I
>> tell it "no". :)
> There was a Scottish presenter at TPC a couple years ago
> who said "peril" instead of "pearl". I had to bite my
> tongue to avoid offering a tip about the correct
> pronunciation :)
Some Scottish speakers insert a vowel between "r" and "l"
in words, e.g. "world" is pronounced as "wo'ruld".
So for them, "peril" is quite acceptable for "perl".
Is this on-topic? :-)
hth
t
--
Oh! I've said too much. Smithers, use the amnesia ray.
------------------------------
Date: Thu, 29 Nov 2001 12:19:23 +1100
From: "Andrew Hamm" <ahamm@programmer.net>
Subject: Re: Must be a better way..
Message-Id: <3c058dcc_1@news.iprimus.com.au>
HoboSong wrote in message
<4fbb9396.0111272352.1d2a5286@posting.google.com>...
>
> Im sorry, I should have been more clear. Im using those regexes as
>'switches' while reading a text file. Each section of the file has a
>completely different set of instructions, so I need to process all the
>lines until I run into a new
>section. How do you say:
>
> &DoThis until ( /YOU SEE THIS/ ) and then &DoDifferent until ( /YOU
>GET HERE/);
>
wellll, assuming that each section is started by a particular line and
stopped by a recogmisable end, you can do this:
&Section1 if /SECTION1/ .. /END SECTION1/;
&Section2 if /SECTION2/ .. /END SECTION2/;
&Section3 if /SECTION3/ .. /END SECTION3/;
I process files which are satisfied with
/^{/ .. /^}/
or
/something/ .. /^end$/
so that's a very nice way of doing things.
If the ending of one section is actually just the starting of the next
section, then either your ending patterns must either include the keys for
all sections (possibly including itself if sections can repeat) or you use a
state variable like you did - hence, not much in the way of savings!
>Oh, and I didnt use chomp because I cant seem to find the right
>combination of "\l\n" or whatever to designate the end of a page, so I
>leave them as is.
>
What's the end of a page? I can't see anything in your messages. If you can
describe that (and maybe go into details about your sections including their
ends) then it will be easier to give useful advice.
--
Space Corps Directive #723
Terraformers are expressly forbidden from recreating Swindon.
-- Red Dwarf
------------------------------
Date: 28 Nov 2001 17:19:44 -0800
From: chrisvano@yahoo.com (HoboSong)
Subject: Re: newbie learning perl
Message-Id: <4fbb9396.0111281719.a956085@posting.google.com>
"Harvey" <darmfam@hotmail.com> wrote in message news:<xlaN7.131568$Yb.34674544@typhoon.tampabay.rr.com>...
> I'm reading "learning Perl" and have tried some of the exercises and have
> had some sucsess runnitn them on Win(NT). But I've had some problems getting
> them to work on a linux OS.
>
> The following hash:
>
> %words =qw(
> me my
> I it
> one two
> be been
> );
>
> Print "$words{me}"
>
> returns the following error message:
>
> "Can't modify not in scalar assignment at hash line (number), near ");"
>
> As mentioned, this works on Win(NT) but not RH Linux. I know there are major
> differences between the two versions. But I expected the basics to work. Can
> anyone give me some direction with this.
>
> Thanks in advance.
change your print line to (pay attention to case and punctuation):
print $words{me} ;
^ ^
small p in print, end with semicolon. Your scripts will work on Linux no
problem, if you add #!/usr/bin/perl -w (for RedHat Linux)
and change
print $words{me}; to: print "$words{me}\n";
------------------------------
Date: Wed, 28 Nov 2001 23:24:21 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: perl execution ?
Message-Id: <2dsa0uoeq7pj2bcnsd31fn0af2cb48p8l1@4ax.com>
WhoKnows wrote:
>> >Can i execute my perl scripts directly saying 'filename.pl' at the
>>command
>> >prompt w/o saying 'perl filename.pl'
>>
>> What platform? Because it largely depends on that.
>
>Apache webserver, OS : red hat linux 6.2
Ah. Yes you can. Either you add this directory, or "." to your PATH (I
would hesitate to do that), or you use
./filename.pl
at the prompt.
Either way, make sure your shebang line (the first line, starts with
"#!") points to the proper perl executable, and set the permissions of
the script to executable for the appropriate people..
--
Bart.
------------------------------
Date: Thu, 29 Nov 2001 00:28:45 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: perl execution ?
Message-Id: <Pine.LNX.4.30.0111290024170.17435-100000@lxplus023.cern.ch>
On Nov 28, WhoKnows inscribed on the eternal scroll:
> > >Can i execute my perl scripts directly saying 'filename.pl' at the
> command
> > >prompt w/o saying 'perl filename.pl'
> >
> > What platform? Because it largely depends on that.
>
> Apache webserver,
But you asked about the command prompt, not about the web server
(which makes it sound more like a stealth CGI question).
Can you ask a more precise question? Let's try taking you at your
word, anyhow, and disregarding the remark about Apache for now.
> OS : red hat linux 6.2
In any flavour of unix, the answer (if Perl has been installed in the
normal way) will be "yes". The reason being that a perl executable
will have been installed into a directory that is normally on your
PATH (to cut a long story short).
If you don't know what that means, then you have some learning to do
about unix-type command prompts - the Perl language group isn't the
place for that - try your redhat tutorials or a general unix manual.
good luck
------------------------------
Date: Wed, 28 Nov 2001 23:53:39 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: perl execution ?
Message-Id: <f2ua0ucnbc4ns0kf359ghk4q6thko9kbs7@4ax.com>
Alan J. Flavell wrote:
>In any flavour of unix, the answer (if Perl has been installed in the
>normal way) will be "yes". The reason being that a perl executable
>will have been installed into a directory that is normally on your
>PATH (to cut a long story short).
???
It's the path to the script that would need to be in the path, provided
it has a proper shebang line (with a full path to perl -- that's why
that one doesn't matter). If not, "./script.pl" would still work, if the
script is in the current directory.
--
Bart.
------------------------------
Date: Thu, 29 Nov 2001 02:21:31 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: perl execution ?
Message-Id: <Pine.LNX.4.30.0111290219210.25567-100000@lxplus023.cern.ch>
On Nov 28, Bart Lateur inscribed on the eternal scroll:
> Alan J. Flavell wrote:
>
> >In any flavour of unix, the answer (if Perl has been installed in the
> >normal way) will be "yes". The reason being that a perl executable
> >will have been installed into a directory that is normally on your
> >PATH (to cut a long story short).
>
> ???
>
> It's the path to the script that would need to be in the path, provided
> it has a proper shebang line (with a full path to perl -- that's why
> that one doesn't matter).
Good call. I seem to have offered an answer to the wrong question.
Sorry.
------------------------------
Date: Thu, 29 Nov 2001 12:42:41 +1100
From: "Andrew Hamm" <ahamm@programmer.net>
Subject: Re: recursive regexp
Message-Id: <3c059340$1_2@news.iprimus.com.au>
Luciano Ponte wrote in message <9u38cm$k9b$1@nreada.inwind.it>...
>I have a file on a single line:
>
>xxxxx(variable text)xxx<prefix>string1<suffix>xxxxx(variable
>text)xxx<prefix>string2<suffix>xxx ... and so on for 20 times.
>
>I need to extract string1, string2 ...string20 and put them on a new file.
>How to do this?
>
Sounds like it may be a good job for split. Is that a very close sample of
data?
I'm going to assume that xxxxx(variable text)xxxxxxxx really means just a
bunch of insignificant stuff, not necessarily with xxx's or ( ) in it?
And I'm going to assume that <prefix> and <suffix> really do exist. Is it
the same prefix and suffix for both parts?
Finally, I'm going to assume that the line is "well-formed" in the sense
that all <prefix> really are followed by a <suffix>
One problem you'll have with a pure regex solution is greediess of a .* part
you might put in there. For example, with this data:
<prefix>string1<suffix>junk<prefix>string2<suffix>
if you apply a pattern
/<prefix>.*<suffix>/ then it will capture everything from the first suffix
to the 2nd suffix. That's probably not what you want ;-)
If you use split you can completely bypass the problem with having to
specify "the bits in between". Split will use your simpler pattern to find
boundaries between bits:
@stuff = split /<prefix>|<suffix>/, $str;
If there really are < and > characters around the suffixes (are you
processing HTML?) then you can combine the pattern:
@stuff = split /<(prefix|suffix)>/, $str;
However, that's a bit defective, because the ( ) used to group the two parts
will seriously affect the results coming out of split. In particular, the
<prefix> and <suffix> will be returned as part of the array, and that's just
noise if you don't want to extract those bits. This happens because ( ) is
used to capture parts of a pattern match, and split takes care to return
them if you act like you want to capture them. The pattern can be 'fixed"
with
/<(?:prefix|suffix)>/
because (?: something ) is special regex syntax saying "group this stuff
into a unit but don't remember it".
If you agree that this final pattern is uglier and barely worth the effort
(after all, it now doesn't save you any typing) then I'd stick with the
original /<prefix>|<suffix>/
Of course, if you really are matching some simple HTML, then usually the
suffix is just /prefix so you can use this pattern:
/<\/?cmd>/
Anyway, I digress slightly.
Split will return all the bits on either side of the pattern. So, for your
data you'll get a list returned where every 2nd element is interesting and
the others are not interesting. So:
@stuff = split /<prefix>|<suffix>/, $str;
@stuff = ........
hmmmmmm - I'm looking for a one-line way to extract the bits. Bit I can't
think of something sweet and simple and effective. Possibly a really crazy
one-liner could be worked out but that's not pragmatic programming no matter
how much fun it is. So I'll use a simple loop:
@final = ();
for($i = 1; $i < @stuff; $i += 2) {
push @final, $stuff[$i];
}
and @final is your result.
Actually, I'm starting to think of something that's almost a one-liner, and
it involves the grep operator, but it will only be a re-hash of the for loop
so I don't see the point. No doubt this is but a small part of your total
problem so it's time to move on and do the real work, right?
--
Space Corps Directive #723
Terraformers are expressly forbidden from recreating Swindon.
-- Red Dwarf
------------------------------
Date: Wed, 28 Nov 2001 23:20:45 GMT
From: gurm@intrasof.com (Smiley)
Subject: Strange behavior in a Perl CGI script
Message-Id: <3c057075.407215615@news1.on.sympatico.ca>
How can a Perl CGI script act like a directory? I've looked through
my textbooks, but haven't found any reference to this - yet that's
just what a script I've downloaded seems to be doing.
It's accessing another file by referring to itself as a directory, eg.
http://www.myserver.com/cgi-bin/myscript.cgi/thisdocument.doc
I'm very confused, can somebody give me an idea of what's going on?
------------------------------
Date: 28 Nov 2001 15:39:45 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: Strange behavior in a Perl CGI script
Message-Id: <m1zo5631oe.fsf@halfdome.holdit.com>
>>>>> "Smiley" == Smiley <gurm@intrasof.com> writes:
Smiley> How can a Perl CGI script act like a directory? I've looked through
Smiley> my textbooks, but haven't found any reference to this - yet that's
Smiley> just what a script I've downloaded seems to be doing.
Smiley> It's accessing another file by referring to itself as a directory, eg.
Smiley> http://www.myserver.com/cgi-bin/myscript.cgi/thisdocument.doc
Smiley> I'm very confused, can somebody give me an idea of what's going on?
First, "comp.lang.perl" is no longer an authorized group. If your
server carries it, they are in error.
Second, look at any Perl CGI text that talks about $ENV{PATH_INFO}. You'll
see that your CGI script can be treated like a directory, and any path
following the directory is included in the environment variable. In
particular, search google for:
site:stonehenge.com $ENV{PATH_INFO}
and you'll see the columns that I've written that talk about it.
Hope that helps. And tell your news admin that comp.lang.perl
has been dead since 1995, by consensus decree.
print "Just another Perl hacker,"
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Thu, 29 Nov 2001 12:24:01 +1100
From: "Skeleton Man" <invalid_email@www.skeleton-man.f2s.com>
Subject: Re: Strange behavior in a Perl CGI script
Message-Id: <3c058d38$1_1@news.iprimus.com.au>
What would be the point in having www.site.com/script.cgi/this.filename ?
It's no different to script.cgi?this.filename really... all it does is uses
$ENV{'PATH_INFO'}
instead of $ENV{'QUERY_STRING'}
A more useful approach is to make the script the directory index, then you
can do this:
www.mysite.com/?this.filename
In the above example, script.cgi is the directory index, so you don't need
to put the filename in..
I do this quite a lot.. makes it harder for someone to hack your script if
they don't know what it's called...
--
Regards,
Chris
---- -Begin Xmas Signature------
*__=*Time::HiRes::usleep=>my@m=map{$_+=$_%2?-1:+1}map ord,
split//=>'Ldssx!Bishrul`r!`oe!`!I`qqx!Odv!Xd`s/////////'.
'Ldssx!Bishrul`r!`oe!`!I`qqx!Odv!Xd`s/////////';
unshift @m=>(32)x@m;require Time::HiRes;for(0..@m){
print map(chr,@m[0..(@m/2-1)])=>"\b"x(@m/2);push@m=>
shift@m=>__(0170*0xA**3);}
------------------------------
Date: Thu, 29 Nov 2001 02:03:37 GMT
From: "SilentBob" <silentbob@rymon.net>
Subject: Re: Strange behavior in a Perl CGI script
Message-Id: <ZHgN7.77949$z55.9214043@typhoon.neo.rr.com>
"Skeleton Man" <invalid_email@www.skeleton-man.f2s.com> wrote in message
news:3c058d38$1_1@news.iprimus.com.au...
> What would be the point in having www.site.com/script.cgi/this.filename ?
Basically, when you tried to save it from a browser, instead of saving as
'script.cgi', it would be saved as 'this.filename'
> It's no different to script.cgi?this.filename really... all it does is
uses
> $ENV{'PATH_INFO'}
> instead of $ENV{'QUERY_STRING'}
>
> A more useful approach is to make the script the directory index, then you
> can do this:
> www.mysite.com/?this.filename
>
> In the above example, script.cgi is the directory index, so you don't need
> to put the filename in..
> I do this quite a lot.. makes it harder for someone to hack your script
if
> they don't know what it's called...
> --
> Regards,
> Chris
------------------------------
Date: 28 Nov 2001 16:57:31 -0800
From: winter7@e-mailanywhere.com (winter7)
Subject: Re: syswrite on closed socket?
Message-Id: <fb7341b.0111281657.1f976d89@posting.google.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<9u2gsi$di9$2@mamenchi.zrz.TU-Berlin.DE>...
> According to winter7 <winter7@e-mailanywhere.com>:
> > my server program hangs when trying to syswrite on closed socket.
> >
> > client usally send message before terminate so that server can delete
> > client from client list. but when client terminate abnormally, server
> > doesn't recognize and hangs when syswrite to this client.
> >
> > how do I overcome this problem?
>
> One way is to check writability via select(), probably via the
> module IO::Select.
>
> Anno
Doesn't race condition exist between select and syswrite?
------------------------------
Date: 28 Nov 2001 17:02:06 -0800
From: winter7@e-mailanywhere.com (winter7)
Subject: Re: syswrite on closed socket?
Message-Id: <fb7341b.0111281702.3010c18f@posting.google.com>
nobull@mail.com wrote in message news:<u9g06yybdu.fsf@wcl-l.bham.ac.uk>...
> winter7@e-mailanywhere.com (winter7) writes:
>
> > my server program hangs when trying to syswrite on closed socket.
>
> I suspect this is untrue.
>
> > client usally send message before terminate so that server can delete
> > client from client list. but when client terminate abnormally, server
> > doesn't recognize and hangs when syswrite to this client.
>
> You are not talking out a closed socket. You are talking about a
> socket the peer of which is closed.
>
> I suspect you are wrong about this anyhow. I suspect that the client
> has forked (or has been created by forking) since the socket was
> opened and that the branch of the fork is holding the socket open.
>
> > how do I overcome this problem?
>
> If I've guessed right, close unwanted file handles after forking.
Yes. I'm taking about a socket the peer of which is closed.
and I don't fork, I use 4 args select function for my multi server.
Thanks.
------------------------------
Date: Wed, 28 Nov 2001 18:47:19 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Using CGI.pm to obtain a list of params - Oops!
Message-Id: <9u40mh$tje$1@slb6.atl.mindspring.net>
"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x7y9kq5xhq.fsf@home.sysarch.com...
> WAS> Mea culpa! Out, gremlins! I wrote the above when I should have
written:
>
> WAS> s/(G)(\d+)/sprintf($1.($2+1))/ge;
>
> that sprintf has no arguments other than the format string so it us
> useless. a simple $1 . ($2 + 1) will do.
>
Gee, I dunno. I thought the argument was $1.($2+1). Regardless, it works
well. I'll certainly try your suggestion, though, after dinner.
> also why even grab the G? grabbing a fixed string makes little sense as
> it has to do an extra copy. it may make life a little nicer in defining
> the grab part but you could just remove the () and put a G in the
> replacement string.
>
> s/G(\d+)/'G'.($1+1)/ge;
>
I agree. This appears to be best. Let's try it.
Bless you, Uri! Your code works; and is more concise, as well. Thank you,
thank you, thank you!
> WAS> P.S. Unless someone sees a more concise way to do the above s///,
> WAS> let's end this "off subject" tangent from the original
> WAS> thread. I'm moving on to a version of the program that uses
> WAS> CGI.pm. With the suggestions y'all have contributed, the end
> WAS> result should be much better than it would have been otherwise.
>
> was that more concise?
>
Indeed it was. Thanks again, Uri.
Bill Segraves
Auburn, AL
------------------------------
Date: Wed, 28 Nov 2001 19:04:04 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: Using CGI.pm to obtain a list of params - Oops!
Message-Id: <9u41m3$mrk$1@slb7.atl.mindspring.net>
"William Alexander Segraves" <wsegrave@mindspring.com> wrote in message
news:...
> "Uri Guttman" <uri@stemsystems.com> wrote in message
> news:x7y9kq5xhq.fsf@home.sysarch.com...
> > WAS> Mea culpa! Out, gremlins! I wrote the above when I should have
> written:
> >
> > WAS> s/(G)(\d+)/sprintf($1.($2+1))/ge;
> >
> > that sprintf has no arguments other than the format string so it us
> > useless. a simple $1 . ($2 + 1) will do.
> >
>
> Gee, I dunno. I thought the argument was $1.($2+1). Regardless, it works
> well. I'll certainly try your suggestion, though, after dinner.
>
O.K. Let's try your version.
s/(G)(\d+)/$1.($2+1)/ge;
Your suggestion worked, as well. Thanks again.
Bill Segraves
Auburn, AL
------------------------------
Date: Thu, 29 Nov 2001 00:06:03 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <slrna0auvn.rql.mgjv@verbruggen.comdyn.com.au>
On 28 Nov 2001 15:36:59 -0500,
Joe Schaefer <joe+usenet@sunstarsys.com> wrote:
>
> @a = 1..4;
> grep( $_ % 2, @a ) = "a".."z";
>
> ~>Can't modify grep iterator in scalar assignment ...
>
> Why is this illegal? I know it's easy to convert that
Because grep operates on a list, not an array. You can't modify a
list. How would
grep( $_ % 2, (@a, 5, 6) ) = "a" .. "z";
work if grep produced an lvalue array? and where would this array be
stored?
> I know it's easy to convert that
> into a real slice:
>
> @a[ grep $a[$_] % 2, 0 .. $#a ] = "a" .. "z";
>
> ~> @a = ("a", 2, "b", 4)
But here the changes are made by the array slice, as you obviously
know. Grep is only used to produce a set of indexes into that array.
> I'm just wondering if there is a problem with making grep
> work just like substr() does for scalars.
Mainly because grep needs to work with lists, and lists are not
modifiable. The scalar that goes into substr is a modifiable value, or
if it isn't, you get an error. Functions that can be used as lvalues
need to operate on an lvalue, which should be either a scalar (e.g.
substr) or an array (e.g. splice), or possibly a hash (keys is an
lvalue operating on a hash, but in a different way).
I don't think it would be possible to allow grep to work in the way
you want it to without changing its semantics, and breaking quite a
few things along the way. grep has to be able to operate on lists, I
think.
Martien
--
|
Martien Verbruggen | I'm just very selective about what I
Trading Post Australia Pty Ltd | accept as reality - Calvin
|
------------------------------
Date: 28 Nov 2001 19:54:40 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <m31yii4crz.fsf@mumonkan.sunstarsys.com>
Martien Verbruggen <mgjv@tradingpost.com.au> writes:
> Because grep operates on a list, not an array. You can't modify a
> list. How would
>
> grep( $_ % 2, (@a, 5, 6) ) = "a" .. "z";
>
> work if grep produced an lvalue array? and where would this array be
> stored?
Thanks for responding- I think you've understood what I'm trying to do.
($a,$b) = "foo"; # OK
(1, $b) = "foo"; # can't modify constant item in list assign
( (1,$b)[-1] ) = "foo"; # can't modify list slice in list assignment
The errors here are compile-time errors, so I guess perl needs to
know all about the elements in the LHS's *list* at compile time. I think
I'm starting to see why grep can't work the way I'd like, since a list
slice won't even work this way.
[...]
> I don't think it would be possible to allow grep to work in the way
> you want it to without changing its semantics, and breaking quite a
> few things along the way. grep has to be able to operate on lists, I
> think.
% perl -wle 'print ++$_ for grep $a++, ($a, 2*$a, $a, 0)'
Use of uninitialized value in multiplication (*) at -e line 1.
1
5
Modification of a read-only value attempted at -e line 1.
I'm very happy with the way grep builds a list of scalars; the
outcome above is ok by me. I was just wondering why you can't
use the resulting list on the LHS of an assignment.
It looks like the reason is that the list elements need to be known
to perl at compile-time, which isn't really possible with list slices
or grep().
--
Joe Schaefer "Life is just one damn thing after another."
--Mark Twain
------------------------------
Date: Thu, 29 Nov 2001 01:31:47 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Why isn't grep an l-valued function?
Message-Id: <tk3b0u8a4lfq4t91if3i1fshnm2ktq133f@4ax.com>
Martien Verbruggen wrote:
>> grep( $_ % 2, @a ) = "a".."z";
>>
>> ~>Can't modify grep iterator in scalar assignment ...
>>
>> Why is this illegal?
>
>Because grep operates on a list, not an array. You can't modify a
>list. How would
>
>grep( $_ % 2, (@a, 5, 6) ) = "a" .. "z";
>
>work if grep produced an lvalue array? and where would this array be
>stored?
That doesn't make sense. The argument between parens for a foreach
command, is a list too, yet you can still modify it.
@a = ('A' .. 'Z');
foreach(@a, $x, $y) {
$_ = ++$i;
}
This works: every value, i.e. every array item and every scalar
variable, will be modified.
It works with slices, too.
The reason why it doesn't work with grep, is IMO incidental: grep()
doesn't return a list of Lvalues, *that* is the problem. But so far,
nobody has ever requested that feature, I think, I don't think many
people have even tried to use it. Perhaps there are even speed
considerations against it. Funny, the $_ alias in the grep block *is* an
Lvalue.
--
Bart.
------------------------------
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.
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 2232
***************************************