2. FH – Opening a File
Opening a file :
open(file handler, filename);
3 Modes : Read, Write and Append
To open file in write mode
open(FH, >myfile.txt);
To open file in append mode
open(FH, >>myfile.txt);
Error handling
open(FH, myfile.txt) or die(“Could not open file : $! n”);
To read from File Handler
@filearr = <FH>;
3. FH – Writing to a File
Writing a statement to a file.
Print FH (“Writing to the filen”);
Closing a file
close(FH);
Perl automatically calls close, if we do not specify.
4. FH - seek
The seek function moves backward or forword in a file.
seek(FH, distance, relative_to);
distance is number of bytes to skip
relative_to could be either 0, 1 or 2
0 => From beginning of file
1 => From current position
2 => From end of file
5. FH – tell, read
Tell : Returns the distance, in bytes, between the beginning of the file and
the current position of the file.
tell(FH);
Read : enables us to read an aribtrary number of characters into a scalar
variable.
read(FH, result, length, offset);
result is the scalar variable into which the bytes are to be stored
lenght is the number of bytes to read
offset may be specified to place the read data at some place other than
beginning. A negative offset means, that many characters from end in
backward, a positive offset means that many from beginning forward.
read(FH,$scalar,80); # reads 80 bytes from the file represented by FH,
storing the resulting character string in $scalar
6. FH – other functions
getc : Reads a single character of input from file
$singlechar = getc(FH);
binmode : Tell the system that the file is a binary file. It must be called
after the file is opened and before read.
binmode(FH);
rename : Renames a file.
rename(oldname, newname);
unlink : Delete the file.
$num = unlink(@filelists); # It returns the number of files deleted.
mkdir : create directory
mkdir(“dirname”, permissions);
chdir(dirname) : Set the current working directory
rmdir(dirname) : Deletes an empty directory
7. FH – HERE docs
Print <<FH;
Hello I am here.
Welcome.
Bye.
FH
9. Basic RE
A regular expression is contained in slashes, and
matching occurs with the =~ operator. The following
expression is true if the string the appears in variable
$sentence.
$sentence =~ /the/
The RE is case sensitive, so if
$sentence = "The quick brown fox";
then the above match will be false.
The operator !~ is used for spotting a non-match. In
10. Special RE Characters
. # Any single character except a newline
^ # The beginning of the line or string
$ # The end of the line or string
* # Zero or more of the last character
+ # One or more of the last character
? # Zero or one of the last character
11. RE match examples
t.e # t followed by anthing followed by e
# This will match the
tre
tle
but not te, tale
^f# f at the beginning of a line
^ftp # ftp at the beginning of a line
e$ # e at the end of a line
tle$ # tle at the end of a line
12. RE match examples
and* # an followed by zero or more d characters
# This will match an
and
andd
anddd (etc)
.* # Any string without a newline. This is because the .
matches anything except a newline and the * means
zero or more of these.
^$ # A line with nothing in it.
13. More on RE
[qjk] # Either q or j or k
[^qjk] # Neither q nor j nor k
[a-z] # Anything from a to z inclusive
[^a-z] # No lower case letters
[a-zA-Z] # Any letter
[a-z]+ # Any non-zero sequence of lower case
letters
jelly|jam # Either jelly or jam
(eg|le)gs # Either eggs or legs
14. RE Special Characters
n # A newline
t # A tab
w # Any alphanumeric (word) character.
same as [a-zA-Z0-9_]
W # Any non-word character. same as [^a-
zA-Z0-9_]
d # Any digit, same as [0-9]
D # Any non-digit, same as [^0-9]
s # Any whitespace character: space, tab,
newline, etc
15. Substitution
To replace an occurrence of london by London in the
string $sentence we use the expression
$sentence =~ s/hello/Hello/
This example only replaces the first occurrence of the
string, and it may be that there will be more than one
such string we want to replace. To make a global
substitution the last slash is followed by a g as follows:
$sentence =~ s/hello/Hello/g
To ignore case and substitute.
$sentence =~ s/hello/Hello/gi
16. Translation
The tr function allows character-by-character
translation. The following expression replaces each a
with e, each b with d, and each c with f in the variable
$sentence. The expression returns the number of
substitutions made.
$sentence =~ tr/abc/edf/
To convert lower case to upper case
tr/a-z/A-Z/;
17. Remembering Patterns
remember patterns that have been matched so that
they can be used again.
anything matched in parentheses gets remembered in
the variables $1,...,$9.
These strings can also be used in the same regular
expression (or substitution) by using the special RE
codes 1,...,9.