Brent 'Dax' Royal-Gordon wrote:

Rod Adams <[EMAIL PROTECTED]> wrote:


Larry Wall wrote:


That, and we'd like a novice to be able to write

  given $x {
     when 1 | 2 | 3 {...}
     when 4 | 5 | 6 {...}
  }

Or just change C<when> to accept a list of things to compare against,
followed by a coderef.


And change if, unless, while and until to do the same thing.

Actually, upon further investigation, I believe we get this all of this without junctions.

According to S03, "The scalar comma |,| now constructs a list reference of its operands."
Then S04 mentions that C< $scalar ~~ @array > is true if $scalar is in @array.
Since C< given ... when > uses smart matching for it's evaluation, one should be able to write the above as:


given $x {
 when 1, 2, 3 {...} # at worst, this is:  when (1,2,3) {...}
 when 4, 5, 6 {...}
}

The simple if is:

if $x ~~ (1,2,3,4) {...} # parens needed here since , is lower than ~~ in precedence.

Same for unless/while/until. And all of this from the entirely useful C< ~~ >. The S04 code describing @Array ~~ $Scalar (for Num/Str) uses junctions, but I'd argue a better implementation would be a short circuiting C< for > loop, even if junctions exist. It's just plain faster that way.

So what I see now for utility of junctions is thus:

- Common cases which C< ~~ > appears to handle for us suitably well.
- Edge cases which, IMHO, do not merit the huffman level of several single character operators. All of which can be accomplished without the use of junctions, though not as gracefully.



I see no need for junctions in core.

And lose the ability to say:
   when none(1, 2, 3) { ... }
   when 1 ^ 2 ^ 3  { ... }    # More useful with classes, regexen, etc.
   when 1 & 2 & 3 { ... }    # Likewise

All so that a newbie doesn't confuzzle himself.


You can always write your switch statements the Perl 5 way.
Or you could write:

   when ({$^a ~~ /1/  && $^a ~~ /2/  && $^a ~~/3/}) {...}

And the like. Look up what C< $scalar ~~ $coderef > does if you're not convinced.
(side question: what is the proper syntax for using a closure as the evaluation expression in a C< when > statement?)


hmm, since the $_ is set, you could likely get away with:

   when ({/1/  && /2/  && /3/}) {...}

in the case of RE's.

Personally, I'd rather have a chain saw than a nail trimmer, even if
I'm less likely to hurt myself with the nail trimmer. And it looks
like we'll have a warning or stricture to keep newbies from chopping
their legs off anyway.


I can understand your sentiments here, but I'd be a lot more sympathetic to your cause if the alternative ways of accomplishing these things were actually difficult. I think it's been demonstrated that any of the junction evaluations could be done with a call to C< grep >, with numerous other ways to perform them as well.

-- Rod Adams

Reply via email to