PerlWiz - Keyboard Input

Click here for the documentation home page
Contents Prior Page Next Page

On this page, we will look at how PerlWiz accepts keyboard input into the IDE without the user having direct access to the console.

Many Perl IDEs fall short in this area - a shame, as this inhibits many beginners trying things out interactively with Perl when first learning.

Perl is a console-based language, which means it is design principally to work in a text environment. This means that many of its tools are also designed to work in a text environment - the debugger for example.

When creating Perl programs from a graphically-based IDE, the link to the console is usually abstracted - i.e. hidden - from the end user, or shown to the user "warts and all" - both approaches can be difficult to grasp.

PerlWiz takes a middle-road - there is no direct access to the console, but input is accepted from the keyboard in a controlled way.

Write a program to accept standard input

Let's create a program to take input from the keyboard. To do this, we need to take data from the standard input file stream - referred to as STDIN. This is how the console accepts data from the keyboard.

Perl represents this as <STDIN> or just <>.

So, to create a variable called $name and populate it from the keyboard, we would need only one line of code:-

$name = <STDIN>;


$name = <>;

Try typing in the following program - it simply prompts for a name, waits for a response and then outputs Hello there name.


print "What is your name? ";

$name = <>;

print "\nHello there $name - how are you?\n";

Run the program and type in data

When you have finished, execute the program by pressing clicking on the Text Output tab.

A message will appear asking What is your name?

Type out your name. This will appear in the output window in highlighted text. You can use the backspace button to delete the last typed letter.

When you press the Enter key, the line of text will be sent to the Perl program as a line of input, and the input will disappear from the display (it is not part of the output, after all!)

The data is now stored in the $name variable.

To display the result, we start a new line of text, and output the text Hello there $name - how are you? followed by another new line, where Perl will substitute the contents of the $name variable into the text.

Therefore, if I typed Simon then I would expect Hello there Simon - how are you? to be displayed.

In fact, the following displays:

Hello there Simon
 - how are you?

which is a bit frustrating. Why does it do this?

This isn't actually a limitation of PerlWiz, but rather of how Perl understands STDIN input. It takes the whole of what the user types in, including the press of the Enter key at the end - hence why the line breaks after the name, as the name actually ends with the line break from the original input.

Using chomp

Luckily, Perl has a very easy way of dealing with this - we use the chomp command followed by the variable in question to remove the last character from the end of the string. Let's add that to our listing, which now becomes:-


print "What is your name? ";

$name = <STDIN>;

chomp $name;

print "\nHello there $name - how are you?\n";

When you make a change to a program, PerlWiz picks up that you will need to execute the program again, so you just have to click on the Text Output tab again to execute the program.

Type your name in again. This time you should get an output something like this:-

Hello there Simon - how are you?

which is what we were after.

If you wanted to execute the program again, either press F9, click on the button on the toolbar, or right-click over the Text Output tab - they all have the same effect.

One final point on this program. You can combine the input from STDIN and chomp commands as follows, if you prefer a smaller program:-  

chomp ($name = <STDIN>);

Doing a calculation

Let's try another program to convert from Celsius/centigrade temperature to Fahrenheit. Click on the button on the toolbar to create a new temporary file (or just type over the old program if you don't wish to keep it).


print "What is the value in deg c? ";

$c = <>; chomp $c;

$f = ($c * 1.8) + 32;

print "\n$c Celsius in Fahrenheit is $f\n";

When you execute this program, type in a number - e.g. 20 degrees Celsius - and you will be given that temperature in Fahrenheit - e.g. 68 degrees Fahrenheit. Yippee!!

Debugging Tool

You can also use this as a debugging tool. Just say you wanted to simulate data coming in from another source, you could put an input from <STDIN> and type out the simulated text at the keyboard, and watch to see what the result is of doing this. Just remember to take the test out again before you start using the program out of a test environment! 


(c) Copyright 2003-4 Arctan Computer Ventures Ltd.   All Rights Reserved.
If you have any issues regarding this on-line help, please contact the author by clicking here.
Alternatively, you can leave a voice message on +44(0)7050-618-297 or fax on +44(0)7050-618-298

This Page was last updated: 22 February 2004 11:21