Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use strict;
- use warnings;
- use utf8::all;
- use Moose;
- use MongoDB;
- use MongoDB::OID;
- use Data::Printer;
- {
- package User;
- use Moose;
- use Moose::Util::TypeConstraints;
- class_type('MongoDB::OID');
- coerce 'MongoDB::OID'
- => from 'Str'
- => via { MongoDB::OID->new($_) };
- has '_id' => (
- is => 'ro',
- isa => 'MongoDB::OID',
- required => 1,
- coerce => 1,
- );
- has 'name' => (
- is => 'rw',
- isa => 'Str',
- required => 1,
- );
- no Moose;
- __PACKAGE__->meta->make_immutable;
- }
- my $user1 = User->new(
- _id => '4de50e865c93120f5d000000',
- name => 'Fred',
- );
- p $user1;
- p $user1->_id;
- ~~~~~~~~~~~~~~
- Output:
- ~~~~~~~~~~~~~~
- sidburn@sid:~/perl/mongo$ ./oid.pl
- User {
- Parents Moose::Object
- Linear @ISA User, Moose::Object
- public methods (4) : DESTROY, meta, name, new
- private methods (1) : _id
- internals: {
- _id MongoDB::OID,
- name "Fred"
- }
- }
- MongoDB::OID {
- Parents Moose::Object
- Linear @ISA MongoDB::OID, Moose::Object
- public methods (10) : ("", BUILDARGS, build_value, DESTROY, get_time, meta, new, TO_JSON, to_string, value
- private methods (1) : _build_value
- internals: {
- value "4de61042acdcdc811f000000"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement