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

R

Ronny

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
 
M

Mirco Wahab

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
 
N

Nick of course

Ronny said:
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.
 
B

Ben Morrow

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
 
M

Mumia W.

Mirco said:
[...]
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?

:)
 
M

Mirco Wahab

Thus spoke Mumia W. (on 2006-06-07 17:10):
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
 
B

Ben Morrow

Quoth Mirco Wahab said:
Thus spoke Mumia W. (on 2006-06-07 17:10):

"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
 
R

Ronny

Ben said:
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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top