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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:27:00+00:00 2026-05-13T05:27:00+00:00

I’m looking for a way to set up some helper methods from within a

  • 0

I’m looking for a way to set up some helper methods from within a parent Moose class, rather than a standalone utility class. If it is possible, it would be a more transparent way of adding Moose sugar to modules, as it does not require explicitly requiring any helper modules (as everything would come via the extends declaration).

Based on the example provided in the documentation, this is roughly what I am going for:

package Parent;

use Moose;

Moose::Exporter->setup_import_methods(
    with_meta => [ 'has_rw' ],
    as_is     => [ 'thing' ],
    also      => 'Moose',
);

sub has_rw {
    my ( $meta, $name, %options ) = @_;
    $meta->add_attribute(
        $name,
        is => 'rw',
        %options,
    );
}

# then later ...
package Child;

use Moose;
extends 'Parent';

has 'name';
has_rw 'size';
thing;

However this does not work:

perl -I. -MChild -wle'$obj = Child->new(size => 1); print $obj->size'
String found where operator expected at Child.pm line 10, near "has_rw 'size'"
        (Do you need to predeclare has_rw?)
syntax error at Child.pm line 10, near "has_rw 'size'"
Bareword "thing" not allowed while "strict subs" in use at Child.pm line 12.
Compilation failed in require.
BEGIN failed--compilation aborted.

PS. I’ve also tried moving the export magic into a role (with Role; rather than extends Parent;) but the same errors occur.

  • 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-13T05:27:01+00:00Added an answer on May 13, 2026 at 5:27 am

    This isn’t supported, and there is a good reason why. A Class or a Role are not the same as sugar methods, and at some level different things should be different. If your problem is having to “use” Moose + A Custom Sugar package then you can solve that by simply having your custom sugar package export Moose as well, stealing from your example:

    package MySugar;
    use strict;
    use Moose::Exporter;
    
    Moose::Exporter->setup_import_methods(
        with_meta => [ 'has_rw' ],
        as_is     => [ 'thing' ],
        also      => 'Moose',
    );
    
    sub has_rw {
        my ( $meta, $name, %options ) = @_;
        $meta->add_attribute(
            $name,
            is => 'rw',
            %options,
        );
    }
    

    Then you simply say:

    package MyApp;
    use MySugar; # imports everything from Moose + has_rw and thing    
    extends(Parent);
    
    has_rw 'name';
    has 'size';
    thing;
    

    This is how MooseX::POE works, as well as several other packages. I for one would argue against having extends bring in the sugar like you’re suggesting here because a Class is not a bundle of sugar functions, and the two really should never be confused.

    UPDATE: To bring in both at once the cleanest approach is to rework Parent as a role that is applied to Moose::Object.

    package Parent::Methods;
    use 5.10.0;
    use Moose::Role;
    
    sub something_special { say 'sparkles' }
    

    Then we simply change the call to Moose::Exporter in MySugar to look like

    Moose::Exporter->setup_import_methods(
        apply_base_class_roles => 'Parent::Methods',
        with_meta              => ['has_rw'],
        as_is                  => ['thing'],
        also                   => 'Moose',
    );
    

    Now you can simply say

    package MyApp;
    use MySugar; 
    
    has_rw 'name';
    has 'size';
    thing;
    
    package main;
    MyApp->new->something_special; # prints sparkles
    

    Which is I believe the final detail you were wanting.

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

Sidebar

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.