[Courses] [Perl] Perl course part 3

Alice Wood alice at schiffwood.co.uk
Sun Apr 13 02:53:52 EST 2003


LinuxChix Perl Course Part 3: User Input, chomp and chop.

Contents

1) User input
2) Chomp and chop
3) Exercises
4) Where to find out more...

           -----------------------------------------

1) User input from the keyboard:

The line input operator <>, (also known as the diamond operator) allows the 
program to read data (or even
futher code) from file handles.  In particular the special filehandle
STDIN can be used to read input from the keyboard.  For example:

$line = <STDIN>;

Each time we use this construct the next complete line of text is read in.
The definition of a "complete line of text" depends on the value of $/
which is newline by default. Read the INPUT_RECORD_SEPARATOR
section of "perldoc perlvar" for more information about $/.

Example:

Type the following into a file called mycat.pl

#!/usr/bin/perl -w
use strict;

my $line = <STDIN>;
print "You wrote: $line";


and execute it from the command line:

mylogin$  ./mycat.pl

You'll notice that the program "hangs" rather that printing and then
returning. This means it is waiting for some user input. Type
something and then press enter and see what happens.

mylogin$  ./mycat.pl
Let me add some input
You wrote: Let me add some input
mylogin$

           -----------------------------------------

2) Chomp

This is a command that is worth learning early as it is so useful
later on. Chomp can be performed on both scalar variables (ones starting
with $ signs) and lists. When used, chomp removes any trailing string
that corresponds to the current value of $/ (also known as the
INPUT_RECORD_SEPARATOR which you can read more about in "perldoc
perlvar"). Usually $/ is set to newline, but we can change it if we
want to.

Chomp is often used to remove the trailing charadcter from input you've
just read in. This is best demonstrated with an example:

Type the following into a file called mycat.pl

                #!/usr/bin/perl -w
                use strict;

                my $line = <STDIN>;
                print "You wrote: [$line]\n";


and execute it from the command line, you should get something like:

                mylogin$  ./mycat.pl
                Let me add some input
                You wrote: [Let me add some input
		]
                mylogin$


As you can see when we read in the line, we read in the newline as well. We
can remove this newline by using chomp:

Change your program so that it reads as follows:

                #!/usr/bin/perl -w
                use strict;

                my $line = <STDIN>;
		chomp $line;
                print "You wrote: [$line]\n";


and execute it from the command line, this time you should get:

                mylogin$  ./mycat.pl
                Let me add some input
                You wrote: [Let me add some input]
                mylogin$


"chomp" is also called the "safe chop"

chop will remove any single final character.
chomp will remove only a new line character (i.e. the enter from
the keyboard).

Also, this is the common usage of chomp when it is combined with
accepting the input in a single line of code:

    chomp(my $input = <STDIN>);


           -----------------------------------------
3) Exercises

a) Try adding the following line to your program, before my $line = <STDIN=
>;

$/ ",";

What happens now?  Try entering a line without a comma in it followed by one
with a comma in it but not as the last character.  Can you explain what is
happening?

b) Write a currency conversion program to calculate the exchange value of 
100GBP in your
local currency (or if you are in the UK, use the Euro exchange
rate). your program should ask for the exchange rate.

Answers: All missing answers will be sorted out next time and also
published via the website.

           -----------------------------------------

4) Where to find out more...

An archive of lessons is being created at

http://www.schiffwood.demon.co.uk/index.html

Here there are links to useful websites and other books.

Part 4 of the course will follow on 26th April 2003
          -----------------------------------------

Note: The author uses Perl version 5.6.1. All exercises should therefore 
work on later versions. If you wish to find out which version of Perl you are
running type "perl -v" into your command line.

If you have any comments regarding this Perl lesson, suggestions for future 
topics, useful website addresses, useful reference material or you just want 
to say what you thought, please send your email to 

alice at schiffwood.co.uk

I will be pleased to hear from you.

Credits and acknowledgements

Sources of material include:
Learning Perl, Randal Schwartx & Tom Phoenix,
Perl Training Australia (http://www.perltraining.com.au)
Perl 5 Developer's guide, Peschko & DeWolfe
http://learn.perl.org
With very great thanks to Jacinta Richardson and Meryll Larkin for the
course help, and Trish Fraser for the webpages and consumer testing.
-- 
Alice Wood
Project Gutenberg newsletter editor


More information about the Courses mailing list