#!/usr/bin/perl -w # Perl: example on how to read lines from standard input # Perl: also shows how to use Getopt::Long # # Perl: Comments containing "Perl:" are obvious to a Perl programmer # Perl: and would not normally appear in a production Perl program. # Perl: Remove these comments before you submit. # # - Ian! D. Allen idallen@idallen.ca use strict; # Perl: always run Perl with "use strict" ! use Getopt::Long; # for parsing arguments: see "man Getopt::Long" my $name; # place to store --name my $greeting; # place to store --greet GetOptions ( 'name=s' => \$name, # --name "some name" 'greet=s' => \$greeting, # --greet "some greeting" ); $name = $name || "Daffy Duck"; # set a default if not given $greeting = $greeting || "Hello"; # set a default if not given warn "$greeting $name:\n"; # Perl: We only print the prompt if STDIN is a terminal (not a file). # Perl: The "warn" prints on standard error (useful for errors and prompts). # Perl: Note the use of "-t STDIN" at the end of the warn() statement! # warn "Enter message, end with EOF (usually CTRL-D):\n" if -t STDIN; # Perl: The <...> operator stores each line read internally in variable $_ # Perl: You might want to collect lines into an array using push(). # while(){ chomp; # Perl: remove trailing newline from $_ my $len = length; # Perl: calculate length of $_ print "You entered ($len): '$_'\n"; } print "Read $. lines.\n"; # Perl: $. is the current line number