in reply to In-place editing of files

Instead of break, you really need to have next. Also, you'd better check for the end of your block, a lone closing brace (according to your example), and quit the inner while() loop when you find it:
last if /^\s*}\s*$/;