[Israel.pm] Quote Operators

Offer Kaye offer.kaye at gmail.com
Mon Mar 16 16:31:28 PDT 2009


On Mon, Mar 16, 2009 at 10:42 PM, Shmuel Fomberg wrote:
> Hi.
>
> Some interesting behavior:
>
> print q{abc{def}ghi};
> will print the equivalent to
> print 'abc{def}ghi';
> Using unbalanced brackes will be a syntax error, unless slashed:
> print q{prq{abc{de\{f}ghi}};
> will print: 'prq{abc{de{f}ghi}'
>

Yes, you must balance your brackets (which you did by escaping the 4th
opening one). See:
  http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators
To quote:
<blockquote>
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'

Note, however, that this does not always work for quoting Perl code:

	$s = q{ if($a eq "}") ... }; # WRONG

is a syntax error. The Text::Balanced module (from CPAN, and starting
from Perl 5.8 part of the standard distribution) is able to do this
properly.
</blockquote>

See also on the same page under:
http://perldoc.perl.org/perlop.html#Gory-details-of-parsing-quoted-constructs

"The most important Perl parsing rule is the first one discussed
below: when processing a quoted construct, Perl first finds the end of
that construct, then interprets its contents."

The way I understand this, is if you don't balance your delimiters,
you are confusing Perl as to where your quoted construct ends.

> this code:
> print qq#text here#;
> /something/;
> Is not quite like this code:
> print qq #text here#;
> /something/;
> But it is equal to this code:
> print qq !text here!;
> /something/;
>

Back in http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators
this is explained:

"There can be whitespace between the operator and the quoting
characters, except when #  is being used as the quoting character.
q#foo# is parsed as the string foo , while q #foo# is the operator q
followed by a comment. Its argument will be taken from the next line"

> How am I going to make my tokenizer identify all these? <facepalm>
>
> Shmuel.
>

PPI? (http://search.cpan.org/dist/PPI/)

Hope this helps,
-- 
Offer Kaye



More information about the Perl mailing list