Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[svn:perl6-synopsis] r8899 - doc/trunk/design/syn

0 views
Skip to first unread message

autr...@cvs.perl.org

unread,
Apr 21, 2006, 11:56:23 AM4/21/06
to perl6-l...@perl.org
Author: autrijus
Date: Fri Apr 21 08:56:22 2006
New Revision: 8899

Modified:
doc/trunk/design/syn/S05.pod

Log:
* S05: Oops, turns out I entirely read perlop.pod incorrectly;
"it matches once only" means "it matches successfully once only",
not "it performs the match once only". Sorry, TimToady++'s
original example of:

(state $x) ||= / pattern /;

was correct.

Modified: doc/trunk/design/syn/S05.pod
==============================================================================
--- doc/trunk/design/syn/S05.pod (original)
+++ doc/trunk/design/syn/S05.pod Fri Apr 21 08:56:22 2006
@@ -1066,9 +1066,9 @@
The Perl 5 C<?...?> syntax (I<match once>) was rarely used and can be
now emulated more cleanly with a state variable:

- (state $x) //= / pattern /; # only matches first time
+ (state $x) ||= / pattern /; # only matches first time

-To reset the pattern, simply say C<undefine $x>.
+To reset the pattern, simply say C<$x = 0>.

=back

Uri Guttman

unread,
Apr 21, 2006, 1:12:35 PM4/21/06
to autr...@cvs.perl.org, perl6-l...@perl.org
>>>>> "a" == autrijus <autr...@cvs.perl.org> writes:

a> * S05: Oops, turns out I entirely read perlop.pod incorrectly;
a> "it matches once only" means "it matches successfully once only",
a> not "it performs the match once only". Sorry, TimToady++'s
a> original example of:

a> (state $x) ||= / pattern /;

a> was correct.

a> +To reset the pattern, simply say C<$x = 0>.

i did a fresh read of S05 due to all the recent activity (i will post
some edits and questions soonish), but that example baffles me. how does
it emulate the (never used by me) // of p5? my take would be that the rx
would be or-assigned to $x and it would remain set through repeated
calls to the outer sub (assuming a sub). what is the context that makes
it match against $_ vs returning an rx. wouldn't the assignment bleed
into the the //? or is $x || /pattern/ going to do a match if it $x is
false?

in any case, one quick comment on S05 is it needs some more examples and
some explanations of how the examples do what they are claimed to do. i
know this is a synopsis/spec but some places do go into more explanation
with examples than others. i would like to see it more consistant at
that level.

thanx,

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Jonathan Scott Duff

unread,
Apr 21, 2006, 1:45:13 PM4/21/06
to Uri Guttman, autr...@cvs.develooper.com, perl6-l...@perl.org
On Fri, Apr 21, 2006 at 01:12:35PM -0400, Uri Guttman wrote:
> >>>>> "a" == autrijus <autr...@cvs.perl.org> writes:
>
> a> * S05: Oops, turns out I entirely read perlop.pod incorrectly;
> a> "it matches once only" means "it matches successfully once only",
> a> not "it performs the match once only". Sorry, TimToady++'s
> a> original example of:
>
> a> (state $x) ||= / pattern /;
>
> a> was correct.
>
> a> +To reset the pattern, simply say C<$x = 0>.
>
> i did a fresh read of S05 due to all the recent activity (i will post
> some edits and questions soonish), but that example baffles me. how does
> it emulate the (never used by me) // of p5? my take would be that the rx
> would be or-assigned to $x and it would remain set through repeated
> calls to the outer sub (assuming a sub). what is the context that makes
> it match against $_ vs returning an rx.

According to S05, "a /.../ matches immediately in a value context
(void, Boolean, string, or numeric)" and since

(state $x) ||= / pattern /;

is very much the same as

state $x; $x = $x || /pattern/;

I'd say that's a "boolean context" and thus matches against $_ instead
of assigning the Regex object to $x.

-Scott
--
Jonathan Scott Duff
du...@pobox.com

Larry Wall

unread,
Apr 21, 2006, 2:06:20 PM4/21/06
to perl6-l...@perl.org, Uri Guttman, autr...@cvs.develooper.com
On Fri, Apr 21, 2006 at 12:45:13PM -0500, Jonathan Scott Duff wrote:
: According to S05, "a /.../ matches immediately in a value context

