Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

qw comments

by mdupont (Scribe)
on Nov 09, 2001 at 22:33 UTC ( #124430=perlmeditation: print w/replies, xml ) Need Help??

Dear fellow monks,

I have a problem with qw, one of my favorite little perl tools.

Like all our good friends, the better you know them, the more little things that get on your nerves and the more you want to change them for the better!

Other monks have meditated on qw before , here is a replacement for it! :)

I want to have it ignore everything after a # sign on a line, i have a big ol' qw and want to comment one element out. What a pain!

Also it would be nice to explicitly create an undef, newline or \r element and not have it as a string.

Maybe it would look like this :

{ my %translate =( '\n'=>"\n", '\r'=>"\r", '\t'=>"\t" ); my $regex=join "|", map { quotemeta $_ } keys %translate; sub myqw { local $_ = shift; s/\#.+//g; # bye bye comments return map { if ($_ eq '\undef') { undef; # undef element in the array } else { s/($regex)/$translate{$1}/ge; # safer than eval $_; } } split " "; } } # test it print join "|", myqw (q[ this is a set of words # and they are commented as well a b b\t\te # b tab tab e c # d # remove d because we wont need it e\ne # e newline e g \undef h ]); __END__ this|is|a|set|of|words|a|b|b e|c|e e|g||h
thanks to demerphq in advance for the review (and lots of tips) on the post!

Edit: chipmunk 2001-11-09

Replies are listed 'Best First'.
(jeffa) Re: qw comments
by jeffa (Bishop) on Nov 10, 2001 at 07:12 UTC
    If you don't mind the warnings that are generated with warnings turned on, here is a simple solution:
    my @arry = grep /^[^#]/, qw( one two three #four five );
    It doesn't catch comments from the middle, but that's because i chose not to - just a simple comment at the beginning usually suffices.


    (the triplet paradiddle)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://124430]
Approved by root
[1nickt]: Discipulus I was sleeping in :-) It's a Holyday here. No work today. Just eating. Enjoy your day everymonk!

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2017-11-23 14:01 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (336 votes). Check out past polls.