?
Solved

Dynamically access module methods

Posted on 2005-05-07
3
Medium Priority
?
195 Views
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');


Thanks,
Zumpoof
0
Comment
Question by:zumpoof
  • 2
3 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 13951942
my $calc_type = \&Auto::Calc::interest;

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

Author Comment

by:zumpoof
ID: 13952276
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.

Thanks,
Zumpoof
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 13952480
$method[0] = \&Auto::Calc::interest;
$method[1] = \&Auto::Calc::monthly;

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

809 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