Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

qq question

by Anonymous Monk
on Oct 18, 2005 at 15:08 UTC ( [id://501005]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,

I've got the following snippet:
#!/usr/bin/perl my $test = qq( func(1); func(2); func(3); func(4); ); print $test;
which prints out the following:
func(1); func(2); func(3); func(4);
My question is, why didn't the print end here:
func(1);
How did perl know how to go to the last );
??
Thanks,
Jonathan

Replies are listed 'Best First'.
Re: qq question
by inman (Curate) on Oct 18, 2005 at 15:16 UTC
    From the docs:

    Non-bracketing delimiters use the same character fore and aft, but the four sorts of brackets (round, angle, square, curly) will all nest, which means that q{foo{bar}baz} is the same as 'foo{bar}baz'

    The code falls into this category because the parens are balanced. If you unbalanced the parentheses(sp?) then you would end up with a syntax error.

Re: qq question
by swampyankee (Parson) on Oct 18, 2005 at 15:29 UTC
    qq (...) quotes everthing between delimiters, but you knew that. Perl is keeping track of the parentheses nesting depth, so there is something like this going on:
    $string = qq( # start the quote func(1 # push down to second level ln( # push down to third level e) # pop up to second level ); # pop up to first level ); # finish the quote
    After all, Perl's parser has to do something similar with an equation such as
    $a = $b + $c * ($d + $e * ($f + $g * ($h +)));

    emc

Re: qq question
by Moron (Curate) on Oct 18, 2005 at 16:47 UTC
    I think the underlying cause is that the '(' in the expression 'qq(' is being declared as a string delimiter but one which maintains the nesting rules of a bracket.

    Revealing tests cases include (with their effects):

    1) try with too many right brackets (perl will generate a syntax error on an attempt to nest to a depth of -1).

    2) try with too many left brackets (perl will complain using words like 'can't find a string terminator ")"'

    3) use a different nesting or non-nesting delimiter from the content and mismatch the brackets in the content as before, e.g. qq{(((((()} (perl will have no problem with this despite the mismatching brackets, because they are of a different type than the delimiter being declared for qq.)

    -M

    Free your mind

Re: qq question
by pg (Canon) on Oct 18, 2005 at 17:14 UTC

    If you run this:

    use strict; use warnings; my $test = qq(a (b c d); print $test;

    You get this error:

    Can't find string terminator ")" anywhere before EOF at math1.pl line +4.

    Which clearly indicates that perl follows the depth (matching) of '()' pairs.

Re: qq question
by ioannis (Abbot) on Oct 18, 2005 at 15:25 UTC
    The qq operator is not a macro text substitution. The purpose here is (among other things, see perlop) to enable interpolation. The relevant section in perlop is 'Gory details of parsing quote-like constructs'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://501005]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found