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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:46:08+00:00 2026-06-05T07:46:08+00:00

In the follwoing code I tried to define a trigger for an attribute within

  • 0

In the follwoing code I tried to define a trigger for an attribute within a
parameterized role.

#!/usr/bin/env perl

package WordSizeRoleParameterized;
use MooseX::Role::Parameterized;

# a parameterized role with a trigger for an attribute
# does not work
role {   
    has 'word' => ( is => 'rw', trigger => \&_trigger_word_size,);

    has 'word_size' => ( is => 'ro', writer => '_set_word_size', );

    method '_trigger_word_size' => sub {
        my $self = shift;
        my $size;

        if ( $self->word ) { $size = split //, $self->word; }
        else               { $size = 0; }

        print "WordsizeRoleParameterized::_trigger_word_size() called : size = $size \n";
        $self->_set_word_size($size);
    };
};

1;

package WordSizeRole;
use Moose::Role;

# a plain role with a trigger for an attribute
# works as expected 
    has 'word' => ( is => 'rw', trigger => \&_trigger_word_size,);

    has 'word_size' => ( is => 'ro', writer => '_set_word_size', );

    sub _trigger_word_size {
        my $self = shift;
        my $size;

        if ( $self->word ) { $size = split //, $self->word;}
        else               { $size = 0;}
        $self->_set_word_size($size);
    }

1;

package ClassWordSizeParameterized;
use Moose;
with 'WordSizeRoleParameterized';
1;

package ClassWordSize;
use Moose;
with 'WordSizeRole';
1;

#------------
package main;
#------------

use strict;
use warnings;

# no error
my $wordy = ClassWordSize->new({'word' => 'goodie'});
print "word : '", $wordy->word, "', size: ", $wordy->word_size, "\n";

# but using the paramaeterized role ...
my $wordy_parameterized = ClassWordSizeParameterized->new();

# will cause an error if calling $wordy_parameterized->word('Superdubadubaduuuh')); 
#
# Undefined subroutine &WordSizeRoleParameterized::_trigger_word_size called at accessor
# ClassWordSizeParameterized::word (defined at roleTrigger.pl line 8) line 7.
$wordy_parameterized->word('Superdubadubaduuuh');
print "word : '", $wordy_parameterized->word, "', size: ", $wordy_parameterized->word_size, "\n";

Calling $wordy_parameterized->word(‘Superdubadubaduuuh’), which should fire the trigger
routine results in the error :
Undefined subroutine &WordSizeRoleParameterized::_trigger_word_size called

Interstingly the method WordSizeRoleParameterized::_trigger_word_size exists (you can call $wordy_parameterized->_trigger_word_size();), but it is not fired as trigger routine for the attribute. The documentation of MooseX::Role::Parameterized does not say anything about it.

Is it not allowed or simgply impossible by some implementation details of MooseX::Role::Parameterized ?

  • 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-05T07:46:09+00:00Added an answer on June 5, 2026 at 7:46 am

    It’s more or less an implementation detail of MooseX::Role::Parameterized. When you use the method keyword, it does not create a method in the WordSizeRoleParameterized package. Similarly, has and other keywords you use in the role {} block do not affect the package which uses MooseX::Role::Parameterized. Such declarations affect only the anonymous role that the role {} block generates behind the scenes, and then the classes (or other roles) which consume that anonymous role.

    The problem here is that \&_trigger_word_size is bound to the wrong package (WordSizeRoleParameterized when it should be bound to the anonymous role’s package). Changing \&_trigger_word_size to sub { shift->_trigger_word_size(@_) } does the trick because it uses normal method resolution, so the method will be found in the right package (ClassWordSizeParameterized).

    word : 'goodie', size: 6
    WordsizeRoleParameterized::_trigger_word_size() called : size = 18 
    word : 'Superdubadubaduuuh', size: 18
    

    You’re seeing $wordy_parameterized->_trigger_word_size because the method is copied into the class by way of the anonymous role, not by way of WordSizeRoleParameterized.

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

Sidebar

Related Questions

how to update file to tomcat 5.5. i tried with the follwoing code but
I tried running the following scheme code: (define affiche-gagnant (lambda (j1 j2 g1 g2)
I tried running the following scheme code: (define affiche-gagnant (lambda (j1 j2 g1 g2)
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I tried following code to run timer in class library but timer_Elapsed wasn't fired.
I want to get script's origin filename in global functions. I tried following code,
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I have tried the following code: $('a.buildMenu').click(function (event) { // Prevent normal behaviour event.preventDefault();
I tried the following code, but the cell doesn't change at all: - (void)viewDidLoad
I tried the following code. Although I don't get any errors, it did not

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.