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');
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: Can I share instance through Moose::Role ? by tobyink
in thread Can I share instance through Moose::Role ? by dd1942

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.