[Melbourne-pm] Single vs double quotes: no real difference (was Re: Delivering PDFs via CGI)

Jacinta Richardson jarich at perltraining.com.au
Mon Jun 19 03:13:44 PDT 2006

Craig Sanders wrote:

> also, there's a difference between single-quote (') and double-quote
> (") characters. use single-quotes for fixed strings with no variable
> interpolation (and no escaped chars like \n) - e.g where you define my
> $filename, that's just a constant and doesnt need var. interp.. use
> double-quotes where you need them.
> there's a performance improvement for using
> single quotes when you dont need interpolation....minor, but it adds up
> if you're doing it inside a loop. and it's a good habit to develop.

I'd be *very* surprised if you could find a consistent, detectable performance
improvement, even for large programs with lots of double quoted strings.
According to tye: "Single- and double-quoted strings both have to be parsed at
compile time. Both get compiled into constants and so there is no speed
difference between the two at run time."

In fact, tye said he had to resort to a 3200-byte string in order to detect at
20% slow-down for double-quoted strings at compile time.  Thus his single quoted
string program started up 0.2 seconds faster.


Being something that happens at compile time, it makes no difference to the
run-time speed whether you use single or double quoted strings in a loop.

This is supported by using B::Deparse.

	jarich at tabius:/tmp$ cat test.pl
	while(1) {
	        my $filename="c:\\temp\\mypdf.pdf";

	jarich at tabius:/tmp$ perl -MO=Deparse test.pl
	while (1) {
	    my $filename = 'c:\\temp\\mypdf.pdf';
	test.pl syntax OK

It is unfortunate that this myth keeps being spread.  What *does* matter about
quoting is understanding.  If you use only single quoted strings where you don't
need interpolation, and double quoted strings when you do; then you train
someone who is reading your code to expect to see interpolation when they see
double quotes, and they know that there's been a mistake when they don't.

If you use only double quotes; your reader has to read every string to see
whether interpolation is happening; and also think about whether interpolation
*should* be happening.

If you mix up your quotes with no clear reason (that is sometimes you use single
and other times double for literal data) then the person reading your code will
just go nuts.

More at http://www.nntp.perl.org/group/perl.perl6.stdlib/196  search for "On
whether or not to always use double quotes".  Having said that, I prefer double
quotes most of the time.

All the best,


   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |

More information about the Melbourne-pm mailing list