Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Last active September 30, 2016 11:48
Show Gist options
  • Save jhthorsen/6a00f9cb50d4e21c5dcd58d94925774b to your computer and use it in GitHub Desktop.
Save jhthorsen/6a00f9cb50d4e21c5dcd58d94925774b to your computer and use it in GitHub Desktop.
This is stupid, but I made it anyway
package void;
use strict;
use Exporter 'import';
use Carp;
our @EXPORT = 'void';
sub void {
return unless defined wantarray;
my $caller = (caller 1)[3];
Carp::croak("$caller() is designed for void context");
}
return 1 if defined wantarray;
# perl ./void.pm
require Test::More;
my $manipulate = sub {
$_[0]->{foo} = 123;
return void;
};
eval { $manipulate->({}) };
Test::More::ok(!$@, 'void context') or Test::More::diag($@);
my $x = eval { $manipulate->({}) };
Test::More::like($@, qr{void::__ANON__\(\) is designed for void}, 'not void context');
Test::More::done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment