Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Quote and Quote-like Operators

by setebos (Beadle)
on Jan 15, 2009 at 18:30 UTC ( #736637=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Quote and Quote-like Operators
Download Code
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 jeffa (Chancellor) 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 see no difference between:
      my $string = q/{(foo => "'bar'")}/;
      and
      my $string = q{{(foo => "'bar'")}};

        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)
        

        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.

      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

Re: Quote and Quote-like Operators
by MidLifeXis (Prior) 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://736637]
Approved by Arunbear
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-09-02 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls