Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to add comments to multi-line quoted words?

Tags:

perl

The starting point:

my @array=qw(word1 word2 word3);

Now I want to put each word on a separate line:

my @array=qw(
   word1
   word2
   word3
);

Now I want to add comments:

my @array=qw(
   word1 # This is word1
   word2 # This is word2
   word3 # This is word3
);

The above of course does not work and generates warnings with use warnings.

So, what is the best way to create an array out of a commented list like the above?

like image 915
Michael Goldshteyn Avatar asked May 05 '14 14:05

Michael Goldshteyn


1 Answers

I'd recommend avoiding qw.

my @array = (
   'word1',  # This is word1
   'word2',  # This is word2
   'word3',  # This is word3
);

But you could use Syntax::Feature::QwComments.

use syntax qw( qw_comments );

my @array = qw(
   word1  # This is word1
   word2  # This is word2
   word3  # This is word3
);

Or parse it yourself.

sub myqw { $_[0] =~ s/#[^\n]*//rg =~ /\S+/g }

my @array = myqw(q(
   word1  # This is word1
   word2  # This is word2
   word3  # This is word3
));
like image 178
ikegami Avatar answered Oct 31 '22 05:10

ikegami