calculator

M

makko

Hello all,
my very first perl program, a calculator that takes formulas;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##
# CALC 1.0
# running: $perl calc.pl
# elmakko Feb 2005
##

use strict;
use warnings;

my $delimit_arr = "\n\t++++++++++++\n\n";
my $argc = scalar(@ARGV);
if($argc != 1)
{
print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
exit -1;
}
my $ans = eval($ARGV[0]);
system "clear";
banner();
print "\t$ans \n";
print "$delimit_arr";

sub banner()
{
print "\n\t************\n";
print "\t* CALC 1.0 *\n";
print "\t************\n";
print "\n\n";
}

exit 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

regards,
makko.
 
T

thundergnat

makko said:
Hello all,
my very first perl program, a calculator that takes formulas;

First of all, this newsgroup is defunct. In the future, use
comp.lang.perl.misc, or perhaps even better, perl.beginners.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##
# CALC 1.0
# running: $perl calc.pl
# elmakko Feb 2005
##

use strict;
use warnings;
Good!


my $delimit_arr = "\n\t++++++++++++\n\n";
my $argc = scalar(@ARGV);

No need for scalar: my $argc = @ARGV;
if($argc != 1)
{
print "\n/*\n * usage: perl calc.pl <formula>\n */\n\n";
exit -1;
}

Doesn't take into acount spaces in the formulas
my $ans = eval($ARGV[0]);

Ouch. Possibly very dangerous to eval() without any trapping of malicious
entries. Not as big an issue for somthing YOU are going to run on YOUR
computer, but definately not somthing you should get in the habit of doing.
system "clear";

"clear" is not recognized as an internal or external command.
banner();
print "\t$ans \n";
print "$delimit_arr";

sub banner()

Don't use prototypes unless you absolutly need them.

sub banner
{
print "\n\t************\n";
print "\t* CALC 1.0 *\n";
print "\t************\n";
print "\n\n";
}

exit 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Not a horrible first script. Big plusses for use warnings and use strict.
You need to be careful using eval()s though. They may not work exactly
as you expect.

For instance, try the following; does this return the answer you expect?

perl calc.pl 01.5+01.5
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top