From: Rafael Garcia-Suarez Date: Wed, 4 Mar 2009 18:33:31 +0000 (+0100) Subject: Make // behave like || in when clauses X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f92e1a16ee2379315520131bbe3465eb837abfa3;p=p5sagit%2Fp5-mst-13.2.git Make // behave like || in when clauses (plus minor documentation updates about "when") --- diff --git a/op.c b/op.c index 5103efb..943837e 100644 --- a/op.c +++ b/op.c @@ -5183,6 +5183,7 @@ S_looks_like_bool(pTHX_ const OP *o) switch(o->op_type) { case OP_OR: + case OP_DOR: return looks_like_bool(cLOGOPo->op_first); case OP_AND: @@ -5198,7 +5199,6 @@ S_looks_like_bool(pTHX_ const OP *o) case OP_ENTERSUB: case OP_NOT: case OP_XOR: - /* Note that OP_DOR is not here */ case OP_EQ: case OP_NE: case OP_LT: case OP_GT: case OP_LE: case OP_GE: diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index ec86510..83ad2c2 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -531,7 +531,7 @@ This construct is very flexible and powerful. For example: } when (\&complicated_check) { - say 'complicated_check($foo) is true'; + say 'a complicated check for $foo is true'; } default { @@ -567,7 +567,7 @@ a subroutine or method call =item * a regular expression match, i.e. C or C<$foo =~ /REGEX/>, -or a negated regular expression match C<$foo !~ /REGEX/>. +or a negated regular expression match (C or C<$foo !~ /REGEX/>). =item * @@ -580,26 +580,27 @@ C, C, or C =item * -A negated expression C or C, or a logical +a negated expression C or C, or a logical exclusive-or C<(...) xor (...)>. =back -then the value of EXPR is used directly as a boolean. +In those cases the value of EXPR is used directly as a boolean. + Furthermore: =over 4 -=item o +=item * If EXPR is C<... && ...> or C<... and ...>, the test is applied recursively to both arguments. If I arguments pass the test, then the argument is treated as boolean. -=item o +=item * -If EXPR is C<... || ...> or C<... or ...>, the test +If EXPR is C<... || ...>, C<... // ...> or C<... or ...>, the test is applied recursively to the first argument. =back diff --git a/t/op/switch.t b/t/op/switch.t index 14cf98a..f54f6c4 100644 --- a/t/op/switch.t +++ b/t/op/switch.t @@ -8,7 +8,7 @@ BEGIN { use strict; use warnings; -use Test::More tests => 111; +use Test::More tests => 112; # The behaviour of the feature pragma should be tested by lib/switch.t # using the tests in t/lib/switch/*. This file tests the behaviour of @@ -519,6 +519,16 @@ sub bar {"bar"} ok($ok, '((1 == $ok) || "foo") smartmatched'); } +{ + my $ok = 0; + given("foo") { + when((1 == $ok || undef) // "foo") { + $ok = 1; + } + } + ok($ok, '((1 == $ok || undef) // "foo") smartmatched'); +} + TODO: { local $TODO = "RT #50538: when( \@n && \%n ) fails to smart match"; { # this should smart match on each side of &&