: (void, Boolean, string, or numeric)" and since
:
: (state $x) ||= / pattern /;
:
: is very much the same as
:
: state $x; $x = $x || /pattern/;
:
: I'd say that's a "boolean context" and thus matches against $_ instead
: of assigning the Regex object to $x.

Except the right side of || isn't in boolean context till you evaluate
$x that way, so it probably wants to be written with m//:

state $x; $x = $x || m/pattern/;

to force the match immediately. Also, the emulation of ?...? is not complete
without actually evaluating $x, so this might be clearer:

$result = do { state $x ||= m/pattern/ }

or maybe even

$result = do { state $x ||= m/pattern/; $x }

though of course that's redundant since the ||= returns its current value.

Larry

Jonathan Scott Duff

unread,
Apr 21, 2006, 2:34:39 PM4/21/06
to perl6-l...@perl.org, Uri Guttman, autr...@cvs.develooper.com
On Fri, Apr 21, 2006 at 11:06:20AM -0700, Larry Wall wrote:
> On Fri, Apr 21, 2006 at 12:45:13PM -0500, Jonathan Scott Duff wrote:
> : According to S05, "a /.../ matches immediately in a value context
> : (void, Boolean, string, or numeric)" and since
> :
> : (state $x) ||= / pattern /;
> :
> : is very much the same as
> :
> : state $x; $x = $x || /pattern/;
> :
> : I'd say that's a "boolean context" and thus matches against $_ instead
> : of assigning the Regex object to $x.
>
> Except the right side of || isn't in boolean context till you evaluate
> $x that way, so it probably wants to be written with m//:

I almost added a little bit of text to the end of that other message
but didn't. I think it's most appropriate now:

I must say that I have trouble distinguishing when using // gives you
a Regex object and when it actually does pattern matching, so I intend
on always using m// when I want a match, and some form of rx// when I
want an object.

Uri Guttman

unread,
Apr 21, 2006, 3:15:12 PM4/21/06
to perl6-l...@perl.org, autr...@cvs.develooper.com
>>>>> "JSD" == Jonathan Scott Duff <du...@pobox.com> writes:

JSD> On Fri, Apr 21, 2006 at 11:06:20AM -0700, Larry Wall wrote:
>> On Fri, Apr 21, 2006 at 12:45:13PM -0500, Jonathan Scott Duff wrote:
>> : According to S05, "a /.../ matches immediately in a value context
>> : (void, Boolean, string, or numeric)" and since
>> :
>> : (state $x) ||= / pattern /;
>> :
>> : is very much the same as
>> :
>> : state $x; $x = $x || /pattern/;
>> :
>> : I'd say that's a "boolean context" and thus matches against $_ instead
>> : of assigning the Regex object to $x.
>>
>> Except the right side of || isn't in boolean context till you evaluate
>> $x that way, so it probably wants to be written with m//:

JSD> I almost added a little bit of text to the end of that other message
JSD> but didn't. I think it's most appropriate now:

JSD> I must say that I have trouble distinguishing when using // gives you
JSD> a Regex object and when it actually does pattern matching, so I intend
JSD> on always using m// when I want a match, and some form of rx// when I
JSD> want an object.

having read S05's take on that i agree in general. i will probably
always use rx// the way you had to use qr// in p5. i may drop the m in
// as that is the classic 'match this now'. but i think it is good style
to mark these as to what you mean and not let the DWIM rule for you. but
then i also rarely use $_ in p5 so i would be using ~~ as i use =~ now
and that always means match so the m wouldn't be needed for
clarification. but the way p6 uses $_ in some places is much better than
p5 so that rule may need changing too. oy! the habits to be broken and
relearned!

Larry Wall

unread,
Apr 21, 2006, 5:08:44 PM4/21/06
to perl6-l...@perl.org
On Fri, Apr 21, 2006 at 03:15:12PM -0400, Uri Guttman wrote:
: oy! the habits to be broken and relearned!

Habits are no fun unless they're either good or bad.

Larry

0 new messages