I find myself doing this a fair bit (though with significantly longer lists - anywhere from 2 to 20 entries, depending):
do_something($_) for qw(
foo
bar
baz
);
It allows me to trivially add something or remove something. Well, removing isn't quite as nice as with regular lists - I can't just comment it out, I have to delete the whole line. However, what I can't do with this is do something like:
do_something($_) for qw(
foo
bar # introduced November, 2005
baz # deprecated - remove by December, 2006
);
(This does actually tell me it can't do it with a warning message:
Possible attempt to put comments in qw() list at ./x.pl line 10.) The closest I can come up with is something like this:
#!/usr/bin/perl
use strict;
use warnings;
do_something($_) for qw(
foo
bar # introduced November, 2005
baz # deprecated - remove by December, 2006
);
print "-----------------\n";
do_something($_) for words( <<WORDS );
foo
bar # introduced November, 2005
baz # deprecated - remove by December, 2006
WORDS
sub do_something
{
print " [$_[0]]\n";
}
sub words
{
my @lines = split /\n/, shift;
s/#.*$// for @lines;
map { split ' ', $_ } @lines;
}
However, I'm pretty sure that it's way slower than any solution that allows the perl parser to do it. And, for some reason, it just doesn't
feel elegant. And, for that reason, I haven't actually gone there - I'm not currently inserting comments into my lists.
Has anyone ever had a similar desire? What solution did you use?