Bloodnok has asked for the wisdom of the Perl Monks concerning the following question:
Tis late on POETS day and not untypically, I've encountered a problem - which is a seeming oddity in the behaviour of grep - let me explain (which, if I don't, won't win me any friends;-)
There's something odd in the way that grep is behaving in the following...
Irrespective of the values of $choice and @REPORT, the last statement is always reached (and executed).grep /\Q$choice\E/, @REPORT && do { last READ_LOOP; };
However, if the statement is re-written slightly - to add parenthesis for the arguments to the call to grep:
All of a sudden, the last statement is conditionally reached i.e. is now dependent on the values of $choice and @REPORT. For information, $choice typically contains either a path or a digit but, as was said in Airplane, that's not important right now:-)grep(/\Q$choice\E/, @REPORT) && do { last READ_LOOP; };
B::Deparse provides further information:
Armed with this information, I still can't see why perl interprets the 2 non-equivalently.user@unforgiven:~$ perl -MO=Deparse -e 'grep /\Q$choice\E/, @REPORT && + do { last READ_LOOP; };' grep /\Q$choice\E/, @REPORT && do { last READ_LOOP }; -e syntax OK user@unforgiven:~$ perl -MO=Deparse -e 'grep( /\Q$choice\E/, @REPORT) +&& do { last READ_LOOP; };' if (grep /\Q$choice\E/, @REPORT) { last READ_LOOP; } -e syntax OK
I'm sure this is but a small copse, but still I can't see the wood for the trees ... would anyone be good enough to enlighten me ?
TIA ,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Alas poor grep, I thought I knew him well..
by kennethk (Abbot) on May 08, 2009 at 15:11 UTC | |
by Bloodnok (Vicar) on May 08, 2009 at 15:13 UTC | |
|
Re: Alas poor grep, I thought I knew him well..
by ikegami (Patriarch) on May 08, 2009 at 15:14 UTC | |
by Marshall (Canon) on May 08, 2009 at 17:34 UTC |