The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


Object::Generic::False - a perl object that evaluates as false but allows method calls.


  use Object::Generic::False qw(false);
  my $n = false;        # returns global $Object::Generic::False::_false_
  print "n is false" if not $n;
  print "n->foo->bar is also false" if not $n->foo->bar;

  my $result = Object::Generic::False->new('Some error message.');
  print The error is '" . $result->error . "'\n" if not $result;


Lately I've been doing some object oriented perl in which I would like to have $object->foo return false without having $object->foo->bar generate an error. Hence this module.

To generate a false object, either use the exported false() method, which returns the package global $Object::Generic::False::_false_, or create your own instance with Object::Generic::False->new;

Object::Generic::False objects continue to be false when combined with other perl entities. Thus for example unlike numeric 0, (2 + false) is still false. In that respect they act somewhat "tainted" variables.

The exceptions to this rule are comparisons and string concatenation: (false == 0) and (false eq '') are both true, while false()."foo" is "foo".

These objects can also be used as a returned error with an enclosed message. To do so, just pass in a string when creating one with the new message, ie "$result = new Object::Generic::False 'your message'". $result still evaluates as false, but the message may be retrieved with $result->error.


The false() function is exported, which returns the global $Object::Generic::False::_false_ object.


Jim Mahoney, <<gt>


Copyright (C) 2005 by Jim Mahoney

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.