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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:47:53+00:00 2026-05-24T17:47:53+00:00

Building on from a previous query regarding Class::Struct vs Object::Accessor, I’d like to find

  • 0

Building on from a previous query regarding Class::Struct vs Object::Accessor, I’d like to find the best way to assign package subs from coderefs in object constructors. Hey, I’ve got the OO attributes down, so lets get to the methods 🙂

Note that I said ‘best’ way. I’ve already found two ways of doing it which also seem to be popular answers in similar questions, but they require circumventing strict and warnings respectively.

Note that the example gives the class-user the ability to override the formatting method at construction. Poor example, but you get the idea. To preemptively rebut the OO-purists, this makes more sense in my particular design than making the class-user subclass it with their own overridden version.

Here are my two attempts:

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

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

    sub new {
        my ($type, %args) = @_;

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

        $self->first_name(  $args{first_name  } // 'Default First Name'  );
        $self->last_name(   $args{last_name   } // 'Default Last Name'   );
        $self->age_in_years($args{age_in_years} // 'Default Age in Years');
        $self->activated(   $args{activated   } // 'Default Activated'   );

        {
            # Stop skim reading and look here!

            no warnings 'once';
            *formatted = $args{formatted} || sub {
                return 'Default formatting routine';
            };
        }

        return $self;
    }
}

my $account = Account->new;
say $account->formatted;

# Output: Default formatting routine

And the other one:

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

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

    sub new {
        my ($type, %args) = @_;

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

        $self->first_name(  $args{first_name  } // 'Default First Name'  );
        $self->last_name(   $args{last_name   } // 'Default Last Name'   );
        $self->age_in_years($args{age_in_years} // 'Default Age in Years');
        $self->activated(   $args{activated   } // 'Default Activated'   );

        {
            # Stop skim reading and look here!

            no strict 'refs';
            *{'formatted'} = $args{formatted} || sub {
                return 'Default formatting routine';
            };
        }

        return $self;
    }
}

my $account = Account->new;
say $account->formatted;

# Output: Default formatting routine

The former approach should work without warnings, but it doesn’t because Perl thinks I’m not using it anywhere and flags it up as a warning at runtime. The latter doesn’t work without circumventing strict, and that’s how it should be 🙂

All I’m doing is assigning a method to a user-supplied coderef. There should be an easy way of doing this.

Note that defining a new sub that calls the user defined coderef doesn’t work either, since the lexical %args can’t be accessed from it.

Whilst our assigns package variables, it can’t do subs.

Refraining the escape from the garden of strict and warnings, what is the cleanest way to do this? Hell, there’s probably a way to do this using the Object::Accessor module that I just don’t know about.

  • 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-24T17:47:53+00:00Added an answer on May 24, 2026 at 5:47 pm

    It could be stored in $self:

    $$self{formatted} = sub { ... };
    

    Or put in a lexical:

    my $formatted = sub { 1; }; # Dummy sub for initial assignment
    sub new
    {
        ...
        $formatted = sub { ... };
    }
    
    sub formatted { $formatted->(@_) }
    

    Or just put up with the no strict ‘refs’. As long as it’s in a small, well-contained block like you’re doing above, I’ve never considered it evil. Or rather, it is evil, but a small, well-contained one.

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

Sidebar

Related Questions

I'm building a quick csv from a mysql table with a query like: select
Say I'm building an ASP.Net class that inherits from IHttpHandler , should I wire
Has anyone out there got a good set of instructions for building/compiling Ruby from
I'm building a python application from some source code I've found Here I've managed
I am building application that required some data from iPhone's Call log(read only). The
We're building tools to mine information from the web. We have several pieces, such
I'm building a PHP page with data sent from MySQL. Is it better to
I'm building a Silverlight application and one of my caveats from last time was
I'm building an application in Java to retrieve data from MySQL database. After executing
I am building a relatively simple Django application and apart from the main page

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.