Commented braces

M

Michele Dondi

This is something obvious, but it may not be entirely obvious to all.
So I'm copying it here for the benefit of potential readers.

Originally from:

http://perlmonks.org/?node_id=638287


Original question (sanPerl)
------------------------------------

Dear Monks,
I am trying to execute the code given below.

use strict;
my $abcd = "Hello how r you?";
my $hello="Hello";
$abcd =~ s{\w}
{
#{
#}{
if ($hello)
{"1"}
else
{"0"}
}exgs;
print $abcd;

It is giving me error

Substitution replacement not terminated at test.pl line 5.

When I removed line 6 & 7 the code looked as below

use strict;
my $abcd = "Hello how r you?";
my $hello="Hello";
$abcd =~ s{\w}
{
if ($hello)
{"1"}
else
{"0"}
}exgs;
print $abcd;

It works proper and gives me output 11111 111 1 111? My question is,
why the commented braces are playing role in this error ? Have I done
something wrong here?


Reply (moritz)
------------------------------------

The problem is that the code is potentially ambigous.

There are two ways to parse it:

s{...}{
# much stuff here
}x

The other, but less obvious, ist this:

s{...}{
# some characters
{}{
# more characters...
}
# no terminator

When perl tries to parses the regex it doesn't know if there is going
to come an /x modifier, so the braces have to be balanced - even in
comments.

That's one of the reasons Perl 6 puts the modifiers at the start of
the regexes.


Reply (ikegami)
------------------------------------

If Perl were to guess that "#" indicates a comment, it'll introduce a
paradox. Consider

s{foo}{
bar
#}xe
}

If Perl stops at the second "}", then the replacement expression is
not code and "#" are not comments and Perl should have stopped at the
first "}".

If Perl stops at the first "}", then the replacement expression is
code ("e") and "#" are comments ("x") and Perl should have stopped at
the second "}".

Perl needs to find the end of the operator to find the "e" and "x"
flags. To find the end of the operator, Perl initially treats the
expression as a replacement string. When the "e" flag is found is the
replacement string is reparsed as code. Only then does "x" have any
meaning.

Perl 6 fixes this by placing the flags before the replacement
expression.

------------------------------------


Other replies at the link above.


Michele
 
P

Paul Lalli

This is something obvious, but it may not be entirely obvious to all.
So I'm copying it here for the benefit of potential readers.

Originally from:

http://perlmonks.org/?node_id=638287

Original question (sanPerl)
------------------------------------

Dear Monks,
I am trying to execute the code given below.

use strict;
my $abcd = "Hello how r you?";
my $hello="Hello";
$abcd =~ s{\w}
{
#{
#}{
if ($hello)
{"1"}
else
{"0"}
}exgs;
print $abcd;

It is giving me error

Substitution replacement not terminated at test.pl line 5.

When I removed line 6 & 7 the code looked as below

use strict;
my $abcd = "Hello how r you?";
my $hello="Hello";
$abcd =~ s{\w}
{
if ($hello)
{"1"}
else
{"0"}
}exgs;
print $abcd;

It works proper and gives me output 11111 111 1 111? My question is,
why the commented braces are playing role in this error ? Have I done
something wrong here?

`perldoc perlre` explains this just fine, IMO:

The "/x" modifier itself needs a little more explanation.
<snip>
Note that you have to be careful not to include the pattern
delimiter in the comment--perl has no way of knowing you did
not intend to close the pattern early. See the C-comment
deletion code in perlop.


Paul Lalli
 
M

Michele Dondi

`perldoc perlre` explains this just fine, IMO:

I wasn't suggesting that it doesn't. It's a little piece of info that
may just elude someone's attention and intuition.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top