2

In perl you can use both qq(<text>) and "<text>" to create double quoted text. Is there any difference between the two?

(Same question for q(<text>) and '<text>')

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
Kalcifer
  • 1,211
  • 12
  • 18
  • 1
    No, but let me point out a difference in usage, just in case: with the operator form we can then use double quotes inside, `qq("quoted")`, while with quotes we have to escape them. – zdim Dec 22 '22 at 07:47

3 Answers3

4

No.

"<text>" is short for qq"<text>".

'<text>' is short for q'<text>'.

Quote-Like Operators

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 3
    With q and qq you can use any character as delimiter that you dont't need inside the text, so you don't have to escape anything. – Tekki Dec 22 '22 at 08:03
  • 1
    Tad went to look at which characters can be used as delimiters: https://stackoverflow.com/a/5770976/2766176 When I expanded his script to handle everything from 0 to 0x10ffff, only about 25% can be used (but you expected that once you cross 0xFFFF). Also, there are various Unicode paired delimiters that are deprecated, such as 《. You can't use those without enabling a feature. – brian d foy Dec 22 '22 at 12:27
3

Deparse Perl programs to see how Perl interpreted you wrote. When you use the generalized quotes, you get back the single and double quotes:

$ perl -MO=Deparse -e '$s = q(abc)'
$s = 'abc';
-e syntax OK

$ perl -MO=Deparse -e '$s = qq/ab "c" d/'
$s = 'ab "c" d';
-e syntax OK

$ perl -MO=Deparse -e '$s = qq(abc$$)'
$s = "abc${$}";
-e syntax OK

Besides using these to allow the ' and " to appear in the string, I find them very handy for one liners so that the ' and " don't trigger shell problems:

% perl -le "print qq(PID is $$)"
brian d foy
  • 129,424
  • 31
  • 207
  • 592
1

In addition to them being identical, there is the point that you can use q() qq() with different delimiters (like you can with many Perl operators) to make quoting simpler. For example qq#The movie "Foo"#, q{Some other 'Foo'}. Compare

"Bob said, \"What's that?\""      'Bob said, "What\'s that?"'   qq(Bob said, "What's that?")
"Kim whispers, \"(...)\""         qq|Kim whispers, "(...)"|
s/http:\/\/www.example.com\//http:\/\/www.example.net\//  # "leaning toothpick syndrome" (1)
s#http://www.example.com/#http://www.example.net/#        # fixed

(1): leaning toothpick syndrome

TLP
  • 66,756
  • 10
  • 92
  • 149