Perl Programming/User input-output
Input/output, or IO, is an all-encompassing term that describes the way your program interacts with the user. IO comes in two forms, or stream types: the program's stimuli are collectively referred to as input, while the medium that the program uses to communicate back, write logs, play sounds, etc. is known as output. Both types of streams can be redirected either at a lower level than Perl, as is the case when done through the operating system by the shell; or, in Perl itself, as is the case when you reopen the file handles associated with the stream.
Output
[edit | edit source]You have already learned how to output with the print statement. A simple reference is provided:
print "Hello World";
What this print statement is actually doing is printing to STDOUT, which stands for standard output. Standard output is the default destination for all output. If you wish to print anywhere else you must be explicit. We will revisit this later.
Input
[edit | edit source]As you may have imagined, it's very hard to write a good program without any type of input; here is an example program to teach you these concepts:
#!/usr/bin/perl
use strict;
use warnings;
print "What is your name?\n";
## Get the users $name from Standard In
my $name = <STDIN>;
print "Your name is $name\n";
Standard input is usually the keyboard though this can be changed at a lower level than your program. For now we will assume it isn't changed. However, this might not be an assumption you wish to make in production code.
Unit exercise
[edit | edit source]- Write a program that prompts the user for a number and then returns the number multiplied by four (or any other number).