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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:12:43+00:00 2026-06-03T22:12:43+00:00

I am trying to pass a reference to an attribute’s writer method to another

  • 0

I am trying to pass a reference to an attribute’s writer method to another class method. The setter method is apparently invoked since the print statement is executed. However, the following error results:

Attribute (dir) is required, so cannot be set to undef at Test.pm line 51
Test::_validateDir('Test=HASH(0x2270be0)', 'CODE(0x22a90f8)') called at test.pm line 37
Test::BUILD('test_moose=HASH(0x2270be0)', 'HASH(0x1879728)') called at generated method (unknown origin) line 50
Test:new('Test', dir=>'my_bin') called at ./test_moose.pl line 26

Is there a reason why a reference to the generated writer method cannot be taken or is there a different or better way to do this in Moose?

Here’s the code:

package Test;
use Moose;
use File::Spec;

has 'dir' => (is => 'ro', isa => 'Str', required => 1, writer '_set_dir',);

sub BUILD {
    my ($self, $hr) = @_;
    $self->_validateDir(\&_set_dir);
}

sub _validate_dir {
    my ($self, $setter) = @_;
    my $dir = File::Spec->rel2abs($self->dir, '/home/me');
    if (! -e $dir) {
       print "creating directory\n";
       #create directory
    }
    &$setter($dir);
}
  • 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-06-03T22:12:44+00:00Added an answer on June 3, 2026 at 10:12 pm

    Is it right to call the setter as a regular sub? Shouldn’t you pass $self as a first argument?

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

Sidebar

Related Questions

I'm trying to pass a variable by reference to a different class, but I
I am trying to pass a pointer by reference to an object from class
I'm trying to pass one method to another in elisp, and then have that
I am trying to pass current object reference i.e. self to the Timer class
I'm trying to return a reference to an object, not pass it by value.
I am trying pass class as value in hashmap. I need to get the
I'm trying to pass a reference to a variable and then update the contents
I'm trying to pass an NSString by reference but it doesn't work. This is
I'm trying to design a class which can update a reference to an object
I'm trying to pass an object (a reference to the currently logged on user,

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.