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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:49:02+00:00 2026-05-28T15:49:02+00:00

I have a class built with Moose that’s essentially a data container for an

  • 0

I have a class built with Moose that’s essentially a data container for an article list. All the attributes – like name, number, price, quantity – are data. “Well, what else?”, I can hear you say. So what else?

An evil conspiration of unfortunate circumstances now forces external functionality into that package: Tax calculation of the data in this class has to be performed by an external component. This external component is tightly coupled to an entire application including database and dependencies that ruin the component’s testability, dragging it into the everything-coupled-together stew. (Even thinking about refactoring the tax component out of the stew is completely out of the question.)

So my idea is to have the class accept a coderef wrapping the tax calculation component. The class would then remain independent of the tax calculation implementation (and its possible nightmare of dependencies), and at the same time it would allow integration with the application environment.

has ‘tax_calculator’, is => ‘ro’, isa => ‘CodeRef’;

But then, I’d have added a non-data component to my class. Why is that a problem? Because I’m (ab)using $self->meta->get_attribute_list to assemble a data export for my class:

my %data; # need a plain hash, no objects
my @attrs = $self->meta->get_attribute_list;
$data{ $_ } = $self->$_ for @attrs;
return %data;

Now the coderef is part of the attribute list. I could filter it out, of course. But I’m unsure any of what I’m doing here is a sound way to proceed. So how would you handle this problem, perceived as the need to separate data attributes and behaviour attributes?

  • 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-28T15:49:03+00:00Added an answer on May 28, 2026 at 3:49 pm

    A possible half thought out solution: use inheritance. Create your class as you do today but with a calculate_tax method that dies if called (i.e. a virtual function). Then create subclass that overrides that method to call into the external system. You can test the base class and use the child class.

    Alternate solution: use a role to add the calculate_tax method. You can create two roles: Calculate::Simple::Tax and Calculate::Real::Tax. When testing you add the simple role, in production you add the real role.

    I whipped up this example, but I don’t use Moose, so I may be crazy with respect to how to apply the role to the class. There may be some more Moosey way of doing this:

    #!/usr/bin/perl
    
    use warnings;
    
    {
        package Simple::Tax;
        use Moose::Role;
    
        requires 'price';
    
        sub calculate_tax {
            my $self = shift;
            return int($self->price * 0.05);
        }
    }
    
    
    {
        package A;
        use Moose;
        use Moose::Util qw( apply_all_roles );
    
        has price => ( is => "rw", isa => 'Int' ); #price in pennies
    
        sub new_with_simple_tax {
            my $class = shift;
            my $obj = $class->new(@_);
            apply_all_roles( $obj, "Simple::Tax" );
        }
    }
    
    my $o = A->new_with_simple_tax(price => 100);
    print $o->calculate_tax, " cents\n";
    

    It appears as if the right way to do it in Moose is to use two roles. The first is applied to the class and contains the production code. The second is applied to an object you want to use in testing. It subverts the first method using an around method and never calls the original method:

    #!/usr/bin/perl
    
    use warnings;
    
    {
        package Complex::Tax;
        use Moose::Role;
    
        requires 'price';
    
        sub calculate_tax {
            my $self = shift;
            print "complex was called\n";
            #pretend this is more complex
            return int($self->price * 0.15);
        }
    }
    
    {
        package Simple::Tax;
        use Moose::Role;
    
        requires 'price';
    
        around calculate_tax => sub {
            my ($orig_method, $self) = @_;
            return int($self->price * 0.05);
        }
    }
    
    
    {
        package A;
        use Moose;
    
        has price => ( is => "rw", isa => 'Int' ); #price in pennies
    
        with "Complex::Tax";
    }
    
    my $prod = A->new(price => 100);
    print $prod->calculate_tax, " cents\n";
    
    use Moose::Util qw/ apply_all_roles /;
    my $test = A->new(price => 100);
    apply_all_roles($test, 'Simple::Tax');
    print $test->calculate_tax, " cents\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a Moose class that i would like to store using Apache::Session::File. However,
I have a Silverlight application that is built from a set of Silverlight class
I have class method that returns a list of employees that I can iterate
In my Qt-based application (built using PyQt 4.8.6), I have a class that is
I have an std::vector -like class that is compiled with Visual C++ 2008. There's
I have built a class that intercepts UIWebView resource loading by subclassing NSURLCache and
I have built a class in PHP and I must declare a class variable
My code is built to multiple .dll files, and I have a template class
I have built a number of asp.net servercontrols into a class library, & 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.