PBP: 033 String Delimiters

The PBP suggests using interpolating string delimiters only when they’re needed.  How much of my time do I get to waste changing single to double quotes because there’s a contraction in a message, or the other way because there isn’t?

This is one of the things I like least about turning on PerlCritic’s “brutal” level.  Every string can only use double quotes if it needs them.  When it needs them is a little harder than you might think, and I wind up converting back and forth constantly, spending lots of my time for something that I feel has little value.

The PBP suggests that unexpected variable interpolation is a source of many errors.  I find this a strange claim; variable interpolation is one of the basic parts of the language.  If a very new programmer is having trouble with it, that’s learning curve.  Using the different quotes to make the behavior less obvious won’t help.  A programmer with any level of experience won’t have complex issues here.

Here is the first place the PBP suggests using one of the q{} functions, too.  I find them hideously ugly and unreadable.  They are, in my opinion, ugly side effects of other things, to be used only when needed.  Single-quoting every string with a contraction in it isn’t “needed” in this case.  Don’t.  Changing the delimiters on q{} to let things work without backslashing is also ugly and much harder to read, in my opinion.

At least the book isn’t suggesting you avoid interpolating quotes for speed – there’s no need for that, as the interpreter notices and picks the right ones.

Use quotes, and be happy.  Don’t sweat interpolating quotes when something isn’t going to interpolate.  Where I use PerlCritic, this gets turned off right away.

4 Responses to “PBP: 033 String Delimiters”

  1. Dave Doyle (@meraxes) says:

    I tend to to think:

    1) Use whatever quotes you want
    2) Only interpolate a single simple scalar if you do so (no hash/array deref)
    3) If you need more than one varaible interpolated, use sprintf

  2. Max Lybbert says:

    Personally, I follow the “use single quotes if you can” guideline. Not because I’ve had any bugs caused by double quotes, but because I know I don’t have to bother looking for variable names in a non-interpolating string. (I don’t always have syntax highlighting; e.g., when I’m looking at diffs).

  3. I suggest taking a second look at q{} and qq{} for long strings, they’re actually very useful. For example, if you do need to switch between interpolating and non-interpolating, you make the change to one character at the beginning of the string. And the need to backwhack characters all but disappears.

    Heredocs are okay too, of course, but struggling with the outdented closing tags is irritating (I’ve gravitated to including leading underscores in the identifier, myself).

    My personal peeve with the q{} operators is that curly braces aren’t required, and programmers are constantly looking for something that seems neater like q|| or somesuch (had to deal with a block of javascript quoted by q|| the other day– at first, I didn’t get why the regexps had some many backwhacks– what is this elisp?).

  4. I like single-quotes for short strings and the q{} forms for longer strings. In particular, its good to use quotes that are different than the content of the strings so to minimize or save on having to backslash-escape; this is one reason that Perl allowing alternate quote chars than {} is useful.

Leave a Reply to Max Lybbert