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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:19:01+00:00 2026-06-18T00:19:01+00:00

By way of teaching myself Moose, I’m working on a Moose object that interfaces

  • 0

By way of teaching myself Moose, I’m working on a Moose object that interfaces to a particular piece of hardware. Said hardware takes a number of different commands that set various properties of the hardware, all of the form PROPERTYNAME=VALUE for a setter, and PROPERTYNAME? for a getter (note that these ‘setters’ and ‘getters’ are on the network interface to the hardware). What I want to do is create an object where all of these properties of the hardware are implemented with an attribute-like interface. Since getting and setting the various properties takes the same form for all properties, is there a way to automatically generate the setters and getters from a list of those properties?

I.E.: Rather than this:

Package MyHardware;
use Moose;
has property1 => (
    'is' => 'rw',
    'reader' => 'set_property1',
    'writer' => 'get_property1',
);

has property2 => (
    'is' => 'rw',
    'reader' => 'set_property2',
    'writer' => 'get_property2',
);

# ...

has propertyN => (
    'is' => 'rw',
    'reader' => 'set_propertyN',
    'writer' => 'get_propertyN',
);

Is there something I can do like this:

Package MyHardware;
use Moose;

attributes => (
    'is' => 'rw',
    'names' => [qw/property1 property2 ... propertyN/],
    'reader' => sub {
        my $self = shift;
        my $property = shift;
        return $self->_send_command("$property?");
    },
    'writer' => sub {
        my $self = shift;
        my $property = shift;
        my $value = shift;
        return $self->_send_command("$property=$value");
    },
);

EDIT: Here’s what I want to happen:

# CALLER:
my $hw = MyHardware->new();
$hw->property1('foo');
print $hw->property2 . "\n";

And “under the hood”:

$hw->property1('foo');
# Becomes 
sub { return $hw->_send_command('property1=foo'); }

# And

$hw->property2();
# Becomes
sub { return $hw->_send_command('property2?'); }
  • 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-18T00:19:02+00:00Added an answer on June 18, 2026 at 12:19 am

    Figured it out. I realize that I shouldn’t be using attributes at all to do this. Instead, I’ll dynamically generate methods using Class::MOP::Class like so:

    my $meta = Class::MOP::Class->initialize(__PACKAGE__);
    foreach my $prop (qw/property1 property2 property3/) {
        $meta->add_method(qq/set_$prop/, sub { 
                my $self = shift;
                my $value = shift;
                return $self->_send_command(qq/$prop=$value/);
            }
        );
        $meta->add_method(qq/get_$prop/, sub { 
                my $self = shift;
                return $self->_send_command(qq/$prop?/);
            }
        );
    }
    

    Doing it with calls to has() would have effectively put the object state in two places – on the hardware and in the instance – and I only want it in one.

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

Sidebar

Related Questions

I am teaching myself Objective-C utilizing a number of resources, one of which is
I'm teaching myself (read hacking) through some old Excel Macros that are quite long.
I know my way around ruby pretty well and am teaching myself C starting
Teaching myself some c# and my problem is that I have a class which
Im teaching myself using Zed Shaw's Learn Python The Hard Way, and I'm having
I'm teaching myself Django, and I'm trying to do a test thing such that
I've been teaching myself Python through the book Learn Python The Hard Way (2nd
I am currently teaching myself iPhone programming and working on solving assignment 2 [PDF]
I am teaching myself jquery it seems while working on this project. I assumed
I recently began teaching myself (terrible) SQLite. One thing that struck me as annoying

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.