Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

# in qw

by Brovnik (Hermit)
on Sep 11, 2001 at 13:11 UTC ( #111688=perlquestion: print w/replies, xml ) Need Help??
Brovnik has asked for the wisdom of the Perl Monks concerning the following question:

How do I quote # in
for (qw ( #FF0000 #00FF00 #0000FF ))
to avoid the
Possible attempt to put comments in qw() list at
message ?

Replies are listed 'Best First'.
Re: # in qw
by davorg (Chancellor) on Sep 11, 2001 at 13:30 UTC
Re: # in qw
by blakem (Monsignor) on Sep 11, 2001 at 13:45 UTC

    If all your elements start with '#' you can rip it out, then put it back in using map....

    for (map {"#$_"} qw(FF0000 00FF00 0000FF))
    As in:
    #!/usr/bin/perl -wT use strict; print "$_ " for (map {"#$_"} qw(FF0000 00FF00 0000FF)); print "\n"; =output #FF0000 #00FF00 #0000FF


Re: # in qw
by blakem (Monsignor) on Sep 11, 2001 at 13:39 UTC
    hmmm, I don't believe you can... from perlop
    qw/STRING/ Returns a list of the words extracted out of STRING, using embedded wh +itespace as the word delimiters. It is exactly equivalent to split(' ', q/STRING/); This equivalency means that if used in scalar context, you'll get spli +t's (unfortunate) scalar context behavior, complete with mysterious warnings. Some frequently seen examples: use POSIX qw( setlocale localeconv ) @EXPORT = qw( foo bar baz ); A common mistake is to try to separate the words with comma or to put +comments into a multi-line perlman:perlop-string. For this reason the -w switch produc +e warnings if the STRING contains the ``,'' or the ``#'' character.
    Reading that last part, it sounds like '#' will cause a warning in qw no matter what (well as long as warnings are on)

    Looks like you might be stuck with something like:

    split(' ',q/#FF0000 #00FF00 #0000FF/)
    As in:
    #!/usr/bin/perl -wT use strict; print "$_ " for (qw ( #FF0000 #00FF00 #0000FF )); print "\n"; print "$_ " for (split(' ',q/#FF0000 #00FF00 #0000FF/)); print "\n"; =output Possible attempt to put comments in qw() list at ./ line 4. #FF0000 #00FF00 #0000FF #FF0000 #00FF00 #0000FF
    note, the warning is from the original style qw quoting... comment that out, and the warning will be gone. </code>


Re: # in qw
by virtualsue (Vicar) on Sep 11, 2001 at 13:19 UTC
    Use single quotes or q// to avoid interpolation of the list elements and the warning will go away.

    for (q/#FF0000 #00FF00 #0000FF/)

    Update: D'oh!! See replies below.

    for (q/#FF0000/, q/#00FF00/, q/#0000FF/) would also work, but it's more typing.
      But q// will not return a list! qw// does.
        That's true, I just realized it.

        for (split /\s+/, q/#FF000 #00FF00 #0000FF/) or for ('#FF000', '#00FF00', '#0000FF')
Re: # in qw
by busunsl (Vicar) on Sep 11, 2001 at 13:23 UTC
    I assume thie script runs with -w or use warnings.

    So all I can come up with is drop warnings for the block in question:

    no warnings 'syntax'; for (qw ( #FF0000 #00FF00 #0000FF )) ... use warnings 'syntax';
(tye)Re: # in qw
by tye (Sage) on Sep 11, 2001 at 20:19 UTC

    That is an excellent idea. qw() already supports \ for escaping the closing delimiter (or repeats of the opening delimiter when using "paired" delimiters). So it makes sense to support "\," and "\#" to avoid the warnings and to support "\ " for including whitespace inside "words".

    Unfortunately, that isn't currently the case (and there would be a very minor backward compatability issue).

            - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://111688]
Approved by root
[choroba]: exceptions to this rule are rare

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2018-03-20 10:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.