Perl - Part 4
[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]
Programming Component Continued
You will find it useful to be able to accept input into as well as generate output from your programs. Presently you are using the print command for output. We shall now create a program that uses just one of the many possible methods to accept input into your program.
Create a file called greetme.pl with this code in it:
#!/usr/bin/perl
use Term::ReadKey;
print "Hello. Who are you?\n";
$name = ReadLine;
print "Hello, $name.\n";
Note that we wish to use the ReadLine function for input. This is not part of the Perl language so we have to include the module that contains that function. This is the module called Term::ReadKey (note that the language is case-sensitive!) and explains why the second line in this program contains an instruction to use that module, thereby making it's functions available to us for use.
Run the program and see what happens. You should notice a minor flaw in the output as shown below:

As before, the issue of line breaks raises it's head, but not as expected. When we used the ReadLine function the program read in the whole line that we typed - including the carriage return after our name. So when the line is printed back out, the line break is printed too; hence the full stop on the next line. So we need to extract the newline character from the input, before generating our output.
Fortunately Perl is great at this type of manipulation. Sadly, it doesn't look pretty and causes unpleasantness in the life of 13 out of 10 cats whose owners are learning this method of manipulation. It's hugely powerful as a feature; which is pretty much the same opinion that a Great White Shark has about it's mouth.
Don't ask questions yet, just put in this line...
$name =~ s/\n//;
...so that your program looks like this:
#!/usr/bin/perl
use Term::ReadKey;
print "Hello. Who are you?\n";
$name = ReadLine;
$name =~ s/\n//;
print "Hello, $name.\n";
You should now have a program that says 'Hello' back to you with your name and all output properly positioned.>/p>
What was that all about?
Well, two things really:
- Text manipulation:
- Perl is superior when it comes to text manipulation. Put simply, the line $name =~ s/\n//; substituted (because of the letter s) all occurences of \n with a nothing (the contents of the second //).
- The general structure of the substitution is s/what to replace/what to replace it with/
- No. You're not alone. It does look messy and require concentration to get right. But you'll see the logic of the mechanism with practise.
- Language Extensibility:
- The use of the Term::ReadKey module shows we can add aspects and functions to the language. Many languages are created this way. If you buy a book on Perl, make sure as many modules as possible are documented. In any case, documentation and software are freely available from CPAN.
This completes the creation of a Perl program that requests and accepts input, and then generates output, confirming that we can create more interactive programs. It goes without saying that there was an easier way to do this but for now you've been introduced to some features of Perl. Read on for a program that manipulates input further......
[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: 20120108-16:57