Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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)

3 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)

4 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

5 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

6 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

7 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)

8 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

9 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

10 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)

11 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)

12 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)

13 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)

14 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)

15 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)


Download ppt "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."

Similar presentations


Ads by Google