in reply to Operator precedence (or, I'm an idiot)

If you are on a UNIX system, there is a full non-perl solution (untested!); this should work on a bourne-like shell:

for FILE in `find /dir -type f -name \*.pl -print` ; do sed -e 's/and/&&/g' $FILE > $FILE.new ; done

After that you should go through all .new files and see if you actually needed the change. I am not sure that you really want to change every and in &&

If you are sure you don't need to double-check the changed files, you could then substitute that sed with an analogous one-liner using perl -pi.bak 's/and/&&/g' (untested again) and keep the .bak files in a safe place if, one day, you'll find a wrong change.

Update: Oooooopssss! hardburn is right!!! You need to do the changes iteratively or write something to reparse your code... sorry, I am an idiot, too!

Ciao!
--bronto


The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
--John M. Dlugosz

Replies are listed 'Best First'.
Re: Re: Operator precedence (or, I'm an idiot)
by belden (Friar) on Mar 09, 2003 at 17:47 UTC
    You've solved a non-trivial problem in a trivial fashion. That's bound to cause problems. Running a blind sed on your perl code is inadvisable, to say the least. There's no way for either of your examples to *not* make an inappropriate change in comments, pod, or any string that happens to contain 'and':
    sub sandbox { my ($this, $that) = @_; blarf($this) and mungle($that); # etc. }
    Both of your examples will re-write that as:
    sub s&&box { my ($this, $that) = @_; blarf($this) && mungle($that); # etc. }
    Plus, there might be other programmers working on the project that don't have this and and && confusion. As you've already realized, there's no quick fix for the OP. Either examine each file, or put a bit more time into writing a smarter parser.

    I did ++ you for having one saving grace: re-writing the code to temp files rather than over the original code, and keeping the original stuff around.

    I know that this is stuff you already realized - but there's a few other replies similar to yours, and I happened to choose yours to reply to :)

    blyman
    setenv EXINIT 'set noai ts=2'