Perl Programming   «Prev  Next»
Lesson 9Working with a test bed
ObjectiveCreate a test bed to write Perl code.

Working with test bed in Perl

The speed and effectiveness of your learning experience will be greatly enhanced by your willingness to experiment freely with the examples discussed throughout the course. In fact, even after you have finished this course, you will be much more effective if you continue the practice of experimenting with small chunks of code to try out new ideas in Perl (or in any language for that matter).
To this end, I keep a directory called perlwork on my system, within which resides a file called test.pl, where I test my working theories (and where I tested all the code in this course). The skeleton of that file is simply this:

a piece of equipment used for testing new machinery, especially aircraft engines.
"fan blades were subjected to fatigue tests on laboratory test beds"
#!/usr/bin/perl -w

Before you create your own, there are two items with which you need to familiarize yourself:

1) Working with Perl test bed

As you go through the process of experimenting, you will often need to interact with the console, also known as the area for text output on your screen.
In Perl, the easy way to get input from the console is like this:

$line = <>; 

The empty <>in Perl
The empty <> is a shortcut in Perl that takes input from the files listed on the command line, or if there aren't any, from the standard input stream. And the easy way to send output to the console is like this:

print $line; 

Example 1.9
#!/usr/bin/perl -w
# Example 1.9 - demo stdio output buffering
$| = (@ARGV > 0); # command buffered if arguments given
print "Now you don't see it...";
sleep 2;
print "now you do\n";

If you call this program with no arguments, STDOUT is not command buffered. Your terminal (console, window, telnet session, whatever) doesn't receive output until the entire line is completed, so you see nothing for two seconds and then get the full line "Now you don't see it, now you do". If you call the program with at least one argument, STDOUT is command buffered. That means you first see "Now you don't see it...", and then after two seconds you finally see "now you do".
The dubious quest for increasingly compact code has led programmers to use the return value of select, the filehandle that was currently selected, as part of the second select:
select((select(OUTPUT_HANDLE), $| = 1)[0]);

There is another way. The FileHandle and IO modules provide a class method called autoflush. Call it with true or false values (the default value is true) to control autoflushing on a particular output handle:
use FileHandle;
STDERR->autoflush; # already unbuffered in stdio
$filehandle->autoflush(0);

2) Perl chomp Function

When you read text from a file, the newline (\n in Unix, \r\n in Windows/DOS, \r in Mac OS) remains attached to the end of each line. In many circumstances, it is necessary to remove the newline.
The chomp function can be used to remove the newline from the variable .
For example,

 #!/usr/bin/perl -w
print "prompt: ";
$line = <>;
print "The input is: $line";
When you run that program, the newline is still attached to the end of the line, so there is no need to add another one.
This example uses chomp:
#!/usr/bin/perl -w
print "prompt: ";
$line = <>;
chomp $line;
print "The input is: $line\n";

After using chomp on the $line variable, the line ending is no longer at the end of the $line variable, so we use the \n with print to end the current line of text.

Perl Testbed - Exercise

Click the Exercise link below to create your own test bed and run a simple script to get input from a user and print it.
Perl Testbed - Exercise

Perl Complete Reference