This part seems wrong because you're bypassing Moose's constructor. (And constructors are one of the key selling points for using Moose.)
sub TIEHASH { my $class = shift; my $self = bless {},$class; while(my($key,$value) = each(%ENV)) { $self->set_node($key => $value); } return $self; }
Instead, I'd write that as:
sub TIEHASH { my $class = shift; my $self = $class->new; while(my($key,$value) = each(%ENV)) { $self->set_node($key => $value); } return $self; }
So TIEHASH just becomes a wrapper for the Moose constructor.
As well as constructors, Moose also provides destructors, so you shouldn't be overriding the DESTROY method that Moose provides for you. Luckily, you've misspelt it as DESTORY, so no harm done.
To answer your question, this is how package A should be written:
package A; use Moose; has BBB => ( is => 'rw', lazy => 1, builder => '_build_BBB', handles => [qw( get_node )], ); sub _build_BBB { my $self = shift; my $instance = tie(%ENV, 'BBB'); return $instance; }
And this is how you'd use it:
my $a = A->new; print $a->get_node('TERM'), "\n";
Note that I've ditched your bind_vars method in favour of using a lazy builder for the BBB attribute. When the value of BBB is needed, Moose will automatically run the _build_BBB method to populate it. This means that code outside the A class doesn't need to remember to manually call the bind_vars method in order to bring the object into a usable state.
That said, I'm not sure why you're mucking around with trying to tie %ENV anyway. Nothing you've shown demonstrates a need for tying. This seems far simpler:
use v5.14; package MyRole { use Moose::Role; has nodes => ( traits => ['Hash'], is => 'rw', isa => 'HashRef[Item]', handles => { set_node => 'set', get_node => 'get', all_nodes => 'keys', } ); } package BBB { use Moose; with 'MyRole'; } package A { use Moose; has BBB => ( is => 'rw', lazy => 1, default => sub { BBB->new(nodes => \%ENV) }, handles => [qw( get_node )], ); } my $a = A->new; say $a->get_node('TERM');
In reply to Re: Can I share instance through Moose::Role ?
by tobyink
in thread Can I share instance through Moose::Role ?
by dd1942
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |