in reply to Re: In-place editing of files
in thread In-place editing of files

A few things I think are wrong with your solution: The use of the next is incorrect because the 'Tag' line will be looked for again since we are going to the top of the while loop. You are missing a '~' on the $value line. Also, could you explain what that '..' is used for in that context in the if condition?

Replies are listed 'Best First'.
Re^3: In-place editing of files
by Aristotle (Chancellor) on Sep 22, 2004 at 01:37 UTC

    Why did you conclude it's wrong to use next when you didn't know what .. does? As for what it does, that's in the documentation. I'll quote perlop (emphasis mine):

    In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated. It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once. If you don't want it to test the right operand till the next evaluation, as in sed, just use three dots ("...") instead of two. In all other regards, "..." behaves just like ".." does.

    The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. The precedence is a little lower than || and &&. The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encountered. The final sequence number in a range has the string "E0" appended to it, which doesn't affect its numeric value, but gives you something to search for if you want to exclude the endpoint. You can exclude the beginning point by waiting for the sequence number to be greater than 1.

    In other words, that if will fail until it sees a ^\s+Tag {, at which point it will always succeed until a ^\s+} comes along. The outer loop reads the lines, and the flip-flop picks the ranges of interesting lines to which to apply the if block.

    Of course (and that's why I mentioned it), this will not properly respect nesting blocks (but they can easily be taken into consideration with an open-blocks counter).

    Makeshifts last the longest.