Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference betwen qq(<text>) and "<text>" in Perl?

Tags:

perl

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>')

like image 868
Kalcifer Avatar asked Nov 30 '25 12:11

Kalcifer


2 Answers

No.

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

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

Quote-Like Operators

like image 81
ikegami Avatar answered Dec 03 '25 08:12

ikegami


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 $$)"
like image 43
brian d foy Avatar answered Dec 03 '25 07:12

brian d foy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!