I think the only changes I would make involve a few trivial simplifications:
sub list {
my @words = split( /\s+/, $_[0] );
my $lastword = $#words;
for my $i (1..$lastword) {
push @words, map { join " ", @words[$_..$_+$i] } (0..$lastword
+-$i);
}
return @words;
}
Notes:
- Assuming that $/ has its default value, splitting on white-space makes "chomp" unnecessary
- Your map-within-foreach covers everything, including the "substring" containing all input words
- You can eliminate most of your temporary storage variables, but ...
- ... it's important not to use "$#array" inside a loop when altering the size of @array in the loop (unless you're doing something different from this case, and you really know what you're doing)
(update: fixed
@_[0] --> $_[0] in first line of sub -- and this could just as well have been "shift".)