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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:56:37+00:00 2026-05-20T07:56:37+00:00

I want to coerce a Str into a DBIx::Class::Row object for an attribute in

  • 0

I want to coerce a Str into a DBIx::Class::Row object for an attribute in my Moose class. To do this I need to perform a lookup on the DBIC schema to find the row. I want to push an error onto an ArrayRef attribute if the lookup failed.

I currently pass the schema in as an attribute to my class.

With coercion it appears I cannot access the object so I cannot push onto the error arrayref attribute or use the schema object to perform my lookup.

An alternative I tried was to use ‘around’ to lookup and set the attribute when set, but this of course does not get called when the attribute value is passed via the constructor.

Is this possible, or does someone have an alternative implementation to do what I want to achieve?

  • 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-20T07:56:38+00:00Added an answer on May 20, 2026 at 7:56 am

    You can catch and mutate a value being stored when passed into a constructor with an attribute initializer. (However, it is only run when the attribute is set in the constructor, not at any other time.) Documentation for initializers can be found in Class::MOP::Attribute.

    Since this only catches cases where the attribute is set via the constructor, you still need to catch the other cases where the attribute is set. This can be done with a method modifier as you said, but you can combine the two into one method, wrapped around the autogenerated accessor:

    has my_attr => (
        is => 'rw',
        isa => 'DBIx::Class::Row',
        initializer => 'my_attr',
    );
    
    # my_attr is the autogenerated accessor - we method-modify it to mutate the
    # value being set, and catch cases where it is called as an initializer.
    
    # called for reads, writes, and on initialization at construction time
    around 'my_attr' => sub {
        my $orig = shift;
        my $self = shift;
        # value is not defined if being called as a reader
        # setter and attr are only defined if being called as an initializer
        my ($value, $setter, $attr) = @_;
    
        # the reader behaves normally
        return $self->$orig if not @_;
    
        # convert the string to the row object
        my $row = $self->convert_str_to_row_obj($value);
    
        # if called as an initializer, set the value and we're done
        return $setter->($row) if $setter;
    
        # otherwise, call the real writer with the new value
        $self->$orig($row);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
Short version: Why don't I have to coerce 60, and int, into a double,
I have a concrete class A that extends BaseA and implements InterfaceA. I want
I want this code to just work: def main(): c = Castable() print c/3
I want to use a temp directory that will be unique to this build.
Want to check if this a good example for representing the abstract factory pattern.
I want to set the 'lock' column on a row only if it is
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to loop over the contents of a text file and do a

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.