For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > September 2004 > Using variables to store statements/formulae in perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using variables to store statements/formulae in perl
Anthony Murphy

2004-09-29, 4:58 am

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?=20

-- Code Starts --

use strict;
use diagnostics;

# Data and Formula will eventually be read from a conf file and there
may be multiple=20
# instances of them that need to stay grouped and indexed so they're in
a hash.

my (%data, %formula);

# Dummy Test Data

$data{"ext1"} =3D 12;
$data{"ext2"} =3D 9;
$data{"ext3"} =3D 10;

# Dummy Test Formula

$formula{"Addition"} =3D "ext1 + ext2";
$formula{"Subtraction"} =3D "ext3 - ext2";
$formula{"Brackets"} =3D "(ext2 + ext3) - ext1";

# I can quite easily print these out and I could put the $data{"extX"}
in with a=20
# regular expression but how do I get it to evaluate the variable as if
it=20
# were an expression?

print $formula{"Addition"} . "\n";
print $formula{"Subtraction"} . "\n";
print $formula{"Brackets"} . "\n";

-- Code Ends --

Thanks,

Anthony Murphy
Wiggins d Anconia

2004-09-29, 10:45 am

> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com