Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Use of single quotes versus double quotes

by grinder (Bishop)
on Dec 08, 2006 at 19:06 UTC ( [id://588673]=note: print w/replies, xml ) Need Help??


in reply to Use of single quotes versus double quotes

Are there huge time savings

In this case, absolutely none. Both forms are compiled down into constant strings:

  • Double quoted:
    % perl -MO=Concise -e 'print qq{hello, world}' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const(PV "hello, world") s ->5
  • versus single quoted:
    % perl -MO=Concise -e 'print q{hello, world}' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const(PV "hello, world") s ->5 -e syntax OK

The compiler will also fold things like "hello, world\n" and 'hello, world' . "\n" into the same constant strings. It's only when interpolation in a double string comes into play that things change:

% perl -MO=Concise -e 'print qq{hello, $world\n}' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <@> print vK ->a 3 <0> pushmark s ->4 - <1> ex-stringify sK/1 ->9 - <0> ex-pushmark s ->4 8 <2> concat[t2] sKS/2 ->9 6 <2> concat[t1] sK/2 ->7 4 <$> const(PV "hello, ") s ->5 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*world) s ->6 7 <$> const(PV "\n") s ->8 -e syntax OK

... which is only slightly different to...

% perl -MO=Concise -e 'print qq{hello, } . $world . qq{\n}' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <@> print vK ->a 3 <0> pushmark s ->4 8 <2> concat[t4] sKS/2 ->9 6 <2> concat[t2] sK/2 ->7 4 <$> const(PV "hello, ") s ->5 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*world) s ->6 7 <$> const(PV "\n") s ->8 -e syntax OK

The above both amount to much the same thing. So basically, you can always use double quotes, interpolate when you need to, and do not worry about performance.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Use of single quotes versus double quotes
by gam3 (Curate) on Dec 08, 2006 at 23:31 UTC
    You still need to take into account the complile time (if that is the correct terminology). But it does not seem that there is any real difference there either.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://588673]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found