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 330725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:42:29+00:00 2026-05-12T09:42:29+00:00

Is there a simpler or better (=>easier to maintain) way to use Perl and

  • 0

Is there a simpler or better (=>easier to maintain) way to use Perl and Moose to instantiate classes based on incoming data?

The following code is a stripped down sample from a project I’m working on.

package FooBar;
use Moose;
has 'SUBCLASS' =>('isa'=>'Str',required=>'1',is=>'ro');
has 'MSG' =>('isa'=>'Str',required=>'1',is=>'ro');

sub BUILD {
      my $self = shift;
      my ($a)=@_;
      bless($self,$a->{SUBCLASS})
}
sub Hi {
   my $self=shift;
   print "Hi, I'm a " . ref($self)  ." and I say [". $self->MSG()."]\n";
}

package Foo;
use Moose;
extends ("FooBar");

package Bar;
use Moose;
extends ("FooBar");

package main;
use strict;
use warnings;

for my $line (<DATA>) {
   my ($case,$msg)=split(/[\n\r,]\s*/,$line);
   FooBar->new(SUBCLASS=>$case,MSG=>$msg)->Hi();
}

__DATA__
Foo, First Case
Bar, Second Case

EDIT: It just struck me that this is pretty much what happens when you call the DBI. Depending on the parameters you pass, it will use entirely different code while maintaining a (mostly) consistent interface

  • 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-12T09:42:29+00:00Added an answer on May 12, 2026 at 9:42 am

    Ick. Stevan has a very compelling argument that new should always only
    return an instance of Class. Anything else is confusing to new people learning
    the system.

    You might wanna take a look at
    MooseX::AbstractFactory.
    If that won’t work for you then:

    package FooBar;
    use Moose;
    
    has [qw(SUBCLASS MSG)] => ( is => 'ro', required => 1);
    
    sub create_instance {
        return $self->package->new(message => $self->msg);
    }
    
    package FooBar::Object;
    use Moose;
    
    has msg => ( is => 'ro', required => 1);
    
    sub Hi {
       my $self = shift;
       print "Hi, I'm a " . ref($self)  ." and I say [". $self->MSG()."]\n";
    }
    
    package Foo;
    use Moose;
    extends qw(FooBar::Object);
    
    package Bar;
    use Moose;
    extends qw(FooBar::Object);
    
    
    package main;
    or my $line (<DATA>) {
       my ($case,$msg)=split(/[\n\r,]\s*/,$line);
       FooBar->new(SUBCLASS=>$case,MSG=>$msg)->create_instance->Hi
    }
    
    __DATA__
    Foo, First Case
    Bar, Second Case
    

    Of course there are many other ways to implement this same concept in Moose. Without knowing the specifics of your domain problem it’s hard to tell that something like MooseX::Traits wouldn’t be better:

    package Foo;
    use Moose;
    with qw(MooseX::Traits);
    
    package Bar;
    use Moose;
    with qw(MooseX::Traits);
    
    package Messaging;
    use Moose::Role;
    
    has msg => ( is => 'ro', required => 1);
    
    sub Hi {
       my $self = shift;
       print "Hi, I'm a " . ref($self)  ." and I say [". $self->MSG()."]\n";
    }
    
    package main;
    use strict;
    Foo->with_traits('Messaging')->new( msg => 'First Case')->Hi;
    

    This is roughly what the other poster meant about using a Role based solution.

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

Sidebar

Related Questions

Is there a simpler way to rewrite the following condition in JavaScript? if ((x
Fairly simple; I'm just curious if there's a better way to accomplish this. Given
Is there a simpler way of implement this? Or a implemented method in JDK
Is there a simpler way to get a string of a parent directory than
Given two lists of equal length, is there a simpler or preferred way to
Is there a simpler list type than DataGrid that can be connected to a
If there is any possibility to make this code simpler, I'd really appreciate it!
I was wondering if there was a more efficient (efficient as in simpler/cleaner code)
I am fairly new to MVC so please don't hesitate to suggest a better/cleaner/simpler
I work for a company that develops a very large scale data based application.

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.