in reply to methods for dealing with zero '0' as a string or char

> am I an idiot or is this a common issue that I should be handling differently?

No you are not an idiot, it's just that different people have different understandings about a true scalar.

This means you need to be explicit what you exactly want to test.

Background: Perl is designed such that scalars are transparent to their C type, i.e. no matter if the 0 is internally an integer, float or string. °

Hence the inner representation of a scalar can change without effecting the boolean context.

NB: If you really need an explicit string type which is only false when empty, you are free to create your own "string" object and overload it's bool behaviour.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

°) in other languages with explicit types you are forced to transform a number inside a string into an int or float before using numeric operations.

For instance in Python

Replies are listed 'Best First'.
Re^2: methods for dealing with zero '0' as a string or char
by kcott (Archbishop) on Aug 16, 2019 at 07:40 UTC

    Just as an adjunct to what you have there, length returns FALSE for a zero-length EXPR and also an undefined EXPR. The latter behaviour was introduced in 5.12.0 (perl5120delta: Other potentially incompatible changes).

    Interestingly, given the general discussion here, the FALSE value returned for a zero-length EXPR is 0, while the FALSE value returned for an undefined EXPR is undef.

    — Ken

      for clarification

      > Interestingly, given the general discussion here, the FALSE value returned for a zero-length EXPR is 0, while the FALSE value returned for an undefined EXPR is undef.

      Some operators in Perl return an internal magic boolean value, which is !!0 for FALSE and !!1 for TRUE.

      length is not one of them, it returns values which are interpreted as FALSE or TRUE, returning undef for undef is very consistent in my eyes.

      Though one probably could argue that warnings uninitialized should trow a warning here, I'm a bit confused in this matter.

      I remember that tobyink started a discussion once that warnings uninitialized leads to inconsistent behaviour.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice