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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:31:04+00:00 2026-05-24T18:31:04+00:00

I’m toying around with some code where bundling coderefs with variables makes sense, but

  • 0

I’m toying around with some code where bundling coderefs with variables makes sense, but using full blown OOD doesn’t. Basically, I’m writing functional code but resorting to a select few OO-style structures to encapsulate stuff where statefulness really makes sense. Outside that it’s mostly state-free functions.

Using Moose for this would be like disciplining occasionally-late coworkers with an automatic rifle: complete overkill in more ways than one. So spare me the ‘ALL PERL OOP MUST USE MOOSE NOW!!!’ mantra.

So I ran to the good ol’ blessed-hashref class to do my bidding, but found that writing accessor code is a pain, and didn’t want to pull in non-standard modules for what is essentially a very trivial task.

After shuffling through pages on http://perldoc.perl.org, I stumbled across Class::Struct and Object::Accessor, which are core modules that ease the construction of OO attributes. Perfect, I though. But…

Object::Accessor seemed like the perfect ticket, but I was simply using its mk_accessor sub in the constructor, which didn’t seem anywhere near as elegant as using compile-time Class::Struct, which also wrote my default constructor for me. All in all, I preferred Class::Struct to Object::Accessor because allowed slightly less boilerplate and had a more declarative syntax.

A comparison using trivial examples, in case you’re wondering:

Using Class::Struct:

#!/usr/bin/perl

use 5.014;
use autodie;
use strict;
use warnings;

package Account {
    use Class::Struct
        first_name   => '$',
        last_name    => '$',
        age_in_years => '$',
        activated    => '$',
        ;

    sub formatted {
        my ($self_ref) = @_;
        return sprintf
            "First name:   %s\n" .
            "Last name:    %s\n" .
            "Age in Years: %d\n" .
            "Activated:    %s",
            $self_ref->first_name,
            $self_ref->last_name,
            $self_ref->age_in_years,
            $self_ref->activated || 'No'
            ;
    }
}

my $account = Account->new;

$account->first_name('Tom');
$account->last_name('Smith');
$account->age_in_years(16);
$account->activated('Yes');

say $account->formatted

Using Object::Accessor:

use 5.014;
use autodie;
use strict;
use warnings;

package Account {
    use base 'Object::Accessor';

    sub new {
        my ($type) = @_;
        my $self = bless { }, $type;
        $self->mk_accessors(qw(first_name last_name age_in_years activated));
        return $self;
    }

    sub formatted {
        my ($self_ref) = @_;
        return sprintf
            "First name:   %s\n" .
            "Last name:    %s\n" .
            "Age in Years: %d\n" .
            "Activated:    %s",
            $self_ref->first_name,
            $self_ref->last_name,
            $self_ref->age_in_years,
            $self_ref->activated || 'No'
            ;
    }
}

my $account = Account->new;

$account->first_name('Tom');
$account->last_name('Smith');
$account->age_in_years(16);
$account->activated('Yes');

say $account->formatted;

Really, both are perfectly acceptable, but the former is quite a bit cleaner IMO. What I want to know, is, can I use Class::Struct without specifying the type constraints?

I don’t need them in the context I’m using them, yet I don’t seem to be able to just add accessors without specifying type constraints alongside them. No doubt I loose performance due to unnecessary type checking too.

If this isn’t possible, I’ll just stick to Object::Accessor. But are non-type-constrained accessors possible with Class::Struct?

  • 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-24T18:31:06+00:00Added an answer on May 24, 2026 at 6:31 pm

    I prefer Class::Struct for the task. I am usually using only scalars, they have no validation and you can store anything to them (like arrayref or hashref).

    Another advantage is that syntax you’ve choosen is making your objects based on arrays, which are smaller and have faster access. Nothing dramatic, but if you have many objects, it can help.

    package Account;
    use Class::Struct
        map { $_ => '$' } qw(
            first_name
            last_name
            age_in_years
            activated
            items
        );
    
    package main;
    
    my $acc = Account->new(
        first_name => 'John',
        last_name  => 'Doe',
        items      => ['a' .. 'z']
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.