Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

# in qw

by Brovnik (Hermit)
on Sep 11, 2001 at 13:11 UTC ( [id://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 ?
--
Brovnik

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

    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

    -Blake

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: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 ./qw.pl 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>

    -Blake

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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://111688]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-20 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found