m///x and slashes in comments: bug or feature?

Discussion in 'Perl Misc' started by Ronny, Jun 7, 2006.

  1. Ronny

    Ronny Guest

    My perl is 5.8.3, and when I execute the following program:

    #!/usr/local/bin/perl -ws
    use strict;
    "dummy" =~ /
    first|
    second| # this comment has a/slash
    third
    /x;

    I get the error message:

    Bareword found where operator expected at ./matcherr.pl line 5, near
    "second| # this comment has a/slash"
    (Might be a runaway multi-line // string starting on line 3)


    The reason is the '/' in the comment. If I change the line to:

    second| # this comment has no slash

    the program runs fine. This surprises me, because as I have understood
    the
    'x' modifier, a comment within the pattern should eat up everything
    until the
    end of the line. Seemingly, parsing for the terminating "/" happens
    BEFORE
    white space inside the pattern is removed. Or did I misunderstand
    something
    here?

    Ronald
    Ronny, Jun 7, 2006
    #1
    1. Advertising

  2. Ronny

    Mirco Wahab Guest

    Thus spoke Ronny (on 2006-06-07 14:49):

    > #!/usr/local/bin/perl -ws
    > use strict;
    > "dummy" =~ /
    > first|
    > second| # this comment has a/slash
    > third
    > /x;
    > ...
    > The reason is the '/' in the comment. If I change the line to:


    How would the parser know what you intended here?
    "dummy" =~ /
    first|
    second| # this comment has a/s

    would be "valid" also.

    You have to make sure to give
    some impression to the parser
    in what you want ;-)

    "dummy" =~ {
    first|
    second| # this comment has a/slash
    third
    }x;

    Regards

    Mirco
    Mirco Wahab, Jun 7, 2006
    #2
    1. Advertising

  3. Ronny wrote:
    > My perl is 5.8.3, and when I execute the following program:
    >
    > #!/usr/local/bin/perl -ws
    > use strict;
    > "dummy" =~ /
    > first|
    > second| # this comment has a/slash
    > third
    > /x;
    >
    > I get the error message:
    >
    > Bareword found where operator expected at ./matcherr.pl line 5, near
    > "second| # this comment has a/slash"
    > (Might be a runaway multi-line // string starting on line 3)
    >
    >
    > The reason is the '/' in the comment. If I change the line to:
    >
    > second| # this comment has no slash
    >
    > the program runs fine. This surprises me, because as I have understood
    > the
    > 'x' modifier, a comment within the pattern should eat up everything
    > until the
    > end of the line. Seemingly, parsing for the terminating "/" happens
    > BEFORE
    > white space inside the pattern is removed. Or did I misunderstand
    > something
    > here?
    >
    > Ronald


    It's a parsing precedence problem. The regex parser is looking for a /
    to terminate the pattern - before it has parsed the /x. So it does not
    know until it has parsed the whole regex how to treat to how to treat
    the #... comment syntax. It does that in the next pass.
    Nick of course, Jun 7, 2006
    #3
  4. Ronny

    Ben Morrow Guest

    Quoth "Nick of course" <>:
    >
    > Ronny wrote:
    > > My perl is 5.8.3, and when I execute the following program:
    > >
    > > #!/usr/local/bin/perl -ws
    > > use strict;
    > > "dummy" =~ /
    > > first|
    > > second| # this comment has a/slash
    > > third
    > > /x;
    > >
    > > I get the error message:
    > >
    > > Bareword found where operator expected at ./matcherr.pl line 5, near
    > > "second| # this comment has a/slash"
    > > (Might be a runaway multi-line // string starting on line 3)
    > >
    > >
    > > The reason is the '/' in the comment. If I change the line to:

    <snip>
    > It's a parsing precedence problem. The regex parser is looking for a /
    > to terminate the pattern - before it has parsed the /x. So it does not
    > know until it has parsed the whole regex how to treat to how to treat
    > the #... comment syntax. It does that in the next pass.


    ....which is why perl6 puts the modifiers first.

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    Ben Morrow, Jun 7, 2006
    #4
  5. Ronny

    Mumia W. Guest

    Mirco Wahab wrote:
    > [...]
    > You have to make sure to give
    > some impression to the parser
    > in what you want ;-)
    >
    > "dummy" =~ {
    > first|
    > second| # this comment has a/slash
    > third
    > }x;
    >
    > Regards
    >
    > Mirco


    So you can use an anonymous hash to match an RE?

    :)
    Mumia W., Jun 7, 2006
    #5
  6. Ronny

    Mirco Wahab Guest

    Thus spoke Mumia W. (on 2006-06-07 17:10):

    > Mirco Wahab wrote:
    >> "dummy" =~ {
    >> first|
    >> second| # this comment has a/slash
    >> third
    >> }x;

    >
    > So you can use an anonymous hash to match an RE?


    Thanks for pointing out this stupid mistake
    (didn't sleep very long last night ;-)

    Of course, this must be a quoted regex
    then, either:

    my $rg = qr{
    first|
    second| # this comment has a/slash
    third
    }x;
    "dummy" =~ $rg and print 1;


    or:

    "dummy" =~ qr{
    first|
    second| # this comment has a/slash
    third
    }x and print 1;



    Thanks and sorry (what did I mesh up this time ;-)

    Mirco
    Mirco Wahab, Jun 7, 2006
    #6
  7. Ronny

    Ben Morrow Guest

    Quoth Mirco Wahab <>:
    > Thus spoke Mumia W. (on 2006-06-07 17:10):
    > > Mirco Wahab wrote:
    > >> "dummy" =~ {
    > >> first|
    > >> second| # this comment has a/slash
    > >> third
    > >> }x;

    > >
    > > So you can use an anonymous hash to match an RE?

    >
    > "dummy" =~ qr{
    > first|
    > second| # this comment has a/slash
    > third
    > }x and print 1;


    You don't need to use qr//, you can just use m//:

    "dummy" =~ m{...}x and print 1;

    Ben

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Jun 7, 2006
    #7
  8. Ronny

    Ronny Guest

    Ben Morrow schrieb:

    > Quoth Mirco Wahab <>:
    > > "dummy" =~ qr{
    > > first|
    > > second| # this comment has a/slash
    > > third
    > > }x and print 1;

    >
    > You don't need to use qr//, you can just use m//:
    >
    > "dummy" =~ m{...}x and print 1;


    But in any case, though this would make a slash possible in
    the comment, it would make a single closing brace in a
    comment spoil the whole code:

    "dummy" =~ m{
    first|
    second| # this comment has a brace}
    third
    }x and print 1;

    I understand the parsing problem. The crucial point to observe here is
    that
    within m//x, comments can not be as arbitrary as they would be outside
    a regexp. Thank you for enlighting me.

    Ronald
    Ronny, Jun 9, 2006
    #8
    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. qazmlp
    Replies:
    5
    Views:
    678
    Michael Dunn
    Apr 7, 2004
  2. Carlos Ribeiro
    Replies:
    10
    Views:
    498
    Peter Hansen
    Sep 15, 2004
  3. Jatinder Singh
    Replies:
    4
    Views:
    426
    Steve Holden
    Jun 1, 2005
  4. robic0
    Replies:
    7
    Views:
    100
    Samwyse
    Jan 24, 2006
  5. Dan Wilkin
    Replies:
    1
    Views:
    247
    robic0
    Jul 17, 2006
Loading...

Share This Page