> Hello Perl Beginners, > > I'm writing a program in perl that collects data about calls into a > telephone system and presents some statistics based on it. There could > come a time in the future where different data needs to be used and > different statistics need to be reported on so I'm trying to keep the > entire thing as generic as possible. I have a conf file telling me what > data represents what statistics and what formula needs to be used to get > that data. > > Then I began to wonder if I store the formulae in variables then how do > I get them out again and get perl to use them as statements? > > Here is some example code I wrote while trying to figure this out, any > help would be appreciated. Is this even a good way to do this? > > -- Code Starts -- > > use strict; > use diagnostics; > > # Data and Formula will eventually be read from a conf file and there > may be multiple > # instances of them that need to stay grouped and indexed so they're in > a hash. > > my (%data, %formula); > > # Dummy Test Data > > $data{"ext1"} = 12; > $data{"ext2"} = 9; > $data{"ext3"} = 10; > > # Dummy Test Formula > > $formula{"Addition"} = "ext1 + ext2"; > $formula{"Subtraction"} = "ext3 - ext2"; > $formula{"Brackets"} = "(ext2 + ext3) - ext1"; > > # I can quite easily print these out and I could put the $data{"extX"} > in with a > # regular expression but how do I get it to evaluate the variable as if > it > # were an expression? >
perldoc -f eval Depending on where your data is coming from it might be advisable to read through perldoc perlsec > print $formula{"Addition"} . "\n"; > print $formula{"Subtraction"} . "\n"; > print $formula{"Brackets"} . "\n"; > > -- Code Ends -- > > Thanks, > > Anthony Murphy HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>