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 in Scalar and List Context in Perl

In Perl, context is crucial for understanding how various functions and operations behave. Two of the main contexts in Perl are scalar context and list context. The behavior of STDIN is influenced by these contexts. Let's explore this in more detail.

1. Introduction

In Perl, STDIN is a global filehandle used to get input from the standard input, typically the keyboard or redirected file input. How you use STDIN can differ depending on whether you're in scalar or list context.

2. STDIN in Scalar Context

In scalar context, reading from STDIN returns a single line of input:

print "Enter your name: ";
my $name = <STDIN>;
chomp $name;  # Remove the trailing newline
print "Hello, $name!\n";

In the above, <STDIN> reads just one line of input from the user.

3. STDIN in List Context

In list context, reading from STDIN returns all the remaining lines from the input until the end-of-file (EOF) is reached:

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

Here, Perl will keep reading lines until you signal EOF. On UNIX-like systems, EOF is signaled by pressing Ctrl-D, while on Windows, it's Ctrl-Z.

4. Diamond Operator

The diamond operator (<>) is similar to <STDIN>, but it has some special behavior. By default, it reads from STDIN, but if command-line arguments are provided, it treats them as filenames and reads from those files:

# Reads from files listed in @ARGV, or STDIN if no files provided
while (my $line = <>) {
    print $line;
}

5. Chomping Input

When reading from STDIN (or any filehandle), Perl retains the newline character (\n). To remove it, use the chomp function:

my $input = <STDIN>;
chomp $input;  # Removes trailing newline

When dealing with list context, you can chomp all lines at once:

my @inputs = <STDIN>;
chomp @inputs;

6. Context Matters

Remember that context is vital in Perl. A function or operation can behave differently based on its surrounding context:

my $single = <STDIN>;  # Reads one line (scalar context)

my @many = <STDIN>;    # Reads all lines until EOF (list context)

7. Summary

  • STDIN is the standard input filehandle in Perl.
  • In scalar context, <STDIN> reads one line from the input.
  • In list context, <STDIN> reads all lines until EOF.
  • The diamond operator (<>) reads from STDIN or files provided in @ARGV.
  • Always remember to chomp your input to remove trailing newlines.
  • Context (scalar vs. list) is a crucial concept in Perl and determines the behavior of many operations and functions.
  1. Reading from STDIN in scalar context in Perl:

    • Description: Reading a single line from STDIN in scalar context.
    • Code Example:
      print "Enter your name: ";
      my $name = <STDIN>;
      chomp $name;
      print "Hello, $name!\n";
      
  2. Using <STDIN> in list context in Perl:

    • Description: Reading multiple lines from STDIN in list context.
    • Code Example:
      print "Enter three lines:\n";
      my @lines = <STDIN>;
      print "You entered:\n@lines";
      
  3. Scalar vs. list context with STDIN in Perl:

    • Description: Demonstrating scalar and list context when reading from STDIN.
    • Code Example:
      print "Enter a number: ";
      my $num_scalar = <STDIN>;    # Scalar context
      chomp $num_scalar;
      
      print "Enter three lines:\n";
      my @lines_list = <STDIN>;    # List context
      
      print "Number (Scalar): $num_scalar\n";
      print "Lines (List): @lines_list";
      
  4. Perl reading multiple lines from STDIN:

    • Description: Reading and processing multiple lines from STDIN.
    • Code Example:
      print "Enter lines (Ctrl-D to end input):\n";
      my @input_lines = <STDIN>;
      chomp @input_lines;
      
      foreach my $line (@input_lines) {
          print "Processed: $line\n";
      }
      
  5. Processing user input from STDIN in Perl:

    • Description: Prompting the user for input and processing it.
    • Code Example:
      print "Enter your age: ";
      my $age = <STDIN>;
      chomp $age;
      
      if ($age =~ /^\d+$/) {
          print "Valid age: $age\n";
      } else {
          print "Invalid input for age.\n";
      }
      
  6. STDIN as an array in Perl:

    • Description: Treating STDIN as an array and processing input.
    • Code Example:
      print "Enter lines (Ctrl-D to end input):\n";
      my @lines = <STDIN>;
      chomp @lines;
      
      foreach my $line (@lines) {
          print "Processed: $line\n";
      }
      
  7. Iterating over STDIN in Perl list context:

    • Description: Iterating over STDIN in list context using a loop.
    • Code Example:
      print "Enter lines (Ctrl-D to end input):\n";
      while (<STDIN>) {
          chomp;
          print "Processed: $_\n";
      }
      
  8. Chomping newlines from input in Perl STDIN:

    • Description: Removing newlines from input using chomp when reading from STDIN.
    • Code Example:
      print "Enter a string: ";
      my $input = <STDIN>;
      chomp $input;
      
      print "Processed: $input\n";