Skip to content

Instantly share code, notes, and snippets.

@dagolden
Created July 14, 2015 16:09
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 dagolden/7bdfa243d61e48decac9 to your computer and use it in GitHub Desktop.
Save dagolden/7bdfa243d61e48decac9 to your computer and use it in GitHub Desktop.
# override instead of 'around' to avoid overhead of calling
# the default BUILDARGS
sub BUILDARGS {
my $class = shift;
if ( @_ == 0 ) {
return { value => MongoDB::BSON::generate_oid() };
}
if ( @_ == 1 ) {
if ( ref( $_[0] ) eq 'HASH' ) {
return $_[0];
}
elsif ( $_[0]->isa($class) ) {
return { value => $_[0]->value };
}
else {
return { value => $_[0] };
}
}
if ( @_ % 2 == 0 ) {
return {@_};
}
die "Arguments to MongoDB::OID->new must be hash or hash reference\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment