[Courses] [Perl] Part 3: User Input and "chomp"

Dan dan at cellectivity.com
Sat Apr 16 00:04:01 EST 2005


LinuxChix Perl Course Part 3: User Input and "chomp"

1) Introduction
2) The Line Input Operator
3) The chomp Function
4) Exercises
5) Answers to Previous Exercises
6) Acknowledgements
7) Licensing


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

1) Introduction

Try executing the following program:


  #!/usr/bin/perl -w
  use strict;
  
  my $line = <STDIN>;
  print "You wrote: $line";


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

  you at localhost$  ./the-program.pl
  Let me add some input
  You wrote: Let me add some input
  you at localhost$

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

2) The Line Input Operator

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

  $line = <STDIN>;

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

3) The chomp Function

Let's change the introductory program a little bit:

  #!/usr/bin/perl -w
  use strict;
  
  my $line = <STDIN>;
  print "You wrote: {$line}\n";

Now when we execute it, we see this:

  you at localhost$  ./the-program.pl
  Let me add some input
  You wrote: {Let me add some input
  }
  you at localhost$

As you can see, the line input operator includes the newline at the
end of the input. There are lots of ways to remove that newline, but
the best way is to use the "chomp" function:

  #!/usr/bin/perl -w
  use strict;
  
  my $line = <STDIN>;
  chomp($line);
  print "You wrote: {$line}\n";

Note that "chomp" doesn't return the result; it changes whatever is
passed in. That means we could have combined two lines in the above
program:

  chomp(my $line = <STDIN>);

But never do this:

  $line = chomp($line);    # No!

One final note: "chomp" was introduced as a replacement for "chop",
which removes the last character from a string no matter what it is.
"chomp" only removes a line input separator (the newline).

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

4) Exercises

a) Write a currency conversion program to convert between dollars and
euros.

b) To make the previous exercise more interesting, have the program
ask for the exchange rate.

c) Try running the following program:

  #!/usr/bin/perl -w
  use strict;
  
  local $/ = ",";
  my $line = <STDIN>;
  print "$line\n";

What happens when you run it?  Try entering a line without a comma
followed by one with a comma. Can you explain what is happening? What
effect does this have on the "chomp" function?

If you need help, try reading "perldoc perlvar":

  $ perldoc perlvar

and looking under "INPUT_RECORD_SEPARATOR"

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

5) Answers to Previous Exercises

a) This program computes the average of five numbers.

  #!/usr/bin/perl -w
  use strict;
  
  my $sum = 23 + 28 + 31 + 17 + 1;
  my $average = $sum/5;
  
  print "The average is: $average\n";

b) The "x" operator repeats a string.

The number of repeats must always be the second parameter. As Karine
put it so well, "Perl has to know which is the string and which is
the "multiplicator". In that example it could guess it, but what
about 4 x 3? 4 x 3 outputs '444', but 3 x 4 outputs '3333'." So
operands need to be ordered.

As a contrast, the Python language allows you to "multiply" strings
by integers using the "*" operator. But because Python has a clear
separation between text and numbers, it accepts operands in either
order.

c) The "q" operator is simply an alias for the single quote. The only
advantage to using it is that it makes it easier to write strings
which include single quotes, because we can choose another character
to delimit the string. The "qq" operator is an alias for the double
quote.

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

6) Acknowledgements

A big thank you to Jacinta Richardson for suggestions and
proofreading. More advanced Perl users might want to check out the
free material from Perl Training Australia
<http://www.perltraining.com.au/>, which she is a part of.

Other contributors include Meryll Larkin.

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

7) Licensing

This course (i.e., all parts of it) is copyright 2003-2005 by Dan
Richter and Alice Wood, and is released under the same license as
Perl itself (Artistic License or GPL, your choice). This is the
license of choice to make it easy for other people to integrate your
Perl code/documentation into their own projects. It is not generally
used in projects unrelated to Perl.




More information about the Courses mailing list