Perl - Part 8

[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]

Perl Input and Output

Perl has the ability to read files from disk as easy, if not easier than any other known programming language. However, what you've seen of keyboard input thus far has been somewhat cumbersome. You had to use an external module to get your input by ReadLine, and in the case of textual input, remove the EOL (end of line or \n character) generated by pressing enter. There is an easier way. But first some theory...

I/O Devices

Modern languages like C, C++, Java, Perl etc. recognise that there is normally a keyboard and screen attached to a computer; or at the very least some type of input and output device. They also recognise that there is a need to be able to log errors. To this end, these languages recognise the existance of three standard I/O devices:

  1. <STDIN> - the standard input device; normally a keyboard, once upon a time a card-reading machine but just as possibly electrified Bongo Drums.
  2. <STDOUT> - the standard output device; normally a screen, once a tele-printer but if you happened to have a spare elephant your could wire him in as an audio-prompt.
  3. <STDERR> - the standard logging mechanism whereby all system events are recorded; today under most *NIX systems this is a file called messages in the /var/log directory. In the middle ages it was normally a tonsured monk who would write down the errors of log events generated by the computer; monks were used as they tended to be literate while most of the population were not.

Output

Unless you specify otherwise it is assumed that output is destined for the screen. Hence the ease with which you have been able to just print directly to the screen. If you want your output to go elsewhere, a few other (simple) steps must be followed. We'll come to samples of these soon enough. In the meantime you do not need to specify <STDOUT> as the destination of your output.

'Error' Logging

Stangely enough, a positive outcome is considered an error - but a good error not a bad one! If you want events - good or bad - recorded in the systems event log so that you can refer to them after the program ends, direct them to <STDERR>. In light of the fact that many programs wind up only logging positive outcomes the device name <STDLOG> may have been a better choice instead of what we are using.

Input

Input is assumed to come from the keyboard. The command used to do it is the only question. When we wrote the Hello World program we were demonstrating that we could generate output, as well as run a program successfully. The next challenge for a programmer seeing a new system or a new language is how to take input from the keyboard. Luckily <STDIN> is pre-declared and we only have to use it. This does away with the cumbersome ReadLine method

Furthermore, Perl has many, many functions to manipulate text; including the chop command. So instead of the powerful but seemly difficult s/// mechanism the chop function just chops off the EOL character. Snip. Done!

Task 004

Copy and modify your greetme.pl file to look as below. Use the command:
cp greetme.pl greetmegood.pl
to copy the file then use:
vi greetmegood.pl
to open and edit the new file....so that your program looks like this:

#!/usr/bin/perl
  
  print "Hello. Who are you?\n";
  $name = <STDIN>;
  chop $name;
  print "Hello, $name.\n";

You should now have a program that says 'Hello' back to you with your name and all output properly positioned but the program should be much simpler than the original one..

Making Decisions: The if Operator & Statement

The programs we have written so far all used a 'straight path' from start to finish. We could predict what the program could do starting from the first line. In reality, this isn't normally the case with a computer program. Normally, a program will have to respond in a manner appropriate to the input - which will vary.

Thus far, we asked a name and said hello to that name. The following program will also ask a name, but also an age. It will repond to the age given by informing the user if they may vote or not. For this, the program needs to be able to make a decision; hence, the if function or statement.

Task 005

Try entering the program below putting your own details in the comment section:


#!/usr/bin/perl
#
# ----------------------------------------------------------------------------------
# Author: Date:
# Title: responder.pl Purpose:
# ----------------------------------------------------------------------------------
#
  
  print "Hello. Who are you? ";
  $name = <STDIN>;
  chomp $name;
  print "How old are you? ";
  $age = <STDIN>;
  if ($age ge 18)
  {
    chomp $age;
    print "Hello, $name. Did you know that at $age you're old enough to vote?\n";
  }

Sample output from the program is shown here:


There are a few things to note:

  1. What happens if a value < 18 is entered?
  2. Why do we not need to chop the line break when making the comparison in the if but we do before using $age in an output statement; modify the program and re-run if you're not sure.
  3. The if function (in context, the whole thing is called an if statement) compares two values. If the comaprision has a TRUE outcome the code immediately after will be executed; otherwise - with a FALSE outcome - some other or no other code will be executed.
  4. Note how the code to be executed in the case of a TRUE outcome is grouped into a block with { and } brackets. This is important.
  5. Note the ge operator - which stands for greater than or equal. There are other operators: lt, le, eq, gt, ne which should be easy to understand. The tradtional <, = and > symbols are used for numeric comparison - but ask yourself why the text comparator was used here...?
  6. Finally, note how no response is made it the user is not entitled to vote. This is poor programming practice and will be solved in the next task

More anon...

[home] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]

Last updated: 20131015-16:45