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

  • Home
  • SEARCH
  • 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 6776511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:02:18+00:00 2026-05-26T16:02:18+00:00

I have an object which holds a stack of objects. The object represents the

  • 0

I have an object which holds a stack of objects. The object represents the current state, and each object in the stack holds the state at a particular level of nesting.

package State;

use Mouse;
use RealState;

has state_stack => {
    is    => 'rw',
    isa   => 'ArrayRef[RealState]',
    default => sub {
        return [RealState->new]
    }
};

I want State to delegate to State->state_stack->[0]. How can I do that efficiently with Mouse (so no meta hacking). I cannot use Moose, my project cannot have any dependencies (I’m bundling Mouse::Tiny).

“You can’t” is fine, I’ll write an AUTOLOAD.

  • 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-26T16:02:19+00:00Added an answer on May 26, 2026 at 4:02 pm

    You can’t it directly, but there’s a hack better than AUTOLOAD. That is, RealState->meta->get_all_method_names() gives you method names which are defined in RealState.

    #!perl
    use 5.14.0;
    package RealState {
        use Mouse;
    
        sub foo { 'foo' }
        __PACKAGE__->meta->make_immutable;
    }
    package State {
        use Mouse;
    
        has stack => (
            is => 'rw',
            isa => 'ArrayRef',
            default => sub { [ RealState->new ] },
        );
    
        # define delegates for stack->[0]
        my $meta = __PACKAGE__->meta;
        foreach my $name(RealState->meta->get_all_method_names) {
            next if Mouse::Object->can($name); # avoid 'new', 'DESTROY', etc.
    
            # say "delegate $name";
            $meta->add_method($name => sub {
                my $self = shift;
                $self->stack->[0]->$name(@_);
            });
        }
    
        $meta->make_immutable;
    }
    
    my $state = State->new();
    say $state->foo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object Dog which holds a set of DogMetadata . Each DogMetadata
I have an object of type IMAGE which holds image. I wanted to display
I have an object which holds a title and an indexReference. I save the
I have an object which holds many values, and some of them (not all
I have data database containing some rather large strings, each of which holds a
My use case is simple. I have a root domain object which references a
I have a sinlgeton object which holds one method, witch is NOT synchronized. The
i have an object which holds a user name and job count, which then
I have an ObservableCollection, which holds a Person object. I have a search feature
I have a Serializable object which is supposed to hold a java.awt.Image as its

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.