Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

STDIN for Input in Perl

In Perl, standard input (often abbreviated as STDIN) is a predefined input stream that provides a way to read data from the console (or from a piped source). This tutorial covers how to use STDIN in Perl to obtain input from the user or another program.

1. Basic Input Using STDIN

The simplest way to read from STDIN is using the <> operator. This reads one line from STDIN.

print "Please enter your name: ";
my $name = <STDIN>;
print "Hello, $name";

When this script is run, it will wait for the user to enter their name and then greet them.

2. Chomping Input

After reading from STDIN, it's common to have a newline character (\n) at the end of the input. You can remove it using the chomp function:

print "Please enter your age: ";
my $age = <STDIN>;
chomp($age);
print "In ten years, you will be ", $age + 10, " years old.\n";

3. Reading Multiple Lines

You can read multiple lines of input from STDIN using a loop:

print "Enter several lines (Ctrl-D to end):\n";
my @lines = <STDIN>;

print "\nYou entered:\n", @lines;

The above will continue reading input until you signal an end-of-file with Ctrl-D (or Ctrl-Z on Windows).

4. Reading Single Characters

To read a single character from STDIN, you can use the getc function:

print "Press a key: ";
my $char = getc(STDIN);
print "You pressed: $char\n";

5. Using STDIN in List Context

In a list context, reading from STDIN will read all remaining lines:

print "Enter several lines:\n";
my @all_lines = <STDIN>;
print "You entered ", scalar @all_lines, " lines.\n";

6. Piping Input to Perl

You can also use STDIN to read input that is piped from another program. For example:

echo -e "apple\nbanana\ncherry" | perl script.pl

If script.pl contains:

while (my $fruit = <STDIN>) {
    print "You piped in: $fruit";
}

The script will print each fruit that was echoed into it.

7. Prompting for Passwords

For security reasons, if you're reading a password or other sensitive information, it's a good idea not to display the typed characters. The Term::ReadKey module, which isn't a core module but is commonly used, can assist with this:

use Term::ReadKey;

print "Enter your password: ";
ReadMode('noecho');  # Don't display characters
my $password = ReadLine(0);  # Read from STDIN
ReadMode(0);  # Reset input mode
chomp($password);

print "\nThank you.\n";

8. Summary

Using STDIN in Perl provides a way to interactively receive input from the user or get data from other programs. Combined with functions like chomp, getc, and modules like Term::ReadKey, Perl offers robust ways to read and handle input effectively.

  1. Reading user input with STDIN in Perl:

    • Description: Reading a single line of user input from the standard input.
    • Code Example:
      print "Enter your name: ";
      my $name = <STDIN>;
      chomp($name);
      print "Hello, $name!\n";
      
  2. Getting input from the command line in Perl:

    • Description: Accepting command-line arguments as input.
    • Code Example:
      my $arg1 = $ARGV[0];
      my $arg2 = $ARGV[1];
      print "Arguments: $arg1, $arg2\n";
      
  3. Perl STDIN and chomp function:

    • Description: Using chomp to remove the newline character from user input.
    • Code Example:
      print "Enter a number: ";
      my $number = <STDIN>;
      chomp($number);
      
  4. Handling user input with STDIN in Perl scripts:

    • Description: Properly handling and processing user input.
    • Code Example:
      print "Enter your age: ";
      my $age = <STDIN>;
      chomp($age);
      
      if ($age =~ /^\d+$/) {
          print "Valid age: $age\n";
      } else {
          print "Invalid input. Please enter a number.\n";
      }
      
  5. Input validation using STDIN in Perl:

    • Description: Validating user input to ensure it meets specific criteria.
    • Code Example:
      print "Enter a positive number: ";
      my $input = <STDIN>;
      chomp($input);
      
      if ($input =~ /^\d+$/ && $input > 0) {
          print "Valid input: $input\n";
      } else {
          print "Invalid input. Please enter a positive number.\n";
      }
      
  6. Interactive console input in Perl:

    • Description: Creating an interactive console input experience.
    • Code Example:
      print "What is your favorite color? ";
      my $color = <STDIN>;
      chomp($color);
      print "You like $color! Great choice.\n";
      
  7. Reading multiple lines from STDIN in Perl:

    • Description: Reading and processing multiple lines of user input.
    • Code Example:
      print "Enter a poem (Ctrl-D to end):\n";
      my @poem_lines = <STDIN>;
      chomp(@poem_lines);
      
  8. STDIN as an array in Perl:

    • Description: Treating STDIN as an array to process multiple lines.
    • Code Example:
      print "Enter three names (one per line):\n";
      my @names = <STDIN>;
      chomp(@names);
      
      print "You entered: @names\n";