http://qs321.pair.com?node_id=736637

setebos has asked for the wisdom of the Perl Monks concerning the following question:

perlop says the following about some of the quote-like operators:

Customary Generic Meaning Interpolates '' q{} Literal no "" qq{} Literal yes `` qx{} Command yes* qw{} Word list no qr{} Pattern yes*


Talking from experience, many programmers tend to use [] for qw, {} for q or qq or vice versa and etc. And they make "steel" rules about their own kind of quoting, the problem is that the other colleagues rarely know or remember those rules, because they have thier own or don't case (and why should they) or do not use those operators.

But does it make any sense, isn't it it much better if everyone uses these operators exactly in the way they are described in Perl manuals?

Replies are listed 'Best First'.
Re: Quote and Quote-like Operators
by jeffa (Bishop) on Jan 15, 2009 at 19:08 UTC

    No. One should choose the delimiters based on what they are wanting to avoid having to escape. If we used them only with {} then we would have to escape {} every time. Best to pick something else:

    my $string = q/{(foo => "'bar'")}/;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      I used slash (/) delimiters for the longest time in my code, until it dawned on me that using a vi-compatible delimiter pair would allow me to bounce on the % key to go from the beginning to the end. (I believe this is written somewhere in the Perl documentation).

      As a result, qw() has become my preferred idiom, as has qq{}. Looking back at older code these days I find that q/a string/ lacks symmetry, and thus beauty.

      Getting back to the OP, isn't it much better if everyone uses these operators exactly in the way they are described in Perl manuals, I would say yes, that's a sensible default. But one should know when (and why) to break the rules.

      • another intruder with the mooring in the heart of the Perl

      I see no difference between:
      my $string = q/{(foo => "'bar'")}/;
      and
      my $string = q{{(foo => "'bar'")}};

        That's only true when the curlies are properly nested within the string. Again, it depends. Sometimes the test of the proper nesting is a plus, sometimes it's a bother.

        My syntax highlighter does. Perhaps i did not pick the best example. My point is still valid.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Quote and Quote-like Operators
by MidLifeXis (Monsignor) on Jan 15, 2009 at 19:25 UTC

    Look at the paragraph above that chart

    While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them. In the following table, a "{}" represents any pair of delimiters you choose.

    --MidLifeXis

Re: Quote and Quote-like Operators
by gaal (Parson) on Jan 15, 2009 at 19:06 UTC
    What do you mean, pick [] for qw? That you've seen people pick different delimiters for different quotelike operators and stick with them? I've not seen this happen. But the whole point of freedom in delimiters is that you choose something that makes sense for whatever you're quoting in that instance. Just like s/// is silly on pathnames (because of "leaning toothpick syndrome"), qw() is a bad choice if one of the things you're quoting is ")-:".
Re: Quote and Quote-like Operators
by alfie (Pilgrim) on Jan 15, 2009 at 21:12 UTC
    I wonder about how you claim that many programmers tend to use [] for qw. I always thought that qw() is the canonical way to use them. Of course it depends on the contents, but if you take a look at e.g. perldoc -f use and the examples therein - of course left aside what others noted already.

    --
    use signature;
    signature(" So long\nRhonda");
      Well, I'm one of the people that use [] for qw. I very seldomly use () as delimiters; parenthesis already have a couple of meanings, using them as delimiters as well doesn't contribute to code clearity in my book.

      My favourite delimiters are:

      Operator Pref. Other Delim. Delims q, qq, qx {} <> // !! qw [] // {} <> s, m, qr // {} <> !! y, tr // [] {} <>
      I never use '#' as a delimiter (althought it's popular with many people).

      As you can see, all the delimiters I often use tend to be long, narrow, and mostly vertical.