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

The die() Function - 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



The die() function is how we handle severe errors in Perl. It takes a character string argument and prints it to standard error output (this normally prints to the screen like standard output does). If the argument string does not end in newline, the \n character, die() automatically appends to the output string the name of the Perl program and the line number of the program where thedie()was executed; this is very helpful—it tells us right where the error took place. Thendie()cleans up the program and exits with a non-0 exit status. Therefore,die()is a permanent solution—the program terminates:

die "there was an error";

Here is an example of usingdie():

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

use strict;

print "please enter a string to pass to die: ";
chomp(my $string = <STDIN>);

print "didn't make it this far...\n";

Executing this code would resemble

$ perl die.pl
please enter a string to pass to die: this is the end
this is the end at die.pl line 9, <STDIN> line 1.

Notice that the name of the script and the line number is automatically added to the output ofdie()because the argument todie()did not end in the newline character. Also notice that the lastprint()is not executed because the program terminated whendie()executed.


Perl’s basic data type is a scalar. A scalar can be either an integer, floating point number, or string. Perl converts between these three automatically when necessary.

Double- and single-quoted strings differ in the way they process the text inside them. Single-quoted strings do little to no processing at all, whereas double-quoted strings interpolate escape sequences and variables.

We can operate on these scalars in a number of ways—ordinary arithmetic, bitwise arithmetic, string manipulation, and logical comparison. We can also combine logical comparisons with Boolean operators. These operators vary in precedence, which is to say that some take effect before others, and as a result we must use parentheses to enforce the precedence we want.

Scalar variables are a way of storing scalars so that we can get at them and change their values. Scalar variable names begin with a dollar sign ($) and are followed by one or more alphanumeric characters or underscores. There are two types of variables—lexical and global. Globals exist all the way through the program, and so can be troublesome if we don’t keep very good track of where they are being used. Lexicals have a life span of the current block, and so we can use them safely without worrying about clobbering similarly named variables somewhere else in the program.

<STDIN>reads in from standard input, which is normally the user’s keyboard. We can store this input in a variable and then operate upon it, making our programs more flexible.<STDIN>reads up to and including the newline character, and we normallychomp() off the newline.

Two ways to terminate our programs are by usingexit()anddie().die()is helpful because it prints its argument, and if that argument does not end in\n, it magically adds the script name and line number to the output which helps us locate the error.


  1. Change the currency conversion program so that it asks for an exchange rate and three prices to convert.
  2. Write a program that asks for a hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal. 
  3. Write a program that asks for a decimal number less than 256 and converts it to binary. (Hint: You may want to use the bitwise and operator 8 times.) 
  4. Without the aid of the computer, work out the order in which each of the following expressions would be computed, and their value. Put the appropriate parentheses in to reflect the normal precedence: 
    • 2+6/4-3*5+1 
    • 17+-3**3/2 
    • 26+3^4*2 
    • 4+3>=7||2&4*2<4 





>>> 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: