Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comments and qw

by Tanktalus (Canon)
on Feb 12, 2006 at 16:37 UTC ( #529650=perlquestion: print w/ replies, xml ) Need Help??
Tanktalus has asked for the wisdom of the Perl Monks concerning the following question:

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?

Comment on comments and qw
Select or Download Code
Re: comments and qw
by brian_d_foy (Abbot) on Feb 12, 2006 at 17:12 UTC

    Sure I've had a similar desire, but since Perl doesn't allow us to do what you want to do, I don't do that. :)

    If I really want to comment a list, I don't use qw(). Anything else that I might do is just added complexity that I don't need and will probably confuse later programmers.

    do_something($_) for ( 'foo', 'bar', # introduced November, 2005 'baz', # deprecated - remove by December, 2006 );
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: comments and qw
by Fletch (Chancellor) on Feb 12, 2006 at 18:17 UTC
Re: comments and qw
by rnahi (Curate) on Feb 12, 2006 at 18:33 UTC

    It should not be to hard to implement, since there is a similar mechanism to allow comments in regular expressions with the /x modifier.

    Here is a quick example of a qwx function.

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub qwx { my ( $text ) = @_; $text =~ s/\s*[#].*$//mg; return split ' ', $text; } my @items = qwx( ' this is # possibly a # commented list ' ); print Dumper \@items; __END__ $VAR1 = [ 'this', 'is', 'a', 'list' ];

      It might be useful to skip blank lines (or lines that just contain comments) like the real qw():

      sub qwx { local $_ = @_ ? $_[0] : $_; s/[#].*$//mg; s/^\s+//; s/\s+$//; s/\s+/ /g; return split ' ', $text; }

      Also, you could tweak the appearance of caller a little as follows:

      my @items = qwx" this is # possibly a # commented list ";

      Single quote won't work without a space before it.

        It might be useful to skip blank lines (or lines that just contain comments) like the real qw():
        That won't be necessary. split with a single space is a special case that splits on any number of whitespace, including newlines.

        Try it for yourself:

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub qwx { my ( $text ) = @_; $text =~ s/\s*[#].*$//mg; return split ' ', $text; } my @items = qwx" this is # possibly a # commented list # with stand-alone comments # and empty lines " ; print Dumper \@items; __END__ $VAR1 = [ 'this', 'is', 'a', 'list' ];
Re: comments and qw
by bsb (Priest) on Feb 13, 2006 at 09:47 UTC
    You could play bareword roulette:
    use strict; no strict 'subs'; do_something($_) for ( foo, # comment bar, baz, );
      You could also use => to effectively qw all the list members. (I use keywords here to show that there's no roulette being played. They're interpreted as strings.)
      do_something($_) for ( exit => # comment sub => exec => do_something => # It's a string here, not a sub call! ); sub do_something { print "Got ($_[0])\n"; }
      Keep in mind that qw and => have somewhat different rules about what they quote: qw effectively quotes each run of non-whitespace; => quotes only runs of "word" (alpha-num-underscore) characters, and interprets everything else.

      Caution: Contents may have been coded under pressure.
Re: comments and qw
by stefp (Vicar) on Feb 26, 2006 at 04:01 UTC
    TimToady had not seen this thread otherwise he would have advertised that was a no-brainer in Perl6. I asked him but could not find that thread again. Hopefully I crossed again it in "best nodes" So from the mouth of the horse^Wcamel and some other #perl6 usual suspects:
    Feb 23 00:05:33 <cognominal> recently in perlmonks someone wanted a + qw() that supported comments, is there an idiom for that in perl6? Feb 23 00:06:02 <Juerd> cognominal: <<>>, IIRC Feb 23 00:06:43 <cognominal> I was surprised not to see TimToady pu +shing a Perl6 solution :) Feb 23 00:07:10 <TimToady> cognominal: I don't think we actually sa +id anything about comments, but considering that it's "shell quoting" +, and shells have comments, they could sneak in there... Feb 23 00:07:12 <Juerd> It could still happen Feb 23 00:07:45 <Juerd> TimToady: But let's not have \\\n :) Feb 23 00:07:55 <TimToady> Didn't see that PM thread... Feb 23 00:08:03 <audreyt> shells have line-terminated comments Feb 23 00:08:13 <Juerd> audreyt: That was implied :) Feb 23 00:08:21 <TimToady> So put line breaks in. Feb 23 00:08:36 * audreyt purrs randomly Feb 23 00:09:51 <audreyt> my @x = << Feb 23 00:09:59 <audreyt> This is some splitted words # but not + this Feb 23 00:10:05 <audreyt> and this too # but not this Feb 23 00:10:07 <audreyt> >>;

    -- stefp

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://529650]
Approved by Corion
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-09-16 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (36 votes), past polls