On Wed, Apr 06, 2016 at 08:20:24PM +0100, Jonathon Fernyhough wrote:
> Hi,
> 
> I'm working my way through Learning Perl (6th) and Modern Perl (4th) and
> was wondering whether there are any (non-obvious) drawbacks to the
> different string quoting methods.
> 
> First up, double-quoted strings. The "usual method" of double-quoting
> has an alternative of qq{For a string with $variable interpolation};
> 
> qq{} obviously wins when there would otherwise be a lot of escaping, but
> are there any downsides of using this method more generally (other than
> double-quotes being two characters shorter)? For example, is it "faster"
> for Perl to parse a double-quoted string or does the compiler optimise
> this out so the methods are fundamentally equivalent?

Whether it is faster, slower or the same speed (and I really don't know
which it is) I am all but certain that you wouldn't be able to measure
the difference.  Do not optimise for speed until you really have to and
you have profiled your code to know which bits are taking the time.  I
guarantee you that it won't be this.

> Second, single- vs. double-quoted strings for non-interpolated use. In
> Ruby, I've been given the impression* that single-quoted strings are
> "more efficient" than double-quoted strings when interpolation is not
> required (that is, prefer 'Bob' over "Bob"). I'm wondering whether there
> is any similar recommendation for Perl?

Copy and paste my previous paragraph here.

With regard to single versus double quoting where there is no
interpolation, people have different ideas.  My considered opinion is to
use double quotes in all cases, except where it would lead to escaping
which would not be necessary with single quotes.

My reason is that Perl is by default interpolative and using single
quotes fights against that.  I have frequently had to change the quotes
when either adding or removing a variable from a string when working on
a code-base that prefers single quotes.

A few years ago, Tom Christiansen expanded on this in more detail than
you want to read.  Thankfully, he also summarised his argument.

Summary:
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-08/msg00402.html

Full (bottom section):
http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-08/msg00390.html

You'll notice that I disagree with Uri.  You should follow the coding
guidelines of any existing project you are working on, and make up your
own mind about what to do when you get to decide the guidelines.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to