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

The chomp() and chop() Functions - 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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

<STDIN>reads up to and including the newline character. Sometimes we donít want to include the newline in the text we have read, so we canchomp()the newline off the string.

Thechomp()function removes the last character of a string if and only if it is the newline character. For instance:

$string = "testing 1, 2, 3\n";
chomp($string);                # $string is now "testing 1, 2, 3"

Since<STDIN>reads up to and including the newline character, this code reads and then removes the newline:

my $input = <STDIN>;
chomp($input);

Those two statements can be combined into one:

chomp(my $input = <STDIN>);

A related function ischop(). It removes the last character of a string, regardless of what character it is. Here is an example:

$string = "testing, 1, 2, 3";
chop($string);                 # $string is now "testing 1, 2, "

Two Miscellaneous Functions

Before we end our discussion of scalars, we should discuss two functions that are often used to terminate Perl programs: exit() and die().

The exit() Function

The exit() function exits the program. If an argument is provided, it returns that value back to the calling program (or shell). If no argument is provided, it returns back the value 0. In the shell, the value 0 means that the program terminated normally, so we can report that all is well with

exit(0);

or

exit;

If the program exits abnormally due to some error condition, simply return back a nonzero value to tell the calling program that all is not well:

exit(1);

Here is an example of using theexit()function:

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

use strict;

print "enter value to return back to the calling program: ";
chomp(my $value = <STDIN>);

exit($value);

In Unix, we can echo out the value$?to see the return value of the most recent command:

$ perl exit.pl
enter value to return back to the calling program: 0
$ echo $?
0
$ perl exit.pl
enter value to return back to the calling program: 255
$ echo $?
255
$



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

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