Skip to content

Instantly share code, notes, and snippets.

@gugod
Created September 22, 2023 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugod/633bb6bcbf4b9fb43e01a1fb27719ce0 to your computer and use it in GitHub Desktop.
Save gugod/633bb6bcbf4b9fb43e01a1fb27719ce0 to your computer and use it in GitHub Desktop.
Nay -- an object of negative identity.
package Nay;
use v5.36;
no warnings 'experimental::builtin';
use builtin 'false';
use overload 'bool' => sub { false };
use Exporter 'import';
our @EXPORT = qw(Nay);
my $Nay = bless {}, 'Nay';
sub Nay() { $Nay }
sub AUTOLOAD { $Nay }
1;
=head1 Nay
use Nay;
package Foo {
sub bar { rand() > 0.5 ? $_[0] : undef }
sub buz { "buz" }
};
my $foo = bless {}, 'Foo';
# Append '// Nay' after calling methods that might return undef.
# All following method calls may be reduced to Nay.
my $buz = ($foo->bar() // Nay) -> buz();
if ($buz) {
say "buz() is called";
} else {
say "Nay.";
}
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment