Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

codem/(?:-x pattern) #comment /x bug?/code

by dcorbin (Sexton)
on Sep 07, 2000 at 23:47 UTC ( [id://31459]=perlquestion: print w/replies, xml ) Need Help??

dcorbin has asked for the wisdom of the Perl Monks concerning the following question:

I'm at a loss to explain this behavior. Is this correct? Or is it a bug. This is version 5.005_03 built for i386-linux.
#!/usr/bin/perl my $html = qq( abcdef ); sub check { my $pattern = shift; my @br; eval "\@br = ' abcdef ' =~ m/$pattern/x"; die "$@\n" if $@; print "PATTERN=m/$pattern/x\n"; print "ANSWER = "; print join("-",@br),"\n"; } check('(?-x:(abcdef))'); check('(?-x:(abcdef)) # comment here');
executing this produces
PATTERN=m/(?-x:(abcdef))/x
ANSWER = abcdef
PATTERN=m/(?-x:(abcdef)) # comment here/x
ANSWER = 
I think both check's should produce the same results. Am I right or wrong?

Replies are listed 'Best First'.
RE: codem/(?:-x pattern) #comment /x bug?/code
by Shendal (Hermit) on Sep 08, 2000 at 00:08 UTC
    Tried your code with perl 5.6 (activestate build), and it works like a champ.

    Output: PATTERN=m/(?-x:(abcdef))/x ANSWER = abcdef PATTERN=m/(?-x:(abcdef)) # comment here/x ANSWER = abcdef

    Cheers,
    Shendal
      I tried this with 5.00551 on Solaris 2.6 (egcs 2.91.57) and it failed on the 2nd pattern like the original poster. At first it seemed like the (?-x:...) bit was negating the x flag for the entire regexp, but I tried adding the comment to the pattern and it still failed to match, so I'm not sure. Very odd.

      Like another poster, I was thinking that the end of the regexp being on the same line as the comment was causing the /x to be ignored (interpreted as part of the comment), but adding whitespace in there caused the pattern to fail in both cases (odd). Something isn't right.

      Sounds like a bug that's been fixed then.
(Ovid) Re: codem/(?:-x pattern) #comment /x bug?/code
by Ovid (Cardinal) on Sep 08, 2000 at 00:07 UTC
    First, your join statement on @br is useless as @br only contains one element.

    Second, when you have a regex using a (?:...) modifier, anything between the question mark and colon acts as a local switch unless it's after a -, in which case, it serves to negate a switch that's been applied to the entire regex. In this case, the /x switch (or modifier, if you prefer) at the end of the regex has the regex ignore unescaped whitespace, which you don't have in your first regex. As a result, your /x switch serves no purpose in this regex, and would be negated by the ?-x: construct, in any event.

    The reason why the second regex does not match is because it's trying to match "#commenthere" (note the lack of spaces). Since they don't appear in your target text, you get no match.

    Hope this helps.

    Cheers,
    Ovid

    Update: I'm a moron. I need to think about this one a bit more. It's a lot trickier than I first thought. Now that I have a better idea of what you are doing, this does look like a bug. Changing ?-x: to ?: makes it work, but (?-x:...) is supposed to be local.

    P.S. I am using 5.005 (ActiveState on WinNT)

      1) I know. It's code that's been simplified to show the problem.

      2) That's what I thought. I know that /x is not relevant in the first case, but I was trying to show two very consistent cases, one of which works and one doesn't when I think they both should.

      3) But the regex is applied with the x modifier at the expression level. Doesn't that mean that the comment should be treated as a comment?

Re: codem/(?:-x pattern) #comment /x bug?/code
by Hot Pastrami (Monk) on Sep 08, 2000 at 00:04 UTC
    I believe you need the /x modifier for #2 to work... or use ?# for the comment

    Update: Oops, you've got /x there already... sorry, didn't notice. Perhaps it's because the regexp ends on a line after the # for the comment? That may be crazy talk... I don't know a whole lot about these regular expressions yet.

    Alan "Hot Pastrami" Bellows
    -Sitting calmly with scissors-
      I believe I HAVE the x modifier, applied to the entire expression.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://31459]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-23 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found