Re: Can I avoid 'Possible attempt to put comments in qw() list '?

Discussion in 'Perl Misc' started by Uri Guttman, Apr 15, 2012.

  1. Uri Guttman

    Uri Guttman Guest

    >>>>> "HL" == Henry Law <> writes:

    HL> I have a hash of of lists defined along these lines

    HL> Ab => qw(Ab Bb C DB Eb F G)

    HL> Some of them quite legitimately have hash characters in them, but if I
    HL> write this

    HL> D => qw(D E F\# G A B C\#)

    where did you get the idea you needed to escape # in qw? inside qw (and
    usually in other q* things) the only thing that needs escaping is the
    closing delimiter. this is why you can set the delimiter to any
    reasonable char.

    perl -le '@a=qw( a bb # cc ##); print map "[$_] ", @a'
    [a] [bb] [#] [cc] [##]

    uri
    Uri Guttman, Apr 15, 2012
    #1
    1. Advertising

  2. Uri Guttman

    Guest

    Uri Guttman <> wrote:
    > >>>>> "HL" == Henry Law <> writes:

    >
    > HL> I have a hash of of lists defined along these lines
    >
    > HL> Ab => qw(Ab Bb C DB Eb F G)
    >
    > HL> Some of them quite legitimately have hash characters in them, but
    > if I HL> write this
    >
    > HL> D => qw(D E F\# G A B C\#)
    >
    > where did you get the idea you needed to escape # in qw? inside qw (and
    > usually in other q* things) the only thing that needs escaping is the
    > closing delimiter. this is why you can set the delimiter to any
    > reasonable char.
    >
    > perl -le '@a=qw( a bb # cc ##); print map "[$_] ", @a'
    > [a] [bb] [#] [cc] [##]


    Which also generates warnings if you have turned on.

    The OP can turn them off with:

    no warnings qw{qw};

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Apr 15, 2012
    #2
    1. Advertising

  3. Uri Guttman

    Uri Guttman Guest

    >>>>> "x" == xhoster <> writes:

    x> Uri Guttman <> wrote:

    >> perl -le '@a=qw( a bb # cc ##); print map "[$_] ", @a'
    >> [a] [bb] [#] [cc] [##]


    x> Which also generates warnings if you have turned on.

    x> The OP can turn them off with:

    x> no warnings qw{qw};

    correct. i was surprised to see those warnings when i added -w. easy to
    suppress as you did and much better than trying to escape them.

    uri
    Uri Guttman, Apr 16, 2012
    #3
  4. Uri Guttman <> writes:
    >>>>>> "HL" == Henry Law <> writes:

    >
    > HL> I have a hash of of lists defined along these lines
    >
    > HL> Ab => qw(Ab Bb C DB Eb F G)
    >
    > HL> Some of them quite legitimately have hash characters in them, but if I
    > HL> write this
    >
    > HL> D => qw(D E F\# G A B C\#)
    >
    > where did you get the idea you needed to escape # in qw? inside qw (and
    > usually in other q* things) the only thing that needs escaping is the
    > closing delimiter.


    This is not quite true: If 'the delimiters'(s) are 'some kind of
    [pairing] brackets' (at least (), {} and []), 'the closing delimiter'
    only needs to be escaped if it appears at the same nesting level as
    the opening delimiter, eg

    [rw@sapphire]~ $perl -e '$a = qw[[[]]\]]; print $a, "\n";'
    [[]]]
    Rainer Weikusat, Apr 16, 2012
    #4
  5. Henry Law <> writes:
    > On 15/04/12 23:19, Uri Guttman wrote:
    >
    >> where did you get the idea you needed to escape # in qw? inside qw (and
    >> usually in other q* things)

    >
    > OK, OK I'll go sit on the naughty step. The answer to your question
    > is that the very first time I did something like that I spent about
    > 650 msec wondering how the compiler was implemented and decided the
    > safest thing to do was to escape them.


    In situations likes this, it is usually a good idea to consult the
    documentation.

    Comments
    Text from a "#" character until the end of the line is a
    comment, and is ignored. Exceptions include "#" inside a
    string or regular expression.
    Rainer Weikusat, Apr 16, 2012
    #5
  6. 16-04-2012, Rainer Weikusat <> ÐÉÛÅÔ:
    > Uri Guttman <> writes:
    >> where did you get the idea you needed to escape # in qw? inside qw (and
    >> usually in other q* things) the only thing that needs escaping is the
    >> closing delimiter.

    >
    > This is not quite true: If 'the delimiters'(s) are 'some kind of
    > [pairing] brackets' (at least (), {} and []), 'the closing delimiter'
    > only needs to be escaped if it appears at the same nesting level as
    > the opening delimiter, eg
    >
    > [rw@sapphire]~ $perl -e '$a = qw[[[]]\]]; print $a, "\n";'
    > [[]]]


    Essentially, this means that there are 3 characters which need to be
    escaped: opening, closing and backslash.

    powdermilk:~->perl -wle 'print for qw[a \\[] b]'
    a
    \[]
    b
    powdermilk:~->perl -wle 'print for qw[a \[ b]'
    a
    [
    b
    powdermilk:~->perl -wle 'print for qw[a \] b]'
    a
    ]
    b

    Thanks,
    Ilya

    P.S. I was completely missing that this applies to q() and qw() as well...
    Ilya Zakharevich, Jun 7, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexander Malkis
    Replies:
    8
    Views:
    501
    Alexander Malkis
    Apr 14, 2004
  2. Roger23
    Replies:
    2
    Views:
    977
    Roger23
    Oct 12, 2006
  3. Sébastien de Mapias
    Replies:
    7
    Views:
    840
    Daniel Pitts
    May 29, 2008
  4. sameem
    Replies:
    3
    Views:
    263
    Joe Kaplan
    Apr 4, 2008
  5. GMI
    Replies:
    3
    Views:
    468
    Tad McClellan
    Jun 19, 2005
Loading...

Share This Page