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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:15:40+00:00 2026-05-17T18:15:40+00:00

Possible Duplicate: How to have Moose return a child class instance instead of its

  • 0

Possible Duplicate:
How to have Moose return a child class instance instead of its own class, for polymorphism

Suppose I have two related classes MyClass::A and MyClass::B that are both subclasses of MyClass. I would like the constructor for MyClass to take a filename, read the file, and based on the contents of the file, decide whether the file is of type A or B. Then, it should construct an object of the appropriate subclass and return it. Edit: Actually, it should call the constructor of the appropriate subclass and return the result.

So for example, after this code,

my $filename = "file_of_type_A.txt";
my $object = MyClass->new($filename);

$object should be an instance of MyClass::A. This seems like valid behavior, because $object->isa('MyClass') will always return true. I was thinking of using introspection to get a list of all subclasses of MyClass, and then trying to construct each in turn, until one succeeds. However, I don’t see a way to modify the Moose constructor to do this. Neither the BUILDARGS nor BUILD hooks seem appropriate.

So how can I modify a Moose class’s constructor to select an appropriate subclass and delegate construction to that subclass?

  • 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-17T18:15:41+00:00Added an answer on May 17, 2026 at 6:15 pm

    As Ether said above, you should have a factory method do this task. I like to put the factory method in a separate package/class from the things it produces Usually this is a virtual class that has no attributes or constructor, but if you want to do things like keep track of what you have made, you can create an actual factory object.

    package MyFile::Factory;
    
    sub make_file_object {
        my $class = shift;
        my $filepath = shift;
    
        my $type = $class->determine_file_type( $filepath );
        my $file_class = $class->get_class_for_file_type( $type );
    
       return $file_class->new( $filepath, @_ );
    }
    
    # implement those other methods down here.
    
    1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: If I have two tables in SQL with a many-many relationship, do
Possible Duplicate: Understanding Threads I have a public class that is implementing a Runnable
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: Why does gcc have “â” in all its error messages? New to
Possible Duplicate: Custom Collection Initializers I have a simple Pair class: public class Pair<T1,
Possible Duplicate: Why cant we have static method in an inner class? Hi all,
Possible Duplicate: How can I have references between two classes in Objective-C? Objective-C #import
Possible Duplicate: Should a function have only one return statement? This is what I

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.