is an infinite loop if @msgstr becomes the empty list because then -1 returns undef, which causes length to return undef, which is false and gets negated.
You could fix this by using pop @msgstr while !length($msgstr[-1]//0); instead (perl v5.10 and up)