On Tue, Jan 06, 2009 at 04:41:30PM +0300, Richard Hainsworth wrote:
> Supposed I define
>
> regex digit { [0..9] }
>
> what is the negative?
You need to be careful about what you mean here by "negative". If you mean
"match
a single character that is not in the list", then it is as Patrick said.
> By analogy, it should be <!digit> but I cant find this in the Synopses
> (possibly missed the relevant section).
Any assertion may be negated, including <digit>, but it doesn't mean
the same kind of negative. <?digit> and <!digit> are positive and
negative lookaheads, so the never match a character. So
<-digit>
really means
<!digit> .
It just happens that . is looking at the same thing as <!digit>, but
there's no requirement that they line up like that.
> Also, suppose I want a 'when' clause to fire when the test is *not* met.
> What syntax should be used?
>
> So how would I do
> given {
> when ! /<digit>/ {say 'this is not a digit'} # this does not work
> }
That should work, and so should
when not /<digit>/
since Boolean expressions are just tested outright and not compared
back to $_.
But in general, since switch statement cases are ordered, you should
usually match the case positively earlier and give it a different,
possibly null, behavior:
when /^<digit>$/ { }
when * { say 'this is not a digit' }
So there is little need for syntactic relief here, I think.
Larry