Home arrow Perl Programming arrow Page 3 - Scalars: Building a Currency Converter

Currency Converter - Perl

In this conclusion to a five-part series on scalars in Perl, we'll put everything we've learned together to build a currency converter. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars: Building a Currency Converter
  2. Variable Names
  3. Currency Converter
  4. The chomp() and chop() Functions
  5. The die() Function
By: Apress Publishing
Rating: starstarstarstarstar / 2
April 22, 2010

print this article



Letís begin to wind up this chapter with a real exampleóa program to convert between currencies. This is our very first version, so we wonít make it do anything too clever. As we get more and more advanced, weíll be able to hone and refine it.

#!/usr/bin/perl -w
# currency1.pl

use strict;

my $yen = 105.6;  # as of 02 February 2004 print "49518 Yen is ", (49_518/$yen), " dollars\n";
print "360 Yen is   ", (   360/$yen), " dollars\n";
print "30510 Yen is ", (30_510/$yen), " dollars\n";

Save this, and run it through Perl. You should see something like this:

$ perl currency1.pl
49518 Yen is 468.920454545455 dollars
360 Yen is   3.40909090909091 dollars
30510 Yen is 288.920454545455 dollars

First, we declare the exchange rate to be a lexical variable and set it to105.6.

my $yen = 105.6;

Notice that we can declare and assign a variable at the same time. Now we do some calculations based on that exchange rate:

print "49518 Yen is ", (49_518/$yen), " dollars\n";
print "360 Yen is   ", (   360/$yen), " dollars\n";
print "30510 Yen is ", (30_510/$yen), " dollars\n";

Of course, this is currently of limited use, because the exchange rate changes, and we might want to change some different amounts at times. To do either of these things, we need to be able to ask the user for additional data when we run the program.

Introducing <STDIN>

Perl reads from standard input (the keyboard) with<STDIN>. It reads up to and including the newline character, so the newline is part of the string read in. To read a single line of input from the user we can say something like

print "Please enter something interesting\n";
$comment = <STDIN>;

This will read one line from the user, including the newline character, and assign the string that was read to the variable$comment. Letís use this to get the exchange rate from the user when the program is run. This example will read the exchange rate from the userís keyboard, storing it in$yen:

#!/usr/bin/perl -w
# currency2.pl

use strict;

print "Currency converter\n\nPlease enter the exchange rate: ";
my $yen = <STDIN>;

print "49518 Yen is ", (49_518/$yen), " dollars\n";
print "360 Yen is   ", (   360/$yen), " dollars\n";
print "30510 Yen is ", (30_510/$yen), " dollars\n";

Now when you run the program, youíll be asked for the exchange rate. The currency values will be calculated using the rate you entered:

$ perl currency2.pl
Currency converter

Please enter the exchange rate: 100
49518 Yen is 495.18 dollars
360 Yen is   3.6 dollars
30510 Yen is 305.1 dollars

Note that this time we read the exchange rate from the userís keyboard and it was read in as a string. Perl converts the string to a number in order to perform the calculation.

So far, we havenít done any checking to make sure that the exchange rate given makes sense; this is something weíll need to think about in the future.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: