Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6799127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:48:41+00:00 2026-05-26T18:48:41+00:00

Is there a way I can dynamically include a package based on whatever delegate

  • 0

Is there a way I can dynamically include a package based on whatever delegate is used rather than having to include all the various delegates?

I found this example on how to use delegates but it glosses over the details I’m trying to understand. The way this is written it’s essentially all one file…

package Compare;
use Moose::Role;
requires 'compare';


package SpaceshipCompare;
use Moose;
with 'Compare';  

sub compare { my ($a, $b) = @_; return $a <=> $b }

package Sort;
use Moose;

has 'comparer' => (
    is       => 'ro',
    does     => 'Compare',
    handles  => 'Compare',
    required => 1,
);

sub my_sort {
    my ($self, @list) = @_;
    return sort { $self->compare($a, $b) } @list;
}

Usage:

my $sorter = Sort->new( comparer => SpaceshipCompare->new );
my @sorted = $sorter->my_sort("1one", "0", "43");

In my implementation of a delegate I’m using a different resource based on a parameter that’s passed to the constructor.

  sub BUILD{
    my($this,$args) = @_;

        if($args->{cachedDataSource} eq 'local'){

            $this->setDataStore( Cache::LocalCache->new() ); 

        }

        if($args->{cachedDataSource} eq 'remote'){

            $this->setDataStore( Cache::RemoteCache->new() ); 

        }


        if($args->{cachedDataSource} eq 'memd'){

            $this->setDataStore( Cache::MemedCache->new() ); 

        }

}

But in order for this to work I have to

use Cache::LocalCache;
use Cache::RemoteCache;
use Cache::MemedCache;

Is there a better way to do delegates without perhaps having to use all the packages (like some kind of lazy load)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:48:42+00:00Added an answer on May 26, 2026 at 6:48 pm

    In your example, you can simply use require:

    sub BUILD{
        my($this,$args) = @_;
    
            if($args->{cachedDataSource} eq 'local'){
                require Cache::LocalCache;
                $this->setDataStore( Cache::LocalCache->new() ); 
            }
    
            if($args->{cachedDataSource} eq 'remote'){
                require Cache::RemoteCache;
                $this->setDataStore( Cache::RemoteCache->new() ); 
            }
    
            if($args->{cachedDataSource} eq 'memd'){
                require Cache::MemedCache;
                $this->setDataStore( Cache::MemedCache->new() ); 
            }
    }
    

    Since require is a run-time operation, the class won’t be loaded until it’s actually needed. If your users were passing in class names, then it gets a bit more complicated. You might want to use Module::Load for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to dynamically discover all the XAMLs files within all
Is there a way to dynamically include the value of a session or class
Is there any way can declare a bean in just like JSP UseBean in
is there way thats i can preselect an item when the page loads or
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
Is there a way I can control columns from code. I had a drop
Is there a way I can access (for printout) a list of sub +
Is there a way you can run your UnitTest project against a compiled DLL
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
Is there any way I can detect when my page has been set as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.