Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Overuse of double quotes?

by Anonymous Monk
on Oct 24, 2000 at 10:07 UTC ( #38081=perlquestion: print w/replies, xml ) Need Help??
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.


    "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?
      Every eval loads a new Perl interpreter, not something you're interested in in the long run. Try to stick with one process during the life of the interpreter. This includes using system(). system(), I find, is most often used as a shortcut to avoid using a perl module that's more fitted to do a task. Also, it will in the end simplify the entire situation and error handling.
      AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.

        eval does not load another interpreter nor does it create another process.

        eval is slower than most alternatives so you try to avoid doing eval inside of a loop or an oft-used subroutine. But eval isn't so slow that you should be too afraid to use it. But there are also plenty of reasons other than performance to not use eval most of the time.

        Back to the question, the speed of " vs. ' inside an eval would be nearly impossible to detect, much less being something to worry about. After all, ' still has to check for \\ and \'.

                - tye (but my friends call me "Tye")

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://38081]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-08-19 20:50 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (187 votes). Check out past polls.