in reply to What could make "()" a good value for boolean false?

return (); is the same as return;, which returns an empty list when the sub is called in list context and a false value when the sub is called in scalar context. On the other hand, return undef; always returns one value from the sub: undef. When that sub is called in list context, it returns a list consisting of one element (undef). Because that list has one element, when you evaluate it in boolean context, it's true! See also Perl::Critic::Policy::Subroutines::ProhibitExplicitReturnUndef.

Replies are listed 'Best First'.
Re^2: What could make "()" a good value for boolean false?
by morgon (Priest) on Mar 27, 2016 at 21:06 UTC
    That makes sense - thanks.

    So is return (); as opposed to simply return; (the two behave the same) a widely accepted convention or just a matter of personal taste?

      So is return (); as opposed to simply return; (the two behave the same) a widely accepted convention or just a matter of personal taste?

      its not a convention, there are no conventions ... its extra typed chars that serve no purpose , don't see why anyone would adopt that as a style or a convention ...

      > (the two behave the same)

      no, they do the same!

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      I hope not, because I'm opposed to it. Sub that are expected to return a scalar shouldn't suddenly return nothing. It causes subtle problems that aren't caught by the compiler.

      Consider what happens if type suddenly returned nothing instead of `undef`:

      my $h = { type => type(), name => name(), };

      return (); should be used for subs that are expected to return a list.

      return undef; should be used for subs that are expected to return a scalar. There are exceptions, of course.