in reply to matching digits and characters

Wow! That's the first triple negative I've seen in the wild in a long time:)

You code reads:

Print "non-digits found" if $str doesn't match things that are not non-digits!

Try: print '"non digits found" if $str contains non-digits. (It's easier for a human being to read:)

print 'non digits found' if $str =~ /\D/;

Note: Using /g in the scalar context of an if statement is probably superfluous.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re^2: matching digits and characters (scalar //g)
by tye (Sage) on Aug 22, 2003 at 16:46 UTC
    Using /g in the scalar context of an if statement is probably superfluous.

    s/superfluous/a mistake/. //g in a scalar context can make your match fail every so often. I've seen this type of bug reported here several times.

    This particular code, in isolation, probably wouldn't notice the bug (perhaps that is what you meant) since $str appears to be getting updated at least once per regex application. But the use of //g in a scalar context is something that should be vigorously avoided unless you need that advanced magic and understand it.

    I've seen this mistake enough that I'd support a pragma that makes this illegal unless you ask for it. But, in practice, I don't see how to make such useful.

                    - tye

      Yes, I probably should have stated it more strongly.

      I have been bitten by this a few times which is why it stood out at me. I didn't have enough confidence to say the it was always, or even nearly always a mistake without thinking it through further. I know that I have used it to good effect at least once, hence the prevarication.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.