Hello Experts, I'm trying to share intance via Moose::Role like below

#### Role #### package MyRole; use Moose::Role; has 'nodes' => ( traits => ['Hash'], is => 'rw', isa => 'HashRef[Item]', handles => { set_node => 'set', get_node => 'get', all_nodes => 'keys', } ); 1; #### B #### package BBB; use Moose; with 'MyRole'; sub TIEHASH { my $class = shift; my $self = bless {},$class; while(my($key,$value) = each(%ENV)) { $self->set_node($key => $value); } return $self; } sub FETCH { my $self = shift; my $param = shift; return $self->get_node($param); return undef; } sub STORE { my $self = shift; my $key = shift; my $value= shift; return unless defined($key); return $self->set_node($key => $value); } sub DESTORY { return 1 } 1; no Moose; __PACKAGE__->meta->make_immutable; #### A #### package A; use Moose; use base 'BBB'; with 'MyRole'; sub bind_vars { my $self = shift; my $instance = tie(%ENV,'BBB'); $self->BBB($instance); return 1; } has 'BBB' => ( is => 'rw', ); 1; no Moose; __PACKAGE__->meta->make_immutable; #### main #### package main; use base 'A'; my $obj = A->new; $obj->bind_vars; # This works , but that's not I expected print $obj->BBB->get_node('TERM'),"\n"; # I wish to use it in this way print $obj->get_node('TERM'),"\n"; $ENV{TEST} = 123; print $obj->BBB->get_node('TEST'),"\n"; print $obj->get_node('TEST'),"\n"

As you can see, this is my requirements:

1.I want to directly call A->get_node(), other than A->BBB->get_node();

2.I don't want to import DESTROY/FETCH,etc into A, I wish to use it as a delegation in somehow.

May I ask if there is any features / easier way can help me directly call B.get_node transparently in A?

Your any kind of help is appreciated!


In reply to 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.