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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:53:55+00:00 2026-06-07T20:53:55+00:00

More easily explained with an example: my $o = SpecialEffects->new( config => ‘a’ );

  • 0

More easily explained with an example:

my $o = SpecialEffects->new( "config" => 'a' );
my $p = SpecialEffects->new( "config" => 'b' );

$o->sound(); # aliased to fizz(); same as $o->fizz()
$p->sound(); # aliased to clonk(); same as $p->clonk()

Is it possible to do this in Perl? Perhaps using some typeglob or coderef trickery?

I’m trying to keep the SpecialEffects interface simple. I’d prefer not to start building an object hierarchy. The sound() method is what’s exposed, only its behaviour can be configured slightly.

I already know that you can alias with *sound = \&fizz; but that is a global thing as far as I know, and I’d like it encapsulated in the object.

  • 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-07T20:53:58+00:00Added an answer on June 7, 2026 at 8:53 pm

    The simple, easy, non-wizardry way is to just store a method name in your SpecialEffects object, set it according to whatever you want to happen, and call it from sound().

    package SpecialEffects;
    
    sub new {
        my $type = shift;
        my %options = @_;
        my $self = {};
        bless $self, $type;
        if($options{config} eq 'a') {
            $self->{sound_method} = 'fizz';
        } elsif($options{config} eq 'b') {
            $self->{sound_method} = 'clonk';
        }
        return $self;
    }
    
    sub sound {
        my $self = shift;
        my $method_name = $self->{sound_method};
        $self->$method_name();
    }
    
    sub fizz {
        print "fizz\n";
    }
    
    sub clonk {
        print "clonk\n";
    }
    

    You can store and use coderefs about as easily as method names, if you want to be more wizardly.

    package SpecialEffects;
    
    sub new {
        my $type = shift;
        my %options = @_;
        my $self = {};
        bless $self, $type;
        if($options{config} eq 'a') {
            $self->{sound_code} = $self->can('fizz');
        } elsif($options{config} eq 'b') {
            $self->{sound_code} = $self->can('clonk');
        }
        return $self;
    }   
    
    sub sound {
        my $self = shift;
        my $code = $self->{sound_code};
        $self->$code();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably explained more easily with an example. I'm trying to find a
It's more easily explained in code: class Bippy { val x = 42 class
To explain my problem more easily I will create the following fictional example, illustrating
More WCF woes... :) All my workflows implement the same 3 methods. After a
More than one time I have encountered the following problem when building code with
More C++ learning questions. I've been using vectors primarily with raw pointers with a
More of a general MATLAB question than looking for programming advice -- if I
More often than I'd have hoped I seem to come across method/property definitions in
More specifically I want to test whether Oracle ODP.Net is installed on a machine.
More specifically, is List(T)(IEnumerable(T)) thread-safe if the IEnumerable used to initialize the list is

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.