If it helps to wrap your mind around it, think of perl "strings" as java StringBuffers. For this perl code...
my $output = "";
for (1 .. 1000) {
$output .= get_long_string();
}
print $output;
...the analogous java code is...
StringBuffer output = new StringBuffer();
for (1 .. 1000) {
output.appends(get_long_string());
}
System.out.print(output.toString());
I would not be surprised however if this perl code behaved as inefficiently as you might expect it to...
my $output = "";
for (1 .. 1000) {
$output = "" . get_long_string() . $output;
}
print $output;
(but i haven't tested it ... perl might optimize it)