15
Perl Language Day 3

Introduction to Perl Day3

Embed Size (px)

DESCRIPTION

Perl slide 3 introduction

Citation preview

Page 1: Introduction to Perl Day3

Perl Language

Day 3

Page 2: Introduction to Perl Day3

Passing Arguments To Your Program

Page 3: Introduction to Perl Day3

Command Line Arguments

Command line arguments in Perl are extremely easy. @ARGV is the array that holds all arguments passed

in from the command line. Example:

./prog.pl arg1 arg2 arg3 @ARGV would contain ('arg1', ‘arg2', 'arg3’)

$#ARGV returns the number of command line arguments that have been passed. Remember $#array is the size of the array!

Page 4: Introduction to Perl Day3

Reading/Writing Files

Page 5: Introduction to Perl Day3

File Handlers

Opening a File:open (SRC, “my_file.txt”);

Reading from a File$line = <SRC>; # reads upto a newline character

Closing a Fileclose (SRC);

Page 6: Introduction to Perl Day3

File Handlers cont...

Opening a file for output:open (DST, “>my_file.txt”);

Opening a file for appendingopen (DST, “>>my_file.txt”);

Writing to a file:print DST “Printing my first line.\n”;

Safeguarding against opening a non existent fileopen (SRC, “file.txt”) || die “Could not open file.\n”;

Page 7: Introduction to Perl Day3

File Test Operators Check to see if a file exists:

if ( -e “file.txt”) { # The file exists!}

Other file test operators:-r readable-x executable-d is a directory-T is a text file

Page 8: Introduction to Perl Day3

Quick Program with File Handles

Program to copy a file to a destination file

#!/usr/bin/perl -wopen(SRC, “file.txt”) || die “Could not open source file.\n”;open(DST, “>newfile.txt”);while ( $line = <SRC> )

{ print DST $line;

}close SRC;close DST;

Page 9: Introduction to Perl Day3

Some Default File Handles STDIN : Standard Input

$line = <STDIN>; # takes input from stdin

STDOUT : Standard outputprint STDOUT “File handling in Perl is sweet!\n”;

STDERR : Standard Errorprint STDERR “Error!!\n”;

Page 10: Introduction to Perl Day3

The <> File Handle

The “empty” file handle takes the command line file(s) or STDIN; $line = <>;

If program is run ./prog.pl file.txt, this will automatically open file.txt and read the first line.

If program is run ./prog.pl file1.txt file2.txt, this will first read in file1.txt and then file2.txt ... you will not know when one ends and the other begins.

Page 11: Introduction to Perl Day3

The <> File Handle cont...

If program is run ./prog.pl, the program will wait for you to enter text at the prompt, and will continue until you enter the EOF character

CTRL-D in UNIX

Page 12: Introduction to Perl Day3

Example Program with STDIN

Suppose you want to determine if you are one of the three stooges

#!/usr/local/bin/perl%stooges = (larry => 1, moe => 1, curly => 1 );print “Enter your name: ? “; $name = <STDIN>; chomp $name;if($stooges{ lc($name) }) { print “You are one of the Three Stooges!!\n”;} else { print “Sorry, you are not a Stooge!!\n”;}

Page 13: Introduction to Perl Day3

Combining File ContentGiven The two Following Files:

File1.txt123

AndFile2.txt

abc

Write a program that takes the two files as arguments and outputs a third file that looks like:

File3.txt1a2b3

Tip: ./mix_files File1.txt File2.txt File3.txt

Page 14: Introduction to Perl Day3

Combining File Content

#! /usr/bin/perlopen (F, “$ARGV[0]);open (G, “$ARGV[1]);open (H, “>$ARGV[2]);while ( defined (F) && defined (G) && ($l1=<F>) && ($l2=<G>))

{print H “$l1$l2”;

}close (F); close (G); close (H);

Page 15: Introduction to Perl Day3

Chomp and Chop

Chomp : function that deletes a trailing newline from the end of a string.

$line = “this is the first line of text\n”; chomp $line; # removes the new line character print $line; # prints “this is the first line of #

text” without returning Chop : function that chops off the last character of a string.

$line = “this is the first line of text”; chop $line; print $line; #prints “this is the first line of tex”