Perl File I/O Software Tools. Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN.

Slides:



Advertisements
Similar presentations
● Perl reference
Advertisements

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Process Management Software Tools. Slide 2 system Perl programs can execute shell commands (Bourne shell) using the system function. system("date");
6a.1 More about files: Globbing. 6a.2 Open a file for reading, and link it to a filehandle: open(IN, "
Where to get Perl ? / Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter.
Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Perl Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
Perl File I/O Learning Objectives: 1. To understand the usage of file handler in Perl 2. To learn the file error handling / testing in Perl.
Perl File and Directory Access Software Tools. Slide 2 Changing Directories You can change the current working directory within Perl, just like the cd.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
5.1 Previously on... PERL course (let ’ s practice some more loops)
Chapter 5: Loops and Files.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
CS0007: Introduction to Computer Programming File IO and Recursion.
Working with Files Chapter 5. Display a Calendar Display a calendar for a specific month – cal Display a calendar for a specific year – cal 2000.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 12.
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT I/O Redirection.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 7 Dr. Jerry Shiao, Silicon Valley University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Course A201: Introduction to Programming 12/9/2010.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
3.1 Revision: variables & arrays Array declaration Array = ('A','B','C','D'); = = (); Array.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Operating Systems Process Creation
Manipulating Files Refresher. The touch Command touch is used to create a new, empty file. If the file already exists, touch updates the time and date.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Various 3. Noclobber 1 echo hello > output.txt But what if output is an inportant file and you do not want to make a mistake!!! set –o noclobber Now you.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.
Topic: File Input/Output (I/O)
Week 3 Redirection, Pipes, and Background
Input from STDIN STDIN, standard input, comes from the keyboard.
Ruth Anderson UW CSE 160 Winter 2016
Getting a filehandle Open for reading open(FILEHANDLE, FILENAME)
Files I/O, Streams, I/O Redirection, Reading with fscanf
Basic Input/Output Web Programming.
Pemrosesan Teks Herika Hayurani Sept 19, 2006
Perl I/O Learning Objectives:
CPS120: Introduction to Computer Science
Python I/O Peter Wad Sackett.
Perl Control Flow Learning Objectives:
Perl Process Management
Presentation transcript:

Perl File I/O Software Tools

Slide 2 Filehandles l A filehandle is the name for an I/O connection between your Perl program and the outside world. STDIN is a filehandle, representing the connection between Perl programs and the UNIX standard output. l Filehandles can also refer to external files. l Filehandles are usually ALL UPPERCASE.

Slide 3 Opening and Closing Filehandles To open a filehandle called INPUTFILE for reading the file named infile (assuming it is in the current working directory): open(INPUTFILE, "infile"); To close the filehandle INPUTFILE after finished reading: close(INPUTFILE);

Slide 4 Opening Filehandles To open a filehandle called OUTPUTFILE for writing the file outfile in the current working directory: open(OUTPUTFILE, ">outfile"); To open a filehandle called APPENDFILE for appending to the file outfile in the current working directory: open(APPENDFILE, ">>outfile");

Slide 5 Using Filehandles Once a filehandle is open for reading, you can read lines from it just as you can from STDIN. For example, to read lines from the file nameID : $ cat nameID BillGates 444 MrBill 888 $ cat read1 #!/usr/local/bin/perl5 -w open(INFILE, "nameID"); while( ){ chomp; print "The name and ID are: $_\n"; } close(INFILE); $ read1 The name and ID are: BillGates 444 The name and ID are: MrBill 888

Slide 6 Using Filehandles If you want to print to a filehandle, you must place the filehandle immediately after the print keyword and before the arguments (no comma): $ cat write1 #!/usr/local/bin/perl5 -w open(BILLFILE, ">bill"); print BILLFILE "Hi Bill!\n"; print BILLFILE "Bye Bill!\n"; close(BILLFILE); $ write1 $ cat bill Hi Bill! Bye Bill! $

Slide 7 Using Filehandles Do not get confused between STDOUT and output to a file: $ cat write2 #!/usr/local/bin/perl5 -w open(BILLFILE, ">bill"); print "Hi Bill!\n"; print STDOUT "Hi again Bill!\n"; print BILLFILE "Bye Bill!\n"; close(BILLFILE); $ write2 Hi Bill! Hi again Bill! $ cat bill Bye Bill! $

Slide 8 Using Filehandles How to copy data from one file into another: $ cat bill1 Hi Bill! $ cat copy1 #!/usr/local/bin/perl5 -w $a = "bill1"; $b = "bill2"; open(IN, $a); open(OUT, ">$b"); while( ){ print OUT $_; } close(IN); close(OUT); $ copy1 $ cat bill2 Hi Bill! $

Slide 9 Error Checking Perl allows you to check the result of open and report an error if something went wrong ( open returns false if there is a problem): $ cat bill cat: cannot open bill $ cat display1 #!/usr/local/bin/perl5 -w $file = "bill"; unless(open(IN, $file)){ print "Sorry could not open $file\n"; }else{ print "$file contains:\n"; while( ){ print; } close(IN); $ display1 Sorry could not open bill $

Slide 10 die die is a shortcut for error checking. die is like exit, but it prints an error message before stopping the program: $ cat bill cat: cannot open bill $ cat display2 #!/usr/local/bin/perl5 -w $file = "bill"; unless(open(IN, $file)){ die "Sorry could not open $file\n"; } print "$file contains:\n"; while( ){ print; } close(IN); $ display2 Sorry could not open bill $

Slide 11 die If you leave off the \n, die will also attach the line number where it died: $ cat bill cat: cannot open bill $ cat display3 #!/usr/local/bin/perl5 -w $file = "bill"; unless(open(IN, $file)){ die "Sorry could not open $file"; # no \n } print "$file contains:\n"; while( ){ print; } close(IN); $ display3 Sorry could not open bill at display3 line 4. $

Slide 12 die You can write it even shorter using || (“open that file or die!”): $ cat bill cat: cannot open bill $ cat display4 #!/usr/local/bin/perl5 -w $file = "bill"; open(IN, $file) || die "Sorry could not open $file"; print "$file contains:\n"; while( ){ print; } close(IN); $ display4 Sorry could not open bill at display4 line 3. $

Slide 13 die Another useful thing in die is the $! variable, which contains the error string from the operating system: $ cat bill cat: cannot open bill $ cat display5 #!/usr/local/bin/perl5 -w $file = "bill"; open(IN, $file) || die "Sorry could not open $file: $!\n"; print "$file contains:\n"; while( ){ print; } close(IN); $ display5 Sorry could not open bill: No such file or directory $

Slide 14 -x File Tests l Perl also allows you to test the properties of files. Some common file tests: -rFile or directory is readable -wFile or directory is writable -xFile or directory is executable -oFile or directory is owned by user -eFile or directory exists -zFile exists and has zero size (directories are never empty) -sFile exists and has nonzero size (returns size in bytes) -fEntry is a plain file -dEntry is a directory -lEntry is a symbolic link

Slide 15 -x File Tests l File tests allow you to avoid overwriting an existing file with the same name. $ cat bill Hi Bill! $ cat test1 #!/usr/local/bin/perl5 -w $file = "bill"; if(-e $file){ die "File already exists!\n"; } open(OUT, " >$file " ) || die "Could not open $file: $!\n"; print OUT "Bye Bill!\n"; close(OUT); $ test1 File already exists! $