in reply to Re: Weird behavior of int()
in thread Weird behavior of int()

I echo this confusion ... in particular, what confuses me the most is: what is the point of the +0 ?

In what situation do you (intentionally) want/get a different result from int($str+0) vs int($str) ???

As an aside, your regular expressions would be better written as ...

I don't believe the OP is saying they intend to replace the int(...) with a regex, I believe they are assuming int(...) is implemented via a regex?

The key reason why int("information") returns Inf is because of the intended usage of the function, per the docs...

int EXPR int Returns the integer portion of EXPR. If EXPR is omitted, u +ses $_. ...

...the key word being "portion". The If you give it a scalar, it's going to consume as much of that scalar as it can to produce an integer:

$ perl -le 'print int("32hike")' 32

Replies are listed 'Best First'.
Re^3: Weird behavior of int()
by Marshall (Canon) on May 22, 2024 at 03:52 UTC
    what is the point of the +0 ?

    That makes no sense for this usage because int() will do all that is needed. The +0 trick can be used to eliminate leading zeroes, i.e. 006 -> 6. I sometimes translate between formats with and without leading zeroes.