http://qs1969.pair.com?node_id=11137081


in reply to Re^4: loop iterator in a string
in thread loop iterator in a string

I think rejecting any $str containing anything which isn't a cipher, comma or dot prior to eval $str should be good enough

Not quite, since it would also accept ",....," and other things like that (one of the things I meant with "other subtle bugs"). Personally I would suggest both a regex like /\A(\d+(?:\.\.\d+)?)(?:,(?1))*\z/ and checking eval for errors. And at that level, the complexity is high enough that something like* tybalt89's solution is probably better.

* Update: By which I mean, something like tybalt89's solution with some added error checking ;-)

Replies are listed 'Best First'.
Re^6: loop iterator in a string
by LanX (Saint) on Sep 28, 2021 at 10:53 UTC
    > both a regex like /\A(\d+(?:\.\.\d+)?)(?:,(?1))*\z/

    That's overkill in that case,...

    > and checking eval for errors.

    ... checking eval for errors is enough, once the code fails.

    Your all-covered approach reminds me of parents insisting to drive their kids to school while they keep their bike's helmet on.

    Like this you are scaring them away from learning how to bike by themselves.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      checking eval for errors is enough, once the code fails.

      ... but then you don't have protection from unsafe code, which I thought was the point of your post. I generally suggest checking eval for errors because it's a good habit to get into, though with the very restrictive regex I suggested I might be open to the argument that this case might be an exception to that rule.

      Your all-covered approach reminds me of parents insisting to drive their kids to school while they keep their bike's helmet on. Like this you are scaring them away from learning how to bike by themselves.

      Defensive programming is a very different thing from your analogy, which I would say is too extreme and too much rhetoric. Showing your kids how to properly put on their safety gear is not a bad thing.

        > ... but then you don't have protection from unsafe code,

        plus my regex of course.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery