Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

In Love With Double Quotes

by revence27 (Novice)
on Aug 07, 2005 at 10:57 UTC ( #481652=perlmeditation: print w/replies, xml ) Need Help??

That title was just to get you to read past the title. The truth is that, in fact, I am going to bark a very short rant against double quotes, while I wait for the Monastery GongsTM to call us for lunch. I need a decent plate of
How often do you ever stop in the middle of a hack, and ask thine self whether || ! you should use double || single quotes?
This is something you won't find in any Write-Better-Perl-Today texts. Because it's a waste of resources to talk about something that has almost no importance.
But since I'm in the comfort of The Monastery, I'll rant.

'This hot Perlistic string'
is better, IMHO, than
"This hot Perlistic string"
Because, as you can see, there are no $vars in the string, so putting it in double quotes would burden perl for nothing.
Also, I think 'First half ' . $second_half is better than "First half $second_half", for the same reason.
I think if you have lots of stringin' around to do, this would be a neater thing to do.
Punch me in the face.

Replies are listed 'Best First'.
Re: In Love With Double Quotes
by bradcathey (Prior) on Aug 07, 2005 at 12:03 UTC

    This was a very recent discussion here at the monastery.

    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: In Love With Double Quotes
by davorg (Chancellor) on Aug 08, 2005 at 08:27 UTC

    Actually, Perl knows when you've used double quotes around a fixed string and changes them to single quotes internally.

    $ perl -MO=Deparse -le 'print "Fixed string"' BEGIN { $/ = "\n"; $\ = "\n"; } print 'Fixed string'; -e syntax OK $

    In my opinion, a much better reason for using single quotes for fixed strings is that it tells the next programmer to look at your code that nothing interesting is going on in that string.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: In Love With Double Quotes
by spiritway (Vicar) on Aug 07, 2005 at 23:35 UTC

    You're perhaps right about burdening Perl by using double quotes. Still, why should I burden myself, when I can dump the work onto Perl? Still, typing a single quote is easier, since I don't have to press the shift key, so I suppose you've got a point...

      Nah! Have the best of both: Spin the key 180 degrees and remap your keyboard.

      Be well,

Re: In Love With Double Quotes
by revence27 (Novice) on Aug 07, 2005 at 12:09 UTC
    Oh! I haven't been around much, reccently. Gon' read it.
    print "Something's gone wrong with my head: $!";
Re: In Love With Double Quotes
by japhy (Canon) on Aug 08, 2005 at 14:31 UTC
    I tend to use double quotes, almost exclusively, because if I do want to interpolate something into it later, I needn't worry about also fixing the quotes around the string as well. The only time I use single quotes is when I am dealing with a string that I know will never have any interpolation needed because it is holding some constant string that probably contains $'s, @'s, or \'s in it. That's really the only time I use single quotes. Oh, and hash keys that can't be barewords but will be constant.

    At all other times I use double quotes, because I'll invariably end up needing to interpolate something.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: In Love With Double Quotes
by EvanCarroll (Chaplain) on Aug 07, 2005 at 20:23 UTC
    But, this post brings one more thing to the table, a suggestion that a string concatenated with a variable is more legible than a variable interpolated in a double string. Which I don't believe. I would also be willing to cite a strong statistic from the Association of Made up Statistics, which was funded by the Organization of Statistics Pulled Out of My Ass it says, "In 100% of the cases studied explicit concatenation turned out to be a very bad idea."

    Evan Carroll
      If you don't interpolate variables, you must end up with some pretty messy code.

      Instead of
      print "On $todays_date you have received $num_emails emails from $num_ +people people in mailbox $mailbox\n";
      you would prefer this?
      print 'On ' . $todays_date . ' you have received ' . $num_emails . ' e +mails from ' . $num_people . ' people in mailbox ' . $mailbox . '\n';
      IMHO, the former is quite a bit easier on the eyes.
        I agree. I am the only person I know of who has gone on record has having intentionally tried both types as my default quoting style (A guide to coding Perl). I vote for double-quotes in the common case; when you consider that interpolation was designed to be convenient it is not surprising that it is.

        There have been many arguments in favor of using single-quotes as a marker that no interpolation is going to happen. This implies that double-quotes denote that interpolation is going to happen. I find this a subtle sort of commenting and would not count on any one's self discipline to be consistent at it unless I knew there was an effective system to enforce it. The idea shows an attention to detail that I can respect but I find the convention uninteresting and uninspiring. Usually strings are just not that interesting from a programmer's perspective. Often I may start a string without much thought as to what all will be in it when its contents are finalized.

        There are situations where quoting styles are worth attention. When generating templating text or code that has lots of special characters; in such cases it is worth avoiding " and ' and using the other quoting methods.

        Be well,

Re: In Love With Double Quotes
by danb (Friar) on Aug 08, 2005 at 16:16 UTC
    I'm surprised that no one has mentioned my favorite reason for using single quotes instead of double quotes (in this discussion or the last): more whitespace. Whitespace is pretty, and ' takes half as many pixels as ". Witness:
    my $foo = "fhwgads"; if ( $foo eq "fhwgads" ) { print "fh" . "wg" . "ads"; }
    my $foo = 'fhwgads'; if ( $foo eq 'fhwgads' ) { print 'fh' . 'wg' . 'ads'; }
    Doesn't the second example look *so* much better? Ahh, the beauty. Maybe I like whitespace a little too much?


      I can think of two reasons to use " by default instead of ':
      1. To disambiguate when using lots of `s, if your font isn't clear
      2. Easier to write text that uses contractions, e.g.
        open(my $fh, '/etc/motd') || die "Can't open /etc/motd: $!";
      Do you also write:
      instead of
      or does your "more whitespace" rule only apply to string quotes?
Re: In Love With Double Quotes
by Anonymous Monk on Aug 08, 2005 at 14:28 UTC
    You are wrong. Complete, and utterly wrong. It's better to use "This hot Perlistic string" then 'This hot Perlistic string'.

    Your argument that perl needs to do more work is a non-argument. If that difference is important to you you ought to be programming in C directly, as using perl makes C do more work than when you programmed in C directly.

    But for engineering reasons, it's better to use double quotes in a string that doesn't contain dollar (or at) signs or backslashes. The only case where the kind of quotes you use matters is if you need to change them. When would you need to change the quotes of "This hot Perlistic string" to single quotes? When you change it to something like 'This hot Perlistic string costs $100' - although you could still write "This hot Perlistic string costs \$100". But when would you need to change 'This hot Perlistic string'? For instance, if you change it into "This hot Perlistic string\n", or "This hot Perlistic $noun". And then there isn't a single keystroke alternative available that leaves in the single quotes. The latter kind changes are far more common than the former kind. Hence, it's better to use double quotes than single quotes - programmer time is more important than CPU time. (If CPU time was more important, you should have written it in C anyway). Oh, and anyone complaining he gets confused when seeing a double quoted string that doesn't have a variable inside it ought to be fired.

      For engineering reasons, I use single-quotes when I mean to use a literal, and double-quotes when I mean to interpolate. It's just a matter of doing what you're saying, and saying what you're doing.

      If I want to use a string constant, I use single quotes. No ambiguity. No mental gymnastics trying to figure out what '@$_$@#!!' means. It's a curse. Ok, so that doesn't come up too often. However, other interpolations, such as '', are quite frequent.

      If I want to interpolate strings, I use double quotes. No ambiguity. It's just a simple acknowledgement that I know what I'm doing, and what I'm doing is interpolation.

      People who mindlessly use only a single tool probably don't know there are other tools out there, and, moreover, probably don't really know their own tool properly. If you insisted on using double-quotes everywhere in your code while you worked for me, and in the style you present above, I'd have your attitude for supper. It's simply not conducive to either the workplace or perlmonks.

        Of course, one use double quotes when you want to interpolate, and single quotes when you don't. No question about that. The topic however is the large set of strings where "are we interpolating or not" isn't a sensible question. Are you interpolating in "Hello, world"? Perhaps you are, it just happens the set of variables you are interpolating happen to be zero. For me, double quotes also mean "I'm interpolating". But often, I interpolate zero variables.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://481652]
Approved by dfaure
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-05-23 13:37 GMT
Find Nodes?
    Voting Booth?