ts: double vs single quotes 
Author Message
 ts: double vs single quotes

As a C programmer I am in the habit of using only double-quoted
strings in perl. Such strings are subject to variable interpolation
and backslash interpretation. Is there enough of a performance
penalty here that I should consider using single-quoted strings
by default?

--ts

  --------------------------------------------------------------------
  Posted using Reference.COM                   http://www.*-*-*.com/
  FREE Usenet and Mailing list archive, directory and clipping service
  --------------------------------------------------------------------



Sat, 12 Aug 2000 03:00:00 GMT  
 ts: double vs single quotes


Quote:
> Is there enough of a performance
>penalty here that I should consider using single-quoted strings
>by default?

The difference is only in the amount of time it takes to compile the
script. Once the script is running, double quotes that perform no
variable interpolation and single quotes identical. You can see this
if you compile perl with the -DDEBUGGING option and run it with the
appropriate -D flag. (I keep around a special debugging version of
perl around to investigate things like this.)

#!/usr/bin/perl

$a = "double quotes";
$b = 'single quotes';

EXECUTING...

    =>
(quotetest:0)   enter
Entering block 0, type BLOCK
    =>
(quotetest:0)   nextstate
    =>
(quotetest:3)   const(PV("double quotes"))
    =>  PV("double quotes")
(quotetest:3)   gvsv(main::a)
    =>  PV("double quotes")  $b
(quotetest:3)   sassign
    =>  PV("double quotes")
(quotetest:3)   nextstate
    =>
(quotetest:4)   const(PV("single quotes"))
    =>  PV("single quotes")
(quotetest:4)   gvsv(main::b)
    =>  PV("single quotes")  $b
(quotetest:4)   sassign
    =>  PV("single quotes")
(quotetest:4)   leave
Leaving block 0, type BLOCK

Despite this, my personal, unwritten, style guidelines specify that
double quotes are only to be used for interpolation and backslash
interpretation. Then when I'm reading my code I can quickly see what
function the quoting is performing.

--
Andrew Langmead



Sat, 12 Aug 2000 03:00:00 GMT  
 ts: double vs single quotes

Quote:

> As a C programmer I am in the habit of using only double-quoted
> strings in perl. Such strings are subject to variable interpolation
> and backslash interpretation. Is there enough of a performance
> penalty here that I should consider using single-quoted strings
> by default?

i doubt the performance is altered significantly for it to be a
driving force in the (non)usage of single quotes. my rule is that
i only use interpolating quotes when interpolation is useful within the
quotes. this way i can quickly scan for variable locations, skipping
any single quotes i see. also, i just find ' to be cleaner in appearance
than ". though i do tend to avoid explicit concatenations:

  $potato = "i am a $monkey";
    instead of
  $potato = 'i am a ' . $monkey;

as well as 'print' with commas:

  print "i am a $monkey";
    instead of
  print 'i am a ', $monkey;

again because i find them cleaner in appearance,

cheers,

--



Sat, 12 Aug 2000 03:00:00 GMT  
 ts: double vs single quotes

Quote:

> As a C programmer I am in the habit of using only double-quoted strings in
> perl. Such strings are subject to variable interpolation and backslash
> interpretation. Is there enough of a performance penalty here that I should
> consider using single-quoted strings by default?

I would highly doubt it, but you should benchmark to be sure.


Sat, 12 Aug 2000 03:00:00 GMT  
 ts: double vs single quotes

Quote:


> > As a C programmer I am in the habit of using only double-quoted strings in
> > perl. Such strings are subject to variable interpolation and backslash
> > interpretation. Is there enough of a performance penalty here that I should
> > consider using single-quoted strings by default?

> I would highly doubt it, but you should benchmark to be sure.

i use the appropriate quote for the string. this is for semantic
purposes as it shows to me (and other who look at the code) which
strings are constants and which are interpolated. i find it annoying
when i see:

$foo = "constant string" ;

even though it is legal it looks wrong to me.

also single quotes reduce the need for quoting metachars that expand in
double quotes.

uri

--
Uri Guttman                     SYStems ARCHitecture and Software Engineering

http://www.sysarch.com                (781) 643-7504 x*2  FAX: (781) 643-2710
Try the Best Search Engine on the Net -------->  http://www.northernlight.com



Sat, 12 Aug 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Single quotes Vs. Double quotes

2. single vs. double quotes

3. Efficiency: single vs double quote

4. replace single quote to double quote with an example

5. replace single quote to double quote

6. changing a single quote string to a double quote string

7. performance penalty: bareword, single quoting, double quoting

8. replace single quote to double quote with an example

9. Converting single quoted string to double quoted string

10. Convert from DOUBLE-QUOTE to SINGLE-QUOTE interpretation

11. Pays to use single, rather than double, quotes?

12. Single and Double quotes

 

 
Powered by phpBB® Forum Software