$str = "this\nis my short\nstring\n\n"; $i=0; for (split /\n/, $str) { print(++$i, ": $_\n"); } print("\n"); $i=0; for ($str =~ /.*\n|.+/g) { # Just like <> print(++$i, ": $_"); }