Working with Input and Outputs in Perl

  • Input from Standard Input

o Common way to input from standard input:

chomp($line = <STDIN>);
  • Reading continuous input would look something like:
while ( chomp($line = <STDIN>) ) {...}
 # or
 while (defined($_ = <STDIN>)) { ... }
  • The Diamond Operator < >

o Useful for making programs that work like standard Unix utilities, with respect to invocation arguments. Makes it useful with utilities like cat, sed, awk, sort, grep, lpr, etc.

o Invocation arguments to a program are the number of “words” on the command line after the name of the program.

o For example, executing the following from the prompt:

./myprogram.pl filename1.txt filename2.txt
    • The myprogram.pl file needs reading the parameter files. This can be done like so:
while(defined ($line = <>)) {

chomp ($line);

print "reading line $line ";
 }

# Another easier way to write the above is:
 while (<>) {
 chomp;
 print "reading line $_ ";
 }
  • The @ARGV array

o Special array preset by Perl as a list of invocation arguments

o The Diamond Operator discussed above by default reads from this array. If wanted, we can change this array value to make the diamond operator read something else:

@ARGV = qw ( john joe jim jack );
 while (<>) { ... }
  • The print function

o print is a function – can be called with or without parentheses for its arguments to print

o Like any other function, print has a return value, which is boolean, true for success print

o When printing arrays, if the array is captured within a string (using quotations) then that array is interpolated and spaces are added.

@array = qw ( john joe jack ); # array defined
 print @array; # prints johnjoejack
 print "@array"; # prints john joe jack - the array is interpolated
  • The printf function

o Like the C language, Perl adopted the printf function that uses variable substitution / conversion

o Conversions are marked by %, the printf statement needs to be followed by the variable values

%g = automatic – floating point, integer or exponential

%d = decimal
 %s = string
 %f = floating point but rounded
 %% = prints a real percent sign
 printf "%g %g ", 5/2, 51 ** 17; # 2.5 1.0683e+29
 printf "in $d days! " , 17.85; # in 17 days!
 printf "%6d", 42; # "____42" à 6 total digit spaces
 printf "%2d ", 2e3 + 1.9; # 2001
 printf "%10s", "Wilma"; # " Wilma" à 10 total char spaces
 printf "%-15s", "flintstone"; # "flintstone " à 15 total char spaces appended
 printf "%12.3f", 6 * 7 + 2/3; # " 42.667" à 12 spaces
 printf "rate of %.2f%%", .44; # "0.44%"
  • Filehandles – A File handler

o Filehandles keywords:

  • STDIN
  • STDOUT
  • STDERR
  • DATA
  • ARGV
  • ARGVOUT

o Ways to open files:

  • open FILE, “filename”;
  • open FILE, “<filename”; # explicitly stating that this file in coming in as INPUT
  • open FILE, “>filename”; # an output file
  • open FILE, “>> filename”; # an output file but will append to the existing file

o Always remember to close the file after completion, but if you forget, its ok. Perl will automatically close file handles when the handle name is reused (FILE) or when the program exits. Otherwise, close it for read/write access.

  • close FILE;
  • The die function

o Use the $! variable which is the system level message as to why something failed

o The die function will also automatically append program name and line number to the end of the error message

if ( ! open FILE, ">> filename") {
 die "Cannot create file: $!";
 }
  • Using File Handles

o Common way to open files:

if ( ! open FILE, "filename") {
 die "Error Message ($!)";
 }
 while (<FILE>) {
 chomp;
 ...
 }
  • The warn function

o Works just like die, but doesn’t exit the program

  • Can change the default output file handler (used by print and select function)

o This can cause danger so use cautiously

o Remember to reset the file handle!

select FILE;
 print "Example line here"; # This line is printed into the FILE
 select STDOUT;
 print "Another example line here"; # This line is printed to the screen
  • The say function

o Since Perl 5.10, ported from the ongoing development of Perl 6

o It works exactly like print, except it automatically adds a newline at the end

print “Hello World!\n”;

say “Hello World!”;