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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:05:25+00:00 2026-05-14T20:05:25+00:00

This may turn out to be an embarrassingly stupid question, but better than potentially

  • 0

This may turn out to be an embarrassingly stupid question, but better than potentially creating embarrassingly stupid code. 🙂 This is an OO design question, really.

Let’s say I have an object class ‘Foos’ that represents a set of dynamic configuration elements, which are obtained by querying a command on disk, ‘mycrazyfoos -getconfig’. Let’s say that there are two categories of behavior that I want ‘Foos’ objects to have:

  • Existing ones: one is, query ones that exist in the command output I just mentioned (/usr/bin/mycrazyfoos -getconfig`. Make modifications to existing ones via shelling out commands.

  • Create new ones that don’t exist; new ‘crazyfoos’, using a complex set of /usr/bin/mycrazyfoos commands and parameters. Here I’m not really just querying, but actually running a bunch of system() commands. Affecting changes.

Here’s my class structure:

Foos.pm

package Foos, which has a new($hashref->{name => ‘myfooname’,) constructor that takes a ‘crazyfoo NAME’ and then queries the existence of that NAME to see if it already exists (by shelling out and running the mycrazyfoos command above). If that crazyfoo already exists, return a Foos::Existing object. Any changes to this object requires shelling out, running commands and getting confirmation that everything ran okay.

If this is the way to go, then the new() constructor needs to have a test to see which subclass constructor to use (if that even makes sense in this context). Here are the subclasses:

Foos/Existing.pm

As mentioned above, this is for when a Foos object already exists.

Foos/Pending.pm

This is an object that will be created if, in the above, the ‘crazyfoo NAME’ doesn’t actually exist. In this case, the new() constructor above will be checked for additional parameters, and it will go ahead and, when called using ->create() shell out using system() and create a new object… possibly returning an ‘Existing’ one…

OR

As I type this out, I am realizing it is perhaps it’s better to have a single:

(an alternative arrangement)

Foos class, that has a

->new() that takes just a name

->create() that takes additional creation parameters

->delete(), ->change() and other params that affect ones that exist; that will have to just be checked dynamically.

So here we are, two main directions to go with this. I’m curious which would be the more intelligent way to go.

  • 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-14T20:05:25+00:00Added an answer on May 14, 2026 at 8:05 pm

    In general it’s a mistake (design-wise, not syntax-wise) for the new method to return anything but a new object. If you want to sometimes return an existing object, call that method something else, e.g. new_from_cache().

    I also find it odd that you’re splitting up this functionality (constructing a new object, and returning an existing one) not just into separate namespaces, but also different objects. So in general, you’re closer with your second approach, but you can still have the main constructor (new) handle a variety of arguments:

    package Foos;
    use strict;
    use warnings;
    
    sub new
    {
        my ($class, %args) = @_;
    
        if ($args{name})
        {
            # handle the name => value option
        }
    
        if ($args{some_other_option})
        {
            # ...
        }
    
        my $this = {
            # fill in any fields you need...
        };
    
        return bless $this, $class;
    }
    
    sub new_from_cache
    {
        my ($class, %args) = @_;
    
        # check if the object already exists...
    
        # if not, create a new object
        return $class->new(%args);
    }
    

    Note: I don’t want to complicate things while you’re still learning, but you may also want to look at Moose, which takes care of a lot of the gory details of construction for you, and the definition of attributes and their accessors.

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

Sidebar

Related Questions

This may seem like a stupid question, but what message do i send to
This may sound like a stupid question, perhaps it is. But I'm only trying
This may turn out to be a simple question with a very complex answer,
I am sorry ahead of time for how this question may turn out. I
The answer to this question may turn out to be, Don't use typed DataSets
This may seem like a simple question but i am getting an error when
This may seem really silly to you, I admit, but when discussing the Model-View-ViewModel
This may be a stupid question, as I'm not sure how MSBuild works with
This may be a long shot but I'm out of ideas. I've got a
This question may seem to be naive. But I think it'll be much worse

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.