Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Commented braces in Code

by sanPerl (Friar)
on Sep 11, 2007 at 12:46 UTC ( #638287=perlquestion: print w/replies, xml ) Need Help??
sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

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 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?

Replies are listed 'Best First'.
Re: Commented braces in Code
by ikegami (Pope) on Sep 11, 2007 at 13:21 UTC

    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.

Re: Commented braces in Code
by moritz (Cardinal) on Sep 11, 2007 at 12:59 UTC
    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.

Re: Commented braces in Code
by ww (Archbishop) on Sep 11, 2007 at 13:17 UTC
    Part of the problem is the miscount of the (commented) curly braces in lines five and six:
    006: #{
    007: #}{

    Note also that the problem version runs correctly when written with slashes rather than curly braces:

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

    Output: 11111 111 1 111?

    The capability of using alternatives to slashes in regexen is, at least as I understand it, to make the notation more readable. For me, at least, your use of curly braces makes it more difficult to read, particularly given their use for a quite different purpose within the substitution.

Re: Commented braces in Code
by CountZero (Bishop) on Sep 11, 2007 at 20:03 UTC
    One really wonders what could be the use or purpose of those commented out braces?


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      This is just a code given for the sake of presentation to PerlMonks site. The actual code is 50k lines long. When we write a big code and that too in a collaborative developement envirinment then some programmers comments some statements to help them debugging it future. Sometimes the code written in the past is saved in comments to understand the origin of the latest modification. Some times the Comments are too descriptive which includes '{' as a part of text
      Whether one likes it or Not This all happens in real world.
      I found this problem while debugging a big code for the said error and I posted this query to Perlmonks.
      This is a good problem from coding guidelines point of view, and I have communicated this issue to all my programmers and I have also updated Coding guidelines document
      I hope Monks would concentrate on technicality of the issue rather than practical usage. in real world we need to use all kinds of permitation and combination to get the work done through programming, hence knowledge of such exceptions is very important

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://638287]
Approved by Corion
Front-paged by jzb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-01-22 03:25 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (231 votes). Check out past polls.