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

The Archive Base Latest Questions

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

Since Perl/Moose always calls the base class’ BUILD function before it calls the subclass

  • 0

Since Perl/Moose always calls the base class’ BUILD function before it calls the subclass BUILD function, there is a new instance of the base class everytime you instantiate a subclass.

How do I go about creating a static variable that can be used by all the subclasses, or alternatively how can I create a static base or abstract class? (does that approach even make sense?)

I’m trying to create a variable that dynamically enables or disables certain features of a function defined at run-time in the base class but accessible from the sub classes.

So if I do something like

my obj = My::childObject_1->new( 'use_my_var' => 1 );

it will also be true for

my obj2 = My::childObject_2->new();
my obj3 = My::childObject_3->new();

without having to specifically define that variable. Unless

my obj4 = My::childObject_2->new( use_my_var' => 0 ); 

in which case it would from that point be false for all subclasses because they all

extends My::BaseObject

Additionally, is there a design pattern that describes this behavior?

(Note: I’m on a shared system so I can’t install MooseX — or at least I haven’t been able to figure out how to setup local PERL5LIB installs of modules in my user directory =/ so Moose-only solution helps for now!)

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

    UPDATE

    Now there is a much better way to do this, use MooseX::ClassAttribute

    Then just use class_has rather than has for the methods you want shared with all instances.

    package My::Class;
    
    use Moose;
    use MooseX::ClassAttribute;
    
    class_has 'Cache' =>
        ( is      => 'rw',
          isa     => 'HashRef',
          default => sub { {} },
        );
    
    __PACKAGE__->meta()->make_immutable();
    

    OLD

    Additionally, is there a design pattern that describes this behavior?

    Yes. It’s called a Singleton. A Singleton is a pattern whereby multiple initiations (calls to ->new) will return the same object. You can either do it like this, or store the variable outside of a class. Moose provides a layer that will permit you to create Singletons easily (thought it isn’t particularly hard either way): the module MooseX::Singleton. Moose also permits you to delegate to another object by using an accessor.

    Here we use MooseX::Singleton, and delgation to a hidden attribute to achive the desired effect.

    package MySingleton;
    use MooseX::Singleton;
    
    has 'foo' => ( is => 'rw', isa => 'Bool', default => 0 );
    
    package ClassA;
    use Moose;
    
    has '_my_singleton' => (
      isa => 'MySingleton'
      , is => 'ro'
      , default => sub { MySingleton->new }
      , handles => [qw( foo )]
    );
    
    
    package ClassB;
    use Moose;
    
    has '_my_singleton' => (
      isa => 'MySingleton'
      , is => 'ro'
      , default => sub { MySingleton->new }
      , handles => [qw( foo )]
    );
    
    package main;
    use Test::More tests => 5;
    
    my $class_a = ClassA->new;
    my $class_b = ClassA->new;
    
    is( $class_a->foo(0), 0, 'Set A to false' );
    is( $class_a->foo, 0, 'A Is false' );
    is( $class_b->foo, 0, 'B Is false' );
    is( $class_b->foo(1), 1, 'Set B to true' );
    is( $class_a->foo, 1, 'A is true' );
    

    Or, without MooseX

    Please don’t do this unless required. The MooseX method is much nicer:

    package Underclass;
    use Moose;
    
    has 'foo' => ( is => 'rw', isa => 'Bool', default => 0 );
    
    package SingletonWrapper;
    my $obj;
    sub new {
        if ( $obj ) { return $obj; }
        else { $obj = Underclass->new }
    }
    
    package ClassA;
    use Moose;
    
    has '_my_singleton' => (
        isa => 'Underclass'
        , is => 'ro'
        , default => sub { SingletonWrapper->new }
        , handles => [qw( foo )]
    );
    
    
    package ClassB;
    use Moose;
    
    has '_my_singleton' => (
        isa => 'Underclass'
        , is => 'ro'
        , default => sub { SingletonWrapper->new }
        , handles => [qw( foo )]
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm guessing it's not a Perl Compatible Regular Expression, since there's a special kind
I'm fairly new to perl and I'm using a perl DOM parser and there
I have an object that stores arrays as instance variables. Since Perl does not
I am still new to Perl. Since BEGIN blocks are run during compilation can't
Since arrays and hashes can only contain scalars in Perl, why do you have
Problem... Since MacPerl is no longer supported on 64bit perl , I am trying
Since CS3 doesn't have a web service component, as previous versions had, is there
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote
Since the keyboard is the interface we use to the computer, I've always thought
This seems to have most started since I upgrade my DBIx::Class and I can't

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.