Commented braces

Discussion in 'Perl Misc' started by Michele Dondi, Sep 12, 2007.

  1. 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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 12, 2007
    #1
    1. Advertising

  2. Michele Dondi

    Paul Lalli Guest

    On Sep 12, 5:03 am, Michele Dondi <> wrote:
    > 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
     
    Paul Lalli, Sep 12, 2007
    #2
    1. Advertising

  3. On Wed, 12 Sep 2007 07:13:29 -0700, Paul Lalli <>
    wrote:

    >`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
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 12, 2007
    #3
  4. On Thu, 13 Sep 2007 23:52:09 -0700, "Wade Ward" <>
    wrote:

    >Do you know how to reach Mr. Dondi through e-mail?


    <ti tod nfni tod im tod mcl ta razalb>

    Or else I'm on Jabber as <>.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Sep 14, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Heavey
    Replies:
    1
    Views:
    378
    Oisín Grehan
    Dec 2, 2003
  2. =?Utf-8?B?YWRvbGYgZ2FybGlj?=

    BUG: commented out server side controls in html still run

    =?Utf-8?B?YWRvbGYgZ2FybGlj?=, Nov 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    431
    John Saunders
    Nov 22, 2004
  3. Rod Snyder

    commented out form tag causing error

    Rod Snyder, Sep 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    354
    Rod Snyder
    Sep 14, 2005
  4. bronski
    Replies:
    0
    Views:
    455
    bronski
    Feb 16, 2005
  5. Bertrand Mollinier Toublet

    Re: non commented lines

    Bertrand Mollinier Toublet, Jul 25, 2003, in forum: C Programming
    Replies:
    1
    Views:
    351
    Ravi Uday
    Jul 28, 2003
Loading...

Share This Page