Perl is a programming language developed for text manipulation and used to perform tasks like web development, system administration, GUI, network programming and so on.

What is PERL?

Perl is stable, cross-platform programming language. It can be used as Practical Extraction and Report Language. Perl was created by Larry Wall.

Perl combines features of various other languages, like C, awk, sed, sh, and BASIC, among others. It has support for Database connectivity. It can work with HTML, XML and other mark-up languages.

Perl is interpreted.

Regular Expressions

Main attraction of PERL are Regular expressions.

A regular expression can be viewed as a string of characters that can define a pattern or patterns you want to create or view.

syntax for regular expressions in Perl is very similar to other languages with support of sed, grep, and awk.

  1. Match Regular Expression – m//
  2. Translate Regular Expression – tr///
  3. Substitute Regular Expression – s///

Match operator

Match operator is used to match string or statement to a regular expression. m{},m(),m>< are variants for m//.

$var =~ /regex/ 

Options

  • i – case insensitive
  • m – match against a newline
  • o – evaluate only once
  • s – use of . to match a newline char
  • x – use of space
  • g – global
  • cg – continue even global match fails

Variables:

$ – contains last grouping match matched

$& – contains matched string

$` – contains everything before matched string

$’ – contains everything matched after string

Substitution Operator

This is used to replace matched text with some new text.

s/pattern/replacement/                                                                                                

Options

  • i – case insensitive
  • m – match against a newline
  • o – evaluate only once
  • s – use of . to match a newline char
  • x – use of space
  • g – global
  • e – evaluate the replacement as perl statement, and uses its return value as replacement text

Translation operator

Similar to substitution but does not use regular expression for search and replacement.

tr/search/replacement/

Options

  • c – complement search
  • d – deletes found
  • s – remove duplicates

Sending email using PERL

$rec  = 'receiver mail address';
$snd = 'sender mail address';
$sub = 'subject';
$msg = 'message';

open(MAIL, "|/user/sbin/sendemail -t");
#header
print MAIL "$rec\n";
print MAIL "$snd\n";
print MAIL "$sub\n";

#body
print MAIL "$msg\n";

close(MAIL);
print "DONE"

More on How To PERL? in upcoming posts.

Keep Practising

Happy learning! 🙂

One thought on “ How To PERL? ”

Leave a Reply