-3

In Perl, what is the difference between quoting with double quotes("), single quotes('), and grave accents(`)?

This code:

#!/bin/env perl
use v5.10;
say "Hello";
say 'Hi';
say `Hola`;

gives the following result:

Hello
Hi

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
user1080381
  • 1,597
  • 1
  • 16
  • 22
  • 5
    http://perldoc.perl.org/perlop.html – Matt Jacob Dec 02 '16 at 14:49
  • 1
    I don't actually think this is very off-topic. It's a valid question, though it could have been researched by the OP. This stuff is very basic, but it's not easy to find if looking in perldoc directly. – simbabque Dec 02 '16 at 15:03
  • @MattJacob In my case it was not 10 seconds of googling... – user1080381 Dec 02 '16 at 15:21
  • Let's not start a discussion here. There are currently two close votes on the question, both with the off-topic reason _request to find an off-site resource_. I didn't agree with those, so I voiced that disagreement. @MattJacob I think there was indeed. But the community decided to remove that (I'm not going to find a meta thread now). I still think it's a good question, though actually googling part of it finds http://stackoverflow.com/questions/943795/whats-the-difference-between-single-and-double-quotes-in-perl, which is a near-duplicate. The backtick-angle is missing there. – simbabque Dec 02 '16 at 15:32

1 Answers1

5

Single quotes ''

Construct strings without interpolation. There is also a q() operator that does the same.

my $foo = 'bar';
print '$foo'; # prints the word $foo
print q($foo); # is equivalent

You would use single quotes when you just have text and there are no variables inside the text.

Double quotes ""

Construct strings with interpolation of variables. There is also a qq() operator that does the same.

my $foo = 'bar';
print "$foo"; # prints the word bar
print qq($foo); # is equivalent

Use these if you want to put variables into your string. A typical example would be in an old-fashioned CGI program, where you see this:

print "<td>$row[0]</td>";

The qq() variant comes in handy if your text contains double-quotes.

print qq{<a href="$url">$link_text</a>}; # I prefer qq{} to qq()

This is way easier to read than escape all the quotes.

print "<a href=\"$url\">$link_text</a>"; # this is hard to read

Backticks ``

Shell out and execute a command. The return value of the other program is returned. There is also a qx() operator that does the same. This interpolates variables.

print `ls`; # prints a directory listing of the working directory
my $res = qx(./foo --bar);

Use this if you want to write a script that is a bit more powerful than a shell script, where you need to call external programs and capture their output.


All the interpolating ones can only interpolate variables, not commands.

my $foo = 1;
my $bar = 2;
print "$foo + $bar";

This will print 1 + 2. It will not actually calculate and print 3.


All of those (and more) are explain in perlop under Quote and Quotelike operators.

Graham
  • 7,431
  • 18
  • 59
  • 84
simbabque
  • 53,749
  • 8
  • 73
  • 136