develooper Front page | perl.perl5.porters | Postings from December 1999

Re: [ID 19991130.003] Assignment to List Breaks \G.

Thread Previous | Thread Next
From:
Ralph Corderoy
Date:
December 2, 1999 03:21
Subject:
Re: [ID 19991130.003] Assignment to List Breaks \G.
Message ID:
199912021121.LAA08282@inputplus.demon.co.uk

Hi, I'm sending this again as I suspect that sending it from a
different to normal mail address yesterday might have made the list
drop me on the floor.  Sorry for the noise if this isn't the case.

------- Forwarded Message

Date: Wed, 01 Dec 1999 17:04:01 +0000
Subject: Re: [ID 19991130.003] Assignment to List Breaks \G.
In-reply-to: Message from Ilya Zakharevich <ilya@math.ohio-state.edu> "of Tue, 30 Nov 1999 11:43:00 EST."
 <199911301643.LAA12574@monk.mps.ohio-state.edu>
To: Ilya Zakharevich <ilya@math.ohio-state.edu>
Cc: rjk@linguist.dartmouth.edu (Ronald J Kimball), merlyn@stonehenge.com,
        desar@front1m.grolier.fr, ralph@inputplus.demon.co.uk (Ralph Corderoy),
        perl5-porters@perl.org
Message-id: <199912011704.RAA105502@cm01.ess>
Content-transfer-encoding: 7BIT


Ronald J Kimball writes:
> This is the expected behavior for m//g in a list context.  The regular
> expression is applied repeatedly until it no longer matches, and the
> return value is a list of all the substrings matched.  After the final
> application of the regex, which fails to match, pos() is reset to the
> beginning of the string.

Hi, thanks for all the replies.  Having put -Dr to good use I fully
understand what is happening with my example script.  However, I'd like
to put a case forward that this behaviour is broken and judging from
Randal's question in

    http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-10/msg01047.html

there might be others that agree.

Normally, the use of context in Perl to `do the right thing' works well.
I'd suggest that the overloading of /g based on context allows only two
alternatives (/g and no /g) to be selected from a larger desired set of
operations.

If I want to match many times, possibly leaving pos alone on failure,
then the existing /g and /c work well.

    @w = /(\w+)/g;
    @w = /(\w+)/gc;

But if I want to do some lexing then I need /g to enable \G.  As long as
I use scalar context that's fine.

    if (/(\w)+/gc) {
        ($foo) = ($1);

but if I want to assign to a list I'm stuck;  that would create a list
context indicating I want multiple matches.

    if (($foo) = /(\w)+/gc) {      # bad

I want to give a list context for assignment to variables and I need \G
to work.  With the context overloading of /g that's impossible.

If there was a /l for lexing that enabled \G without adding the global
matching of /g then I'd be happy and I suspect a lot of other people
would stop making the same mistake and bothering you about it.

    if (($foo) = /(\w)+/lc) {      # bad

Making /gc work so pos wasn't reset on the last, guaranteed to fail,
iteration wouldn't cut it AFAICS.  It would still give me multiple
matches which aren't desired.

    ($foo) = /(\w+)\s*/lc;    # takes one word.
    ($foo) = /(\w+)\s*/gc;    # takes many words returning one.

To sum up, /g means two things (many matches and enable \G), it means
you can't just enable \G.

Thanks for your time.


Ralph.

------- End of Forwarded Message


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About