Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Some suggestions on coding style - a chance to critique

by moxliukas (Curate)
on Jun 26, 2002 at 14:49 UTC ( #177392=note: print w/replies, xml ) Need Help??

in reply to Some suggestions on coding style - a chance to critique

I'm a beginner at Perl too, so I'm not sure if my suggestions are quite relevant. However, I was surprised to see this sort of code:

my $redirect = ""; my $sendmail = "/usr/sbin/sendmail"; my $subject = "Form Submission Results";

I have always thought that I should avoid double quoted strings in expressions like these, because they take longer to parse (Perl has to do various substitutions, right?). So I would use single quoted strings in these examples. Please correct me if I am wrong, as I would be interested to read some discussion about this.

Replies are listed 'Best First'.
Re: Re: Some suggestions on coding style - a chance to critique
by staunch (Pilgrim) on Jun 26, 2002 at 15:08 UTC
    $ perl -MO=Deparse my $redirect = ""; my $sendmail = "/usr/sbin/sendmail"; my $subject = "Form Submission Results"; my $redirect = ''; my $sendmail = '/usr/sbin/sendmail'; my $subject = 'Form Submission Results'; - syntax OK
    I believe this is optimized away, so I think it really is only a matter of preference. I personally prefer to use single quotes when possible though.


Re: Re: Some suggestions on coding style - a chance to critique
by emilford (Friar) on Jun 26, 2002 at 15:03 UTC
    You're right! I looked at this post and they mentioned using single quotes when no interpolation of variables is needed. They also mentioned that this is a matter of personal preference. This probably would be good practice, but in my use, I'm not sure if it makes that big a difference. I wonder how much of a time difference single quotes vs. double quotes makes? Either way, thanks for the suggestion.
      Running this code:
      use Benchmark qw(cmpthese); cmpthese(10000000, { single => sub {$foo = ''}, double => sub {$foo = ""}, });
      produces these results on my machine:
      Rate double single double 2865330/s -- -2% single 2915452/s 2% --
      So it looks like single quotes may be slightly faster, but that may be within the margin of error. I generally use single quotes unless I need interpolation of variables, but it's probably just a matter of preference.

      -- Mike


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177392]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-06-23 14:31 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.