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

  • Home
  • SEARCH
  • 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 7624405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:57:21+00:00 2026-05-31T04:57:21+00:00

FORWARD NOTE: Please, for the sake of this discussion, let’s ignore for a moment

  • 0

FORWARD NOTE: Please, for the sake of this discussion, let’s ignore for a moment the fact that the same end can be achieved with recourse to Class::Accessor, or even simply by using Moose (probably with better results when accounting for code readability and maintainability).

With regards to object oriented Perl, the book Programming Perl discusses the ability of generating accessor methods with closures. For example, this is a valid piece of code:

#!perl

use v5.12;
use warnings;

# at run-time
package Person1;

my @attributes = qw/name age address/;

for my $att ( @attributes )
{
  my $accessor = __PACKAGE__ . "::$att";

  no strict 'refs'; # allow symbolic refs to typeglob

  *$accessor = sub {
    my $self = shift;
    $self->{$att} = shift if @_;
    return $self->{$att};
  };
}

sub new { bless {}, shift }

package main;

use Data::Dumper;

my $dude = Person1->new;
$dude->name('Lebowski');
say Dumper($dude);

On the above example, if I am not mistaken, the class is composed at run-time, with its accessors being created at the same time as the class is being instantiated. This means that there will be a speed penalty on object creation.

Now consider the following alternative:

#!perl

use v5.12;
use warnings;

package Person2;

BEGIN
{
  for my $att (qw/name age address/)
  {
    my $accessor = __PACKAGE__ . "::$att";

    no strict 'refs'; # allow symbolic refs to typeglob

    *$accessor = sub {
      my $self = shift;
      $self->{$att} = shift if @_;
      return $self->{$att};
    };
  }
}

sub new { bless {}, shift }

package main;

use Data::Dumper;

my $dude = Person2->new;
$dude->name('Lebowski');
say Dumper($dude);

On this version, the composition is made within a BEGIN block (ie, at compile-time), and I believe that by dealing with this task as soon as possible on the life-cycle of the program, I am saving time during object instantiation on run-time.

A simple Benchmark,

# benchmark it!
package main;

use Benchmark qw/cmpthese/;

cmpthese(-2, {
  accessors_new   => sub { Person1->new },
  accessors_begin => sub { Person2->new },
});

seemingly supports my theory with these results:

                    Rate accessors_begin   accessors_new
accessors_begin 853234/s              --             -9%
accessors_new   937924/s             10%              --

Assuming that my reasoning has been correct so far,

  • What other benefits/drawbacks exist when comparing both of these strategies?
  • Is it a good idea to depend on BEGIN blocks as an efficient way of doing this sort of class manipulation?
  • When is it not recommended?
  • 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-31T04:57:23+00:00Added an answer on May 31, 2026 at 4:57 am

    When I run your benchmark I get quite a bit of flutter which could account for your differences. For anything 10% difference or less, run it several times to be sure.

                         Rate accessors_begin   accessors_new
    accessors_begin 1865476/s              --             -4%
    accessors_new   1943339/s              4%              --
    
                         Rate accessors_begin   accessors_new
    accessors_begin 1978799/s              --             -1%
    accessors_new   2001062/s              1%              --
    
                         Rate   accessors_new accessors_begin
    accessors_new   1943339/s              --             -2%
    accessors_begin 1988089/s              2%              --
    
                         Rate accessors_begin   accessors_new
    accessors_begin 1796509/s              --             -8%
    accessors_new   1949296/s              9%              --
    
                         Rate accessors_begin   accessors_new
    accessors_begin 1916122/s              --             -3%
    accessors_new   1969595/s              3%              --
    

    But really all you’re benchmarking is sub new { bless {}, shift }. Benchmarking the same thing against itself is going to emphasize flutter. The work generating the accessors has already been done when the code loaded and never comes into it, BEGIN block or not.

    Perl does not have a single compile-time and run-time. Rather, each thing used, required or evaled goes through it’s own compile and runtime steps. use Some::Class causes Some/Class.pm to go through both compile and runtime executing BEGIN, compiling subroutines and then executing any other code. Whether code is inside or outside a BEGIN block within a module makes little difference to code outside that module.

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

Sidebar

Related Questions

Please note that although it sounds similar, this is not the common how to
im looking forward for a 3rd part tool/solution that can monitor my server's network
can someone please tell me can we forward declare a boost::thread variable. boost::thread t(thread);
Please note this code was not written by me. Otherwise I would not be
Note: I could not find a straight-forward answer to this problem so I will
In forward referencing language such as c#, how does the compiler handle this? What
Is there any straight forward way to do that? I want to give an
This is pretty straight forward. EDIT: Updated question and added fourth echo. Here is
I would like to create simple C++ calculator using bison and flex. Please note
I just want to play a very simple, straight forward note by giving my

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.