hopes has asked for the wisdom of the Perl Monks concerning the following question:

I've allways thought that qq() and "" where "almost" the same.
If you think that are exactly the same operator, you may be surprised (as me now)running this code:
perl -e "print qq(1_000)"
and this
perl -e "print "1_000""
I'm now using perl, v5.6.1 built for MSWin32-x86-multi-thread Binary build 626 provided by ActiveState

Do anyone know more differences between "" and qq?
In the first, "1_000" is interpreted as a number, and in the second as a string. Why?
I've searched for documentation about this, but I didn't find nothing.
perl -e "$_="1_000";$_++;print" perl -e "$_=qq{1_000};$_++;print"


Hopes
$_=$,=q,\,@4O,,s,^$,$\,,s,s,^,b9,s, $_^=q,$\^-]!,,print

Edit Masem 2001-12-09 - Fixed title

Replies are listed 'Best First'.
Re: Differences between qq() and
by Beatnik (Parson) on Dec 09, 2001 at 18:37 UTC
    If you're using quotes (or double quotes, or a bunch of other metachars) withing quotes (or doublequotes etc), you have to escape em. qq() allows you to use doublequotes within doublequotes (since qq() are actually doublequotes).

    On windows if you want to use -e you HAVE to use doublequotes (unlike UN*X, where single quotes work too). If you want to use doublequotes within that line, escape them.
    perl -e "$_=\"1_000\";$_++;print" If you'd wanna use a ) within the qq()'ed statement, you'd have to escape it too. Note that qq() works with a few other chars too, such as qq[], qq{}, qq// and even qq##. And yes, that ALSO works for q(), qr(), qx() and qr().

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re (tilly) 1: Differences between qq() and ""
by tilly (Archbishop) on Dec 09, 2001 at 23:29 UTC
    The difference you point to is not a difference within Perl. Try writing a program in a file and seeing if they behave differently then. :-)
Re: Differences between qq() and ""
by Spenser (Friar) on Dec 10, 2001 at 11:39 UTC

    Okay, my comments are a bit off the track of hopes' question, but they're generally in the same area.  So, here they are for what they're worth:

    I'm still very much in the early stages of learning Perl, so I'm not sure of the subtle details of my observations.  Someone else might be able to elaborate on the causes or reasons.

    Having said that, I've noticed that I can use either print "......" or print qq|.....| (I prefer bars or pipes) without a problem, with the exceptions sited by others above regarding the considerations of using double-quotes within the double-quotes associated with the print statement.

    However, when I use the CGI module with the following line within my code:

    my $q = new CGI;

    I've noticed that I cannot be sloppy about my use of single-quotes and double-quotes, as I might otherwise.  Meaning, single and double quotes are no longer interchangeable.  The following line of code, as an example, is sensitive to quote choice when adding the line about "new CGI" mentioned above:
    print $q->start_form(-method=>'post',  -action=>"order_form.cgi?ord_id=$ord_id")

    The action reference to the cgi script has to be in double-quotes or the value of $ord_id won't come through; it will be passed as a literal value.  I'm not sure why the "new CGI" line causes this.  Just using the CGI module doesn't cause things to be so strict.  And, I don't think it's because of the commands I happen to be using, because there are some commands that I can use with the CGI module and not the "new CGI" statement (i.e., print "Location: order_form.cgi?ord_id=$ord_id \n\n" must be within double-quotes with "new CGI").

      This difference between single and double quotes has nothing to do with your using CGI, it is much more basic to the language than that. Double quotes interpolate, single do not (other than translation of \\ to \, and \' to '). Try it:
      my $foo = "This is a variable"; print "Double quotes: ($foo)\n"; print 'Single quotes: ($foo)\n';
      A book recommendation. merlyn's book Learning Perl gives an excellent overview of Perl that should do a lot to clear up any confusion about how quoting works, the various kinds of data structures, writing functions, what regular expressions are, and so on. I think it would be very helpful to you at your current stage of Perl development.