...
In list context, things are slightly more complicated. An empty list, when evaluated in a Boolean boolean condition, evaluates to false. But the value undef
, when evaluated in list context, evaluates to true because it is converted to a list with the singleton value undef
. Therefore, a function should not return undef
if it might ever be invoked in list context.
...
The read_users()
subroutine returns undef
if it cannot open /etc/shadow
, but it returns a list of user data entries if it succeeds. Because its output is used in list context, a return value of undef
is converted to a list of a single element: (undef)
. Consequently, the if
class condition returns true, and the system incorrectly prints out:
...