Dynamically access module methods

Posted on 2005-05-07
Last Modified: 2010-03-05
I need to dynamically build the path to a method in a module, but don't know the proper syntax. Something like this:

my $calc_type = 'interest';

my $price = Auto::Calc::$calc_type('10000');

Question by:zumpoof
    LVL 84

    Expert Comment

    my $calc_type = \&Auto::Calc::interest;

    my $price = &$calc_type('10000');

    Author Comment

    Sorry, let me clarify. The method within the 'Auto::Calc' module needs to be dynamically accessed. The 'intrest' method does exist, but I don't want to hard code it when I want to access it. Perhaps an easier example is this:

    $method[0] = 'interest';
    $method[1] = 'monthly';

    $total_interest = Auto::Calc::$method[0];
    $monthly_payment = Auto::Calc::$method[1];

    I'm trying to figure out how to have methods set to variables.

    LVL 84

    Accepted Solution

    $method[0] = \&Auto::Calc::interest;
    $method[1] = \&Auto::Calc::monthly;

    $total_interest = &{$method[0]};
    $monthly_payment = &{$method[1]};

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Most Common two letters at the start of a word. 1 70
    Using Perl to parse rows 7 82
    Awk Question 2 106
    batch script for automated email 12 90
    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now