Professional Documents
Culture Documents
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 2
Opening Files
Syntax of “open” function:
open (filehandle, <MODE>, filename);
Returns
a nonzero value if the file has been opened successfully
0 if an error has occurred
Default open mode: READ
if (open(BIGFILE, “datafile.dat”)){
statements to run
} else {
print “Cannot open the file!\n”;
}
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 3
Closing Files
close filehandle;
open(BIGFILE, “data.txt”;
statements…
close BIGFILE;
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 4
Reading Files
Simple way to read a file: ‘file input operator’ - a pair of angle
brackets around the filehandle
open(BIGFILE, “data.txt”)
Reads a line from the file data.txt
$line=<BIGFILE>; and store it in $line
You can also run commands with Open : e.g. open(DATA, "ls -l |")
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 6
Correct/Improve the Program
(<MYFILE>) {
print "$!\n";
close (MYFILE)
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 7
File Checks (use of die and warn)
“die”: often used with file commands
When die is encountered, the program stops executing and
shows a message such as:
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 8
File Modes
Entities Definition
< Read Only Access
> Creates, Writes, and Truncates
>> Creates, Writes, and Appends
+< Reads and Writes
+> Reads, Writes, Creates and Truncates
+>> Reads, Writes, Creates and Appends
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 9
Writing Data to a File
You can use a logic structure to make sure a write has been
performed properly
if (! print MFILE $num1){
warn “Can’t write to the file!”;)
}
close (MFILE);
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 10
Exercise (Copy file content)
#!/usr/bin/perl
# Open file to read
open(DATA1, "<file1.txt");
# Open new file to write
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>) {
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
Exercises/Chapter_06_File_Operations/File-Operations-1.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 11
File Tests
Perl allows the UNIX like file tests to be performed
#/usr/bin/perl -w
my $file = "file1.txt";
my (@description, $size);
if (-e $file) {
push @description, 'a text file' if (-T $file);
push @description, 'a directory' if (-d $file);
push @description, 'executable' if (-x $file);
push @description, (($size = -s $file)) ? "$size bytes" :
'empty';
print "$file is ", join(', ',@description),"\n";
}
Exercises/Chapter_06_File_Operations/File-Operations-2.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 13
Misc File Operations
unlink file1.dat;
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 14
Misc File Operations (cont.)
$dir_entry=readdir TEMPDIR;
closedir TEMPDIR;
Just like file reading, you can read a directory line by line or an
entire directory
# Read the entire directory
opendir (MDIR, “./temp”) || die;
@filelist=readdir MDIR;
closedir MDIR;
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 15
Misc File Operations (cont.)
chdir ../book;
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 16
Do it yourself!
#!/usr/bin/perl –w
use strict;
my $file = "file.txt";
if (____) { # Check if File exists and is readable
# Open File for writing
print FILE "Created by Perl\n";
# Close the File
}
Exercises/Chapter_06_File_Operations/File-Operations-3.pl
for internal use Copyright © Infineon Technologies AG 2014. All rights reserved. Page 17