Increment (++) and decrement (--) strings in Perl

前端 未结 3 1453
执念已碎
执念已碎 2020-12-18 17:45

With perl -e \'$string=\"a\";print ++$string;\' we get b,
but with perl -e \'$string=\"b\";print --$string;\' we get -1

3条回答
  •  执念已碎
    2020-12-18 18:30

    There are at least three reasons:

    1. because there isn't any great need for it
    2. the magic of auto-incrementing has been seen to be faulty, and there is no reason implement auto-decrementing in the same faulty way
    3. the magic of auto-incrementing cannot be fixed because p5p doesn't like to break backwards compatibility

    Raku (née Perl 6) on the other hand does not suffer from a need for backwards compatibility, and therefore has better behavior for auto-incrementing strings and has auto-decrementing as well. The ++ and -- operators work by calling the succ and pred methods on the object they are operating on.

提交回复
热议问题