From: Wolfram Humann on 18 May 2010 12:39 Would someone be so kind to explain the following (quotes are for win32 perl): perl -E" $u='foo'; given($u){ when('foo' and 'bar'){say 'f'} default{say 'd'} } " d perl -E" $u='bar'; given($u){ when('foo' and 'bar'){say 'f'} default{say 'd'} } " f perl -E" $u='baz'; given($u){ when('foo' and 'bar'){say 'f'} default{say 'd'} } " d perl -E" $u='foo'; given($u){ when('foo' or 'bar'){say 'f'} default{say 'd'} } " f perl -E" $u='bar'; given($u){ when('foo' or 'bar'){say 'f'} default{say 'd'} } " d perl -E" $u='baz'; given($u){ when('foo' or 'bar'){say 'f'} default{say 'd'} } " d My expectation was: smart matching of two strings uses 'eq'. $u can not be equal to both 'foo' and 'bar' at the same time so in the 'and' case I always expect the default 'd'. In the 'or' case I expect 'f' to be printed if $u is either 'foo' or 'bar'. Why is my expectation wrong?
From: Willem on 18 May 2010 12:54 Wolfram Humann wrote: ) Would someone be so kind to explain the following (quotes are for ) win32 perl): ) ) perl -E" $u='foo'; given($u){ when('foo' and 'bar'){say 'f'} ) default{say 'd'} } " ) d ) perl -E" $u='bar'; given($u){ when('foo' and 'bar'){say 'f'} ) default{say 'd'} } " ) f ) perl -E" $u='baz'; given($u){ when('foo' and 'bar'){say 'f'} ) default{say 'd'} } " ) d ) perl -E" $u='foo'; given($u){ when('foo' or 'bar'){say 'f'} ) default{say 'd'} } " ) f ) perl -E" $u='bar'; given($u){ when('foo' or 'bar'){say 'f'} ) default{say 'd'} } " ) d ) perl -E" $u='baz'; given($u){ when('foo' or 'bar'){say 'f'} ) default{say 'd'} } " ) d ) My expectation was: smart matching of two strings uses 'eq'. $u can ) not be equal to both 'foo' and 'bar' at the same time so in the 'and' ) case I always expect the default 'd'. In the 'or' case I expect 'f' to ) be printed if $u is either 'foo' or 'bar'. ) Why is my expectation wrong? print('foo' and 'bar'); # bar print('foo' or 'bar'); # foo I guess the smart matching doesn't distribute ofer the 'and' or the 'or'. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT
From: Wolfram Humann on 18 May 2010 14:06 On 18 Mai, 18:54, Willem <wil...(a)turtle.stack.nl> wrote: > print('foo' and 'bar'); # bar > print('foo' or 'bar'); # foo > > I guess the smart matching doesn't distribute ofer the 'and' or the 'or'. > Hm, sounds reasonable. However, it's not really what I expected from the explanation for "when(EXPR)" in perlsyn: Furthermore: If EXPR is ... && ... or ... and ..., the test is applied recursively to both arguments. If both arguments pass the test, then the argument is treated as boolean. If EXPR is ... || ... or ... or ..., the test is applied recursively to the first argument. These rules look complicated, but usually they will do what you want. As a matter of fact, if the 'and' and 'or' are evaluated before the smart matching applies, IMHO it would be better to state exactly that instead of saying "usually they will do what you want" :-) Thanks for the reply, Wolfram
From: Uri Guttman on 18 May 2010 14:25 >>>>> "WH" == Wolfram Humann <w.c.humann(a)arcor.de> writes: WH> On 18 Mai, 18:54, Willem <wil...(a)turtle.stack.nl> wrote: >> print('foo' and 'bar'); �# bar >> print('foo' or 'bar'); � # foo >> >> I guess the smart matching doesn't distribute ofer the 'and' or the 'or'. >> WH> Hm, sounds reasonable. However, it's not really what I expected from WH> the explanation for "when(EXPR)" in perlsyn: WH> Furthermore: WH> If EXPR is ... && ... or ... and ..., the test is applied WH> recursively to both arguments. If both arguments pass the test, then WH> the argument is treated as boolean. WH> If EXPR is ... || ... or ... or ..., the test is applied WH> recursively to the first argument. WH> These rules look complicated, but usually they will do what you WH> want. having read the docs i agree it isn't very clear. try it again with regexes like /foo/ and /bar/ also with && instead of 'and'. i feel the logic as the docs seem to say is $_ ~~ EXPR which makes it: $_ ~~ 'foo' and 'bar' that evaluates to ($_ ~~ 'foo') and 'bar' so it won't distribute as you think (and the docs seem to imply). this could be a bug in perl or the docs. i would raise the issue on p5p as this definitely seems ambiguous. also try explicit tests like $_ eq 'foo' which should work. uri -- Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: sln on 18 May 2010 14:32 On Tue, 18 May 2010 11:06:34 -0700 (PDT), Wolfram Humann <w.c.humann(a)arcor.de> wrote: >On 18 Mai, 18:54, Willem <wil...(a)turtle.stack.nl> wrote: >> print('foo' and 'bar'); �# bar >> print('foo' or 'bar'); � # foo >> >> I guess the smart matching doesn't distribute ofer the 'and' or the 'or'. >> > >Hm, sounds reasonable. However, it's not really what I expected from >the explanation for "when(EXPR)" in perlsyn: > Furthermore: > If EXPR is ... && ... or ... and ..., the test is applied >recursively to both arguments. If both arguments pass the test, then >the argument is treated as boolean. > If EXPR is ... || ... or ... or ..., the test is applied >recursively to the first argument. > These rules look complicated, but usually they will do what you >want. > >As a matter of fact, if the 'and' and 'or' are evaluated before the >smart matching applies, IMHO it would be better to state exactly that >instead of saying "usually they will do what you want" :-) >Thanks for the reply, >Wolfram > Hum, hows that old given/when thing worky for ya? Recursive boolean expression, now where has that been all these years? -sln
|
Next
|
Last
Pages: 1 2 Prev: FAQ 4.56 How do I merge two hashes? Next: bit-twiddling on 32-bit machines |