Hello Respected Monks I am seeking some help question.
package Validation; use Moose::Role; sub check_entity { my ( $self, $db_id ) = @_; #some logic my $found_db = sub { #logic verify id present in db return 1; } return $found_db; }

I have a module that helps me write clean modules using the following `package MyApp::Moose;`. I tried searching a lot and am not sure exactly how I can inject the above role to the caller (so that it will get consumed) and the caller can have access to `check_entity` method.

I refered

Note:- I cant create an object of the caller since it has some required entity (*object may not be needed to inject role I believe)

But unfortunately, I couldn't able to figure out the right way, I am sure there must be a simple way to do it which I am missing out. Also want to do similar for multiple roles in the future once I develop them.

package MyApp::Moose; use strict; use warnings; use namespace::autoclean; use Hook::AfterRuntime; use Import::Into; use Moose (); use Clone 'clone'; sub import { my ($class, @opts) = @_; my $caller = caller; my %opt = map { $_ => 1 } @opts; strict->import::into($caller); warnings->import(); Clone->import::into($caller,'clone'); if($opt{role}) { require Moose::Role; Moose::Role->import({into=>$caller}); } else { Moose->import({into=>$caller}); after_runtime { $caller->meta->make_immutable(); }; } namespace::autoclean->import( -cleanee => $caller, ); return; } 1;

* Currently Using above in code like this.

package MyApp::Process; use MyApp::Moose; sub some_method { my ($self, $db_id) = @_; # I want to call like this $self->check_entity($db_id) || return; } 1;

I really appreciate any help you can provide.

I was hoping I could inject it directly to the caller using metacpan.org/pod/Moose::Exporter(Moose::Exporter) but after several attempts, I was unable to accomplish

The same question was Posted on StackOverflow. https://stackoverflow.com/questions/72678529/how-to-inject-role-to-caller-module/72691410(StackOverFlow)


In reply to How to inject role to Caller Module? by smarthacker67

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.