Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


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

1 Perl File I/O Software Tools

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

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

4 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");

5 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

6 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! $

7 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! $

8 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! $

9 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 $

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

11 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. $

12 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. $

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

14 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

15 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! $


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

Similar presentations


Ads by Google