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

In my programs, if I have a string that doesn't need variable or backslash interpolation, I take pains to use single quotes. The rationale is, "Why arouse all that interpolation machinery when you don't have to. The program will probably run faster without it." But I notice in the literature and in many CPAN modules I've examined that double quotes are used with reckless abandon when single quotes would do. So I'm wondering, "Am I really gaining anything by my practice? Or does a double-quoted string with nothing to interpolate get caught at compile-time, with the compiler substituting simpler, non-interpolating code?"

Replies are listed 'Best First'.
Re: Overuse of double quotes?
by autark (Friar) on Oct 24, 2000 at 10:15 UTC
    That should be easy to check:
    perl -MO=Deparse -le '$a = "foo"' -e syntax OK $a = 'foo';
    That was converted to singel quotes. What if we stick in something that can be interpolated ?
    perl -MO=Deparse -le '$a = "foo $$"' -e syntax OK $a = "foo $$";
    Wee, doublequotes. So indeed, it seems perl does its share of compiletime optimizations.

    -- Autark.

Re: Overuse of double quotes?
by davorg (Chancellor) on Oct 24, 2000 at 12:10 UTC

    autark has pointed out that Perl is quite capable of optimising the strings and using the correct quotes, but I always make sure that I only use double quotes when something is being interpolated within the string for another reason.

    I think that using the "correct" quotes is a useful code documentation tool. When people come along and maintain my code, the presence of double-quotes around a string should signal to them "look, there's something interesting going on in this string".

    But it probably just comes down to personal preference.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Ahh, one further question...
by Anonymous Monk on Oct 24, 2000 at 22:03 UTC
    Okay, thanks! Not to be nitpicky or anything, but just so I understand: It would appear that my practice might still be justified (doc issues aside) inside repetitively-executed eval blocks. Or is the compilation overhead a "push" as well?
    A reply falls below the community's threshold of quality. You may see it by logging in.