You are on page 1of 26

PERL FILE HANDLING,

REGULAR EXPRESSIONS

C. Prayline Rajabai
Assistant Professor
SENSE
VIT University
HANDLING FILES IN PERL
Handling files in PERL is simple.

Associate a filehandle with an external entity i.e., a file.

Three basic file handles in perl are STDIN, STDOUT and


STDERR.

STDIN represents standard input

STDOUT represents standard output

STDERR represents standard error

2
OPENING A FILE
Open command opens a file and returns a filehandle
Syntax :

open FILEHANDLE, MODE EXPR

FILEHANDLE is the filehandle returned by the open function.

MODE is the mode of opening the file.


MODE OPERAND
Read <
Write >
Append >>

EXPR is the expression having the filename.


3
READ AND PRINT THE CONTENTS OF A FILE
$filename = /home/prayline/abc.txt;

open ABC, <$filename or die Error opening the file : $!;

while(<ABC>) {

print Line number $. is : $_;

$. => Returns the line number starting from 1


$_ => Returns the contents of the line

$! => Returns the error code/message.

4
WRITING TO A FILE

$filename = /home/prayline/report.txt;

open ABC, >$filename or die Error opening the file : $!;

for $i (1..20) {

print ABC $i\n;

Opening a file to append is same as writing to a


file but the mode should be >>.

5
CLOSING A FILE
close command in perl is used to close the file.

Syntax :

close FILEHANDLE;

FILEHANDLE is the filehandle of the file that


has to be closed.

6
COPYING A FILE
The below script shows how to copy the contents
of one file to the other.

#!/usr/bin/perl
open(DATA1, "<file1.txt");
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>) {
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
7
RENAMING AND DELETING A FILE
Rename command is used to rename a file.

rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );

Unlink command is used to delete a file.

unlink ("/usr/test/file1.txt");

8
REGULAR EXPRESSIONS
Regular expression is one of the most powerful

features in Perl.

What is a Regular expression (RegEx)?

A regular expression is a string of characters used to

define the pattern or patterns you are viewing.

It specifies a chunk of text.

It is a very powerful way to specify string patterns.


9
PERL SCRIPT TO SEARCH A WORD

$found = 0;
$_ =Hello good morning everybody;
$search = good;
foreach $word (split) {
if ($word eq $search) {
$found = 1;
last;
}
}
if($found) {
Print Found the word every\n;
10
PERL SCRIPT TO SEARCH A WORD USING REGEX
$_ = Hello Good Morning everybody\n
if( $_ =~ /every/) {
print Found the word every\n;
}

Very easy to use.

Text within the forward slashes defines the regular expression.

If =! is used instead of =~ then it means the pattern is not


present in the string.

All the characters in the string are significant including


punctuation and white spaces.
11
For example /every / will not match in the previous example.
TYPES OF REGEX

There are three regular expression operators


within Perl
Match - m//

Substitute - s///

Transliterate - tr///

The forward slashes in each case act as delimiters


for the regular expression that you are specifying

12
THE MATCH OPERATOR
The match operator, m// is used to match a string or

statement containing a substring.

You can omit m from m// if the delimiters are forward slashes,

but for all other delimiters you must use the m prefix.

m{}, m(), and m>< can also be used as match operator.

13
THE MATCH OPERATOR

Example :
$search = Welcome to VIT";
if ($search =~ /VIT/){
print $search is found in the string\n";
}else{
print $search is not found in the string\n";
}

The test expression will return true(1) if the regular

expression matches else it will return false(0).

14
CHARACTER CLASS
Use square brackets to specify any value in the list of
possible values
Example :
my $string = Some string 12345;
if($string =~ /[123456789]/) {
Print Found a number\n;
}
if($string =~ /[aeiou]/) {
print Found a vowel\n;
}
if($string =~ /[0123456789ABCDEF]/) {
print Found a hex digit\n;
15
}
CHARACTER CLASS NEGATION
Use ^ at the beginning of the character class to specify any
single character that is not one of these values

Example :
my $string = Some string 12345;
if($string =~ /[^123456789]/) {
print Number not found\n;
}
if($string =~ /[^aeiou]/) {
print Vowel not found\n;
}
if($string =~ /[^0123456789ABCDEF]/) {
print Hex digit not found\n;
16
}
PATTERN ABBREVIATIONS
Pattern Explanation
Abbrevations

. Any character except newline

\d A digit same as [0-9]

\w A word character, same as [A-Z, a-z, 0-9]

\s A space character (tab, space, etc)

\D Not a digit character, same as [^0-9]

\W Not a word character

\S Not a space character


17
PATTERN ABBREVIATIONS EXAMPLE
$string = Good and bad days\n;
if($string =~ /d..s/) {
print Found something like days\n;
}
if($string =~ /\w\w\w\w\s/) {
print Found a four letter word followed by a space\n;
}

Found something like days


Found a four letter word followed by a space

18
ANCHORS
There are three ways to define an anchor.
Anchor Meaning
^ Anchors to the beginning of a string
$ Anchors to the end of a string
\b Anchors to a word boundary

Examples
if($string =~ /^\w/)
Meaning :: does the string start with a word character.
if($string =~ /\d$/)
Meaning :: does the string end with a digit.
if($string =~ /\bGood\b/)
19
Meaning :: does the string contain the word Good.
MULTIPLIERS
There are three multiplier characters.
Symbol Meaning
* Find zero or more occurrences
+ Find one or more occurrences
? Find zero or one occurrence

Examples
$string =~ /^\w+/;
$string =~ /\d?/;
$string =~ /\d\w+\s+/;
$string =~ /\w+\s?$/;

20
MEMORY IN REGEX
We can use parentheses to capture a piece of matched text
for future use.

Multiple sets of parentheses can be used.

Captured text can be recalled using

\1, \2 etc., if in RegEx.

$1, $2, etc., if after RegEx.

Examples
$string = Cat and Rat;
$string =~ /(\w+)\s/;
print $1\n;
21
#prints Cat\n
THE SUBSTITUTION OPERATOR
Used to replace a substring by another substring

Syntax :
$new =~ s/pattern_to_match/new_pattern/;

It looks for a pattern_to_match in $new and if found,


replaces with the new_pattern.

It looks for the pattern once and only the first occurrence
is replaced.

22
SUBSTITUTION EXAMPLE

#/user/bin/perl
$string = "The cat sat on the mat";
$string =~ s/cat/dog/;
print "$string\n";

The dog sat on the mat

23
MODIFIERS
Two types of modifiers are defined.
/i :: Ignore case

/g :: match/substitute all occurrences.

Examples
$string = Cat and Rat;
if($string =~ s/t/n/g) {
print $string;
}
if($string =~ /CAT/i) {
print Cat is present in the string\n;
} 24
TRANSLATION OPERATOR
It is similar to substitution

The difference is it does not use regular expression for its


search on regular expressions.

The translation operators are


tr/SEARCHLIST/REPLACEMENTLIST/

y/SEARCHLIST/REPLACEMENTLIST/

The translation replaces all occurrences of the characters in


SEARCHLIST with the corresponding characters in the
REPLACEMENTLIST

25
TRANSLATION OPERATOR
Standard Perl ranges can also be used, allowing you to specify
ranges of characters either by letter or numerical value.

To change the case of the string, you might use following


syntax.

$string =~ tr/a-z/A-Z/;

Examples
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ tr/a/o/;
print "$string\n";
#prints The cot sot on the mot 26

You might also like