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.

Slides:



Advertisements
Similar presentations
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Advertisements

● Perl reference
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
COMP234 Perl Printing Special Quotes File Handling.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
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.
Working with Files How to create, view, copy, rename and print files.
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, "
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 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)
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
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 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.
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 Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
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.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
CSE1301 Computer Programming: Lecture 19 File I/O
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.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 7 Dr. Jerry Shiao, Silicon Valley University.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
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
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
Lesson 4-Mastering the Visual Editor. Overview Introducing the visual editor. Working in an existing file with vi. Understanding the visual editor. Navigating.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
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.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
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.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Topic: File Input/Output (I/O)
Week 3 Redirection, Pipes, and Background
Input from STDIN STDIN, standard input, comes from the keyboard.
Computer Programming Lecture 15 Text File I/O
Files I/O, Streams, I/O Redirection, Reading with fscanf
Topics Introduction to File Input and Output
Basic Input/Output Web Programming.
File Input and Output.
Perl I/O Learning Objectives:
Perl Process Management
Topics Introduction to File Input and Output
Presentation transcript:

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

COMP111 Lecture 19 / Slide 2  Another way to read input is with <>.  <> returns a single line in a scalar context, or all the remaining lines in a list context.  However, unlike, <> gets its data from the file (or files) specified on the command line. $ cat f1 1 $ cat f2 2 $ cat mycat #!/usr/local/bin/perl5 -w while(<>){ print; } $ mycat f1 f2 1 2 $ Input from <> (1)

COMP111 Lecture 19 / Slide 3  If you do not specify any filenames on the command line, <> reads from standard input automatically. $ cat mycat #!/usr/local/bin/perl5 -w while(<>){ print; } $ mycat hi test [CTRL-d] $  <> is the easiest way to read files in Perl (we will learn more advanced ways later). Input from <> (2)

COMP111 Lecture 19 / Slide 4  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.  Filehandles can also refer to external files.  Filehandles are usually ALL UPPERCASE. Filehandles

COMP111 Lecture 19 / Slide 5  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); (You can make up the name of the filehandle as you wish) Opening and Closing Filehandles

COMP111 Lecture 19 / Slide 6  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"); Opening Filehandles

COMP111 Lecture 19 / Slide 7  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 Input from Filehandles (1)

COMP111 Lecture 19 / Slide 8  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! $ Output to FILEHANDLES

COMP111 Lecture 19 / Slide 9  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! $ Using Filehandles in a Loop

COMP111 Lecture 19 / Slide 10  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 $ “die” as Error (1)

COMP111 Lecture 19 / Slide 11  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. $ “die” as Error (2)

COMP111 Lecture 19 / Slide 12  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. $ “die” as Error (3)

COMP111 Lecture 19 / Slide 13  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 $ “die” as Error (2)

COMP111 Lecture 19 / Slide 14  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 -x File Tests (1)

COMP111 Lecture 19 / Slide 15  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 $ -x File Tests (2)