A third option -- slurp each stanza into memory at once, process that stanza, and then print.
If you are sure you won't have square brackets inside your stanza, you could even let perl do the slurping for you by setting the input record separator. E.G. something like (warning, untested code ahead which may need tweaking...):
$/="[";
while(<>) {
($newprefix) = m{uri.*([^/]*)$}m;
if ($newprefix) {
# we won't match on the first line so it will print just "["
print $newprefix," ";
}
print;
}
Note the use of the "m" modifier of the matching to allow $ to match the end of line in the middle of a string.
You could also be more prosaic, and read in line at a time, concatenating to a temporary variable, until you hit your new stanza leader, and do a similar process. (That would be cleaner, and more extensible/less hacky, too).
Good luck...
--JAS