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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:29:08+00:00 2026-05-12T12:29:08+00:00

In the example module below, the getters and setters are generated by adding anonymous

  • 0

In the example module below, the getters and setters are generated by adding anonymous subroutines to the symbol table. After the methods have been created in this manner, will the resulting code be functionally equivalent (in terms of behavior, speed, etc.) to a module with manually-written getters and setters, or does this approach have some sort of inherent liability? (I have done some basic speed benchmarking and have not detected any differences so far.)

package Module;    
use strict;
use warnings;

BEGIN {
    my @attr = qw(author title number);
    no strict 'refs';
    for my $a (@attr){
        *{__PACKAGE__ . "::get_$a"} = sub { $_[0]->{$a}         };
        *{__PACKAGE__ . "::set_$a"} = sub { $_[0]->{$a} = $_[1] };
    }
}

sub new {
    my $class = shift;
    bless { @_ }, $class;
}

1;
  • 1 1 Answer
  • 1 View
  • 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-12T12:29:08+00:00Added an answer on May 12, 2026 at 12:29 pm

    There should be no difference in runtime performance if the resulting code is the same in both cases. This is usually not possible, however, unless you use string eval to create your subroutines. For example, the code you provided:

    ... = sub { $_[0]->{$a} };
    

    will be ever-so-slightly slower than the code you would have written manually:

    sub foo { $_[0]->{'foo'} }
    

    simply because the former has to get the value of the variable $a before using it as a key to the hash, whereas the later uses a constant as its hash key. Also, as an aside, shift usually tends to be faster than $_[0]. Here’s some benchmark code:

    use Benchmark qw(cmpthese);
    
    package Foo;
    
    sub manual_shift { shift->{'foo'} }
    sub manual_index { $_[0]->{'foo'} }
    
    my $attr = 'foo';
    
    *dynamic_shift = sub { shift->{$attr} };
    *dynamic_index = sub { $_[0]->{$attr} };
    
    package main;
    
    my $o = bless { foo => 123 }, 'Foo';
    
    cmpthese(-2, {
      manual_shift  => sub { my $a = $o->manual_shift },
      manual_index  => sub { my $a = $o->manual_index },
      dynamic_shift => sub { my $a = $o->dynamic_shift },
      dynamic_index => sub { my $a = $o->dynamic_index },
    });
    

    and the results on my system:

                       Rate dynamic_index  manual_index dynamic_shift  manual_shift
    dynamic_index 1799024/s            --           -3%           -4%           -7%
    manual_index  1853616/s            3%            --           -1%           -4%
    dynamic_shift 1873183/s            4%            1%            --           -3%
    manual_shift  1937019/s            8%            4%            3%            --
    

    They’re so close that differences may get lost in the noise, but over many trials I think you’ll see that the “manual shift” variant is the fastest. But as with all microbenchmarks like this, you have to test your exact scenario on your hardware and your version of perl to be sure of anything.

    And here’s string eval thrown into the mix.

    eval "sub eval_index { \$_[0]->{'$attr'} }";
    eval "sub eval_shift { shift->{'$attr'} }";
    

    It should be exactly the same as the “manual” variants, plus or minus the statistical noise. My results:

                       Rate dynamic_index manual_index dynamic_shift manual_shift eval_shift eval_index
    dynamic_index 1820444/s            --          -1%           -2%          -3%        -4%        -5%
    manual_index  1835005/s            1%           --           -1%          -2%        -3%        -4%
    dynamic_shift 1858131/s            2%           1%            --          -1%        -2%        -3%
    manual_shift  1876708/s            3%           2%            1%           --        -1%        -2%
    eval_shift    1894132/s            4%           3%            2%           1%         --        -1%
    eval_index    1914060/s            5%           4%            3%           2%         1%         --
    

    Again, these are all so close that you’d have to take great pains and perform many trials to sort out the signal from the noise. But the difference between using a constant as a hash key and using a variable (whose value must first be retrieved) as a hash key should show through. (The shift optimization is a separate issue and is more likely to change one way or the other in past or future versions of perl.)

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

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server=HOME\SQLEXPRESS;Database=master;Integrated Security=True" $SqlConnection.Open() $SqlCmd… May 12, 2026 at 10:51 pm
  • Editorial Team
    Editorial Team added an answer In general, I have found that putting as much logic… May 12, 2026 at 10:51 pm
  • Editorial Team
    Editorial Team added an answer Unfortunately this is a pretty tricky thing to do, the… May 12, 2026 at 10:51 pm

Related Questions

We have a web service framework we use; which I had no part in
This is for Excel and VBA. Assume that BondClass has been properly defined in
I know this must be a trivial question, but I've tried many different ways,
I am relatively new to mod_rewrite, but have a site which I would like

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.