Perl 简明教程
Perl - Introduction
Perl 是一种通用编程语言,最初为文本处理而开发,现在用于各种任务,包括系统管理、Web 开发、网络编程、GUI 开发等。
What is Perl?
-
Perl 是一种稳定的跨平台编程语言。
-
虽然 Perl 不是官方缩写,但很少有人将它用作 Practical Extraction and Report Language 。
-
它被用于公共和私营部门的重大任务。
-
Perl 是一种开源软件,根据其 Artistic 许可证或 GNU 通用公共许可证 (GPL) 授权。
-
Perl 由 Larry Wall 创建。
-
Perl 1.0 于 1987 年发布到 usenet 的 alt.comp.sources。
-
在编写本教程时,perl 的最新版本是 5.16.2。
-
Perl 被列入《牛津英语词典》。
PC Magazine 宣布 Perl 是其1998年技术卓越奖开发工具类别的决赛入围者。
Perl Features
-
Perl 采用了其他语言(例如 C、awk、sed、sh 和 BASIC 等)的最佳特性。
-
Perl 的数据库集成接口 DBI 支持第三方数据库,包括 Oracle、Sybase、Postgres、MySQL 等。
-
Perl 可与 HTML、XML 和其他标记语言一起使用。
-
Perl supports Unicode.
-
Perl is Y2K compliant.
-
Perl 同时支持过程式和面向对象编程。
-
Perl 通过 XS 或 SWIG 与外部 C/C++ 库进行交互。
-
Perl 可扩展。Perl 归档网络 ( CPAN ) 上有超过 20,000 个第三方模块可用。
-
Perl 解释器可嵌入到其他系统中。
Perl and the Web
-
由于其文本处理功能和短的开发周期,Perl 曾经是最流行的网络编程语言。
-
Perl 广为人知,称为 " the duct-tape of the Internet "。
-
Perl 可以处理加密的 Web 数据,包括电子商务交易。
-
Perl 可以嵌入到 Web 服务器中,以将处理速度提高 2000%。
-
Perl 的 mod_perl 允许 Apache Web 服务器嵌入 Perl 解释器。
-
Perl 的 DBI 包使 Web-数据库集成变得容易。
Perl - Environment
在开始编写 Perl 程序之前,让我们了解如何设置 Perl 环境。Perl 可在各种平台上使用 −
-
Unix(Solaris、Linux、FreeBSD、AIX、HP/UX、SunOS、IRIX 等)
-
Win 9x/NT/2000/
-
WinCE
-
Macintosh (PPC, 68K)
-
Solaris (x86, SPARC)
-
OpenVMS
-
Alpha (7.2 and later)
-
Symbian
-
Debian GNU/kFreeBSD
-
MirOS BSD
-
And many more…
您的系统很可能已经安装了 Perl。只需在 $ 提示符下输入以下命令即可 −
$perl -v
如果您已经在机器上安装了 Perl,您将收到如下消息 −
This is perl 5, version 16, subversion 2 (v5.16.2) built for i686-linux
Copyright 1987-2012, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
如果您还没有安装 Perl,请继续进行下一部分。
Getting Perl Installation
最新的源代码、二进制文件、文档、新闻等可在 Perl 的官方网站上找到。
Perl Official Website − https://www.perl.org/
您可以从以下网站下载 Perl 文档。
Perl Documentation Website − https://perldoc.perl.org
Install Perl
Perl 发行版适用于各种平台。您只需下载适用于您平台的二进制代码并安装 Perl。
如果你的平台没有二进制代码,你需要 C 编译器来手动编译源代码。编译源代码在安装中所需特性的选择方面提供了更大的灵活性。
以下是关于在各种平台上安装 Perl 的快速概览。
Unix and Linux Installation
下面是有关在 Unix/Linux 计算机上安装 Perl 的简单步骤。
-
使用网络浏览器,并转到 https://www.perl.org/get.html.
-
按照链接下载适用于 Unix/Linux 的压缩源代码。
-
下载 perl-5.x.y.tar.gz 文件,并在 $ 提示符处发出以下命令。
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
NOTE −此处 $ 是 Unix 提示符,您可在此处键入命令,因此请确保在键入上述命令时您没有键入 $。
在标准位置 /usr/local/bin 中安装 Perl,并将其库安装在 /usr/local/lib/perlXX 中,其中 XX 是您正在使用的 Perl 版本。
在发出 make 命令后,将会花费一些时间来编译源代码。安装完成后,您可以在 $ 提示符处发出 perl -v 命令来检查 Perl 安装。如果一切都正常,那么它将显示如上所示的消息。
Windows Installation
以下是有关在 Windows 计算机上安装 Perl 的步骤。
-
按照 Windows http://strawberryperl.com 上草莓 Perl 安装的链接
-
下载 32 位或 64 位版本的安装程序。
-
通过在 Windows Explorer 中双击它来运行下载的文件。这会弹出一个 Perl 安装向导,它非常易于使用。只需接受默认设置,等到安装完成,您就可以开始操作了!
Macintosh Installation
要构建自己的 Perl 版本,您需要“make”,它是苹果开发者工具的一部分,通常随 Mac OS 安装 DVD 一起提供。无需最新的 Xcode 版本(现在要收费)才能安装 make。
以下是有关在 Mac OS X 计算机上安装 Perl 的简单步骤。
-
打开一个网络浏览器并转到 https://www.perl.org/get.html 。
-
点击链接下载适用于 Mac OS X 的压缩源代码。
-
下载 perl-5.x.y.tar.gz 文件,并在 $ 提示符处发出以下命令。
$tar -xzf perl-5.x.y.tar.gz
$cd perl-5.x.y
$./Configure -de
$make
$make test
$make install
在标准位置 /usr/local/bin 中安装 Perl,并将其库安装在 /usr/local/lib/perlXX 中,其中 XX 是您正在使用的 Perl 版本。
Running Perl
以下是启动 Perl 的不同方法。
Interactive Interpreter
您可以输入 perl ,并通过从命令行启动它来在交互式解释器中立即开始编码。您可以从 Unix、DOS 或任何其他系统执行此操作,这些系统将为您提供命令行解释器或外壳窗口。
$perl -e <perl code> # Unix/Linux
or
C:>perl -e <perl code> # Windows/DOS
以下是所有可用命令行选项的列表:
Sr.No. |
Option & Description |
1 |
-d[:debugger] Runs program under debugger |
2 |
-Idirectory Specifies @INC/#include directory |
3 |
-T Enables tainting checks |
4 |
-t Enables tainting warnings |
5 |
-U Allows unsafe operations |
6 |
-w Enables many useful warnings |
7 |
-W Enables all warnings |
8 |
-X Disables all warnings |
9 |
-e program 以程序方式发送的运行 Perl 脚本 |
10 |
file 从给定文件中运行 Perl 脚本 |
Script from the Command-line
Perl 脚本是一个文本文件,其中包含 Perl 代码,可以在命令行中调用应用程序上的解释器来执行,如下所示 −
$perl script.pl # Unix/Linux
or
C:>perl script.pl # Windows/DOS
Integrated Development Environment
您也可以从图形用户界面 (GUI) 环境中运行 Perl。您只需在支持 Perl 的系统上使用 GUI 应用程序即可。您可以下载 Padre, the Perl IDE 。如果您熟悉 Eclipse,还可以使用 Eclipse 插件 EPIC - Perl Editor and IDE for Eclipse 。
在进入下一章之前,请确保您的环境已正确设置且运行正常。如果您无法正确设置环境,则可以向系统管理员寻求帮助。
后续章节给出的所有示例均使用 CentOS 发行版 Linux 上可用的 v5.16.2 版本执行。
Perl - Syntax Overview
Perl 从许多语言借用了语法和概念:awk、sed、C、Bourne Shell、Smalltalk、Lisp 甚至英语。但是,语言之间存在一些明确的差异。本章旨在让您快速了解 Perl 中预期的语法。
Perl 程序包括一系列声明和语句,从上到下运行。循环、子例程和其他控制结构允许您在代码中跳转。每个简单语句都必须以分号 (;) 结尾。
Perl 是一种自由格式语言:您可以根据自己的喜好对其进行格式化和缩进。空白主要用于分隔标记,这与 Python 等语言不同,后者语法的重要组成部分,或者 Fortran,其中它不重要。
First Perl Program
Interactive Mode Programming
您可以在命令行中使用 -e 选项使用 Perl 解释器,它允许您从命令行执行 Perl 语句。让我们在 $ 提示符下尝试如下操作 −
$perl -e 'print "Hello World\n"'
此执行将产生以下结果 −
Hello, world
Script Mode Programming
假设您已在 $ 提示符下,让我们使用 vi 或 vim 编辑器打开一个文本文件 hello.pl,并在文件中添加以下行。
#!/usr/bin/perl
# This will print "Hello, World"
print "Hello, world\n";
在此 /usr/bin/perl 是实际的 Perl 解释器二进制文件。在执行脚本之前,请确保更改脚本文件模式并给予执行权限,通常设置为 0755,最后执行上述脚本,如下所示 −
$chmod 0755 hello.pl
$./hello.pl
此执行将产生以下结果 −
Hello, world
您可以根据个人喜好对函数参数使用圆括号,也可以省略圆括号。只有在需要澄清优先级问题时才偶尔需要它们。以下两个陈述产生相同的结果。
print("Hello, world\n");
print "Hello, world\n";
Perl File Extension
Perl 脚本可以在任何普通纯文本编辑器程序中创建。每种类型的平台都有多种程序可用。网上有许多专为程序员设计的程序可供下载。
根据 Perl 约定,Perl 文件必须使用 .pl 或 .PL 文件扩展名保存,才能被识别为功能性 Perl 脚本。文件名可以包含数字、符号和字母,但不能包含空格。在空格处使用下划线 (_)。
Comments in Perl
任何编程语言中的注释都是开发人员的朋友。注释可用于使程序对用户友好,并且解释器会简单地跳过这些注释,而不会影响代码功能。例如,在上面的程序中,以散列符号 # 开头的行是注释。
简单来说,Perl 中的注释以散列符号开头并一直运行到行的末尾 −
# This is a comment in perl
以 = 开头的行解释为嵌入式文档(pod)部分的开头,并且编译器会忽略此后所有行,直到遇到下一个 =cut。以下示例 −
#!/usr/bin/perl
# This is a single line comment
print "Hello, world\n";
=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the
compiler until the next =cut is encountered.
=cut
这会产生以下结果 −
Hello, world
Whitespaces in Perl
Perl 程序不关心空格。以下程序可以很好地运行:
#!/usr/bin/perl
print "Hello, world\n";
但如果空格在引号字符串内,它们将按原样打印。例如:
#!/usr/bin/perl
# This would print with a line break in the middle
print "Hello
world\n";
这会产生以下结果 −
Hello
world
在引号之外使用时,所有类型的空格(如空格、制表符、换行符等)对解释器来说都是等效的。包含只包含空格的行(可能带有注释)被称为空白行,Perl 完全忽略它。
Single and Double Quotes in Perl
你可以按如下方式对文字字符串使用双引号或单引号:
#!/usr/bin/perl
print "Hello, world\n";
print 'Hello, world\n';
这会产生以下结果 −
Hello, world
Hello, world\n$
在单引号和双引号中有一个重要的区别。只有双引号中的@ {s0}变量和换行符 \n 等特殊字符,而单引号不插入任何变量或特殊字符。在下面的示例中,我们将 $a 用作变量来存储一个值,然后打印该值:
#!/usr/bin/perl
$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
这会产生以下结果 −
Value of a = 10
Value of a = $a\n$
"Here" Documents
你可以非常方便地存储或打印多行文本。你甚至可以在“此处”文档中使用变量。下面是一个简单的语法,请仔细检查 << 和标识符之间不能有空格。
标识符可以是裸词或我们下面用于 EOF 的一些引用文本。如果标识符被引用,你使用的引用类型决定了此处文档中文本的处理方式,就像在常规引用中一样。未引用的标识符的工作方式就像双引号。
#!/usr/bin/perl
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
$var = <<'EOF';
This is case of single quote so variable value will be
interpolated. For example value of a = $a
EOF
print "$var\n";
这会产生以下结果 −
This is the syntax for here document and it will continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
interpolated. For example value of a = 10
This is case of single quote so variable value will be
interpolated. For example value of a = $a
Perl - Data Types
Perl 是一种松散类型的语言,在程序中使用数据时无需指定类型的类型。Perl 解释器将根据数据本身的上下文选择类型。
Perl 有三种基本数据类型:标量、标量的数组和标量的散列,也称为关联数组。以下是对这些数据类型的简要说明。
Sr.No. |
Types & Description |
1 |
Scalar 标量是简单变量。它们前面带有美元符号 ($)。标量是数字、字符串或引用。引用实际上是一个变量的地址,我们将在接下来的章节中看到。 |
2 |
Arrays 数组是有序标量列表,您可以使用从 0 开始的数字索引访问它们。它们前面带有“at”符号 (@)。 |
3 |
Hashes 散列是无序键值对集,您可以使用键作为下标访问它们。它们前面带有百分号 (%)。 |
Numeric Literals
Perl 在内部将所有数字存储为有符号整数或双精度浮点值。数字文本使用以下浮点或整数格式中的任何一种指定 −
Type |
Value |
Integer |
1234 |
Negative integer |
-100 |
Floating point |
2000 |
Scientific notation |
16.12E14 |
Hexadecimal |
0xffff |
Octal |
0577 |
String Literals
字符串是由字符序列构成的。它们通常是使用单引号 (') 或双引号 (") 分隔的字母数字值。它们与 UNIX shell 引号类似,您可以在其中使用单引号字符串和双引号字符串。
双引号字符串文本允许变量插值,而单引号字符串则不行。某些字符在反斜杠前面时具有特殊含义,并且用于表示换行符 (\n) 或制表符 (\t)。
您可以在双引号字符串中直接嵌入换行符或以下任何转义序列−
Escape sequence |
Meaning |
|Backslash |
\' |
Single quote |
\" |
Double quote |
\a |
Alert or bell |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\0nn |
Creates Octal formatted numbers |
\xnn |
Creates Hexideciamal formatted numbers |
\cX |
控制字符,x 可以是任何字符 |
\u |
迫使下一个字母大写 |
\l |
迫使下一个字母小写 |
\U |
迫使所有后续字母大写 |
\L |
迫使所有后续字母小写 |
\Q |
对所有后续非字母数字字符进行反斜杠转义 |
\E |
Example
让我们再来看看字符串在单引号和双引号下的行为方式。这里,我们将使用上表中提到的字符串转义,并将利用标量变量分配字符串值。
#!/usr/bin/perl
# This is case of interpolation.
$str = "Welcome to \ntutorialspoint.com!";
print "$str\n";
# This is case of non-interpolation.
$str = 'Welcome to \ntutorialspoint.com!';
print "$str\n";
# Only W will become upper case.
$str = "\uwelcome to tutorialspoint.com!";
print "$str\n";
# Whole line will become capital.
$str = "\UWelcome to tutorialspoint.com!";
print "$str\n";
# A portion of line will become capital.
$str = "Welcome to \Ututorialspoint\E.com!";
print "$str\n";
# Backsalash non alpha-numeric including spaces.
$str = "\QWelcome to tutorialspoint's family";
print "$str\n";
这会产生以下结果 −
Welcome to
tutorialspoint.com!
Welcome to \ntutorialspoint.com!
Welcome to tutorialspoint.com!
WELCOME TO TUTORIALSPOINT.COM!
Welcome to TUTORIALSPOINT.com!
Welcome\ to\ tutorialspoint\'s\ family
Perl - Variables
变量是存储值的保留内存位置。这意味着当你创建一个变量时,你就预留了一些内存空间。
基于变量的数据类型,解释器分配内存并决定存储在保留内存中的内容。因此,通过将不同的数据类型分配给变量,你可以在这些变量中存储整数、小数或字符串。
我们已经了解到 Perl 具有以下三种基本数据类型 −
-
Scalars
-
Arrays
-
Hashes
相应地,我们将在 Perl 中使用三种类型的变量。 scalar 变量将以美元符号 ($) 为前缀,它可以存储数字、字符串或引用。 array 变量将以符号 @ 为前缀,它将存储标量的有序列表。最后, Hash 变量将以符号 % 为前缀,并且将用于存储成对键/值集。
Perl 以单独的命名空间维护每种变量类型。因此,你可以使用相同的名称将一个标量变量、一个数组或一个哈希用于变量,而不必担心冲突。这意味着 $foo 和 @foo 是两个不同的变量。
运算符左边操作数是变量名,= 运算符右边操作数是存储在变量中的值。例如 −
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
这里 25、“John Paul”和 1445.50 分别分配给 $age、$name 和 $salary 变量的值。我们很快将看到如何将值分配给数组和哈希。
Scalar Variables
标量是单个数据单元。该数据可以是整数、浮点数、字符、字符串、段落或整个网页。简单来说它可以是任何东西,但只是一种。
下面是使用标量变量的一个简单示例−
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
这会产生以下结果 −
Age = 25
Name = John Paul
Salary = 1445.5
Array Variables
数组是存储标量值的已排序列表的变量。数组变量前面有“at”(@) 符号。若要引用数组的单个元素,您需在变量名称后加上美元符号 ($),再跟上方括号中的元素索引。
下面是使用数组变量的一个简单示例:
#!/usr/bin/perl
@ages = (25, 30, 40);
@names = ("John Paul", "Lisa", "Kumar");
print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";
在这里,我们在 $ 符号之前使用转义符号 (\) 只是为了打印它。其他 Perl 会将其理解为变量,并会打印其值。执行后,将生成以下结果:
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = John Paul
$names[1] = Lisa
$names[2] = Kumar
Hash Variables
哈希是一组 key/value 对。哈希变量前面有百分号 (%) 符号。若要引用哈希的单个元素,您需在变量名称后加上用花括号括起来的与该值相关联的“键”。
下面是使用哈希变量的一个简单示例:
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
print "\$data{'John Paul'} = $data{'John Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
这会产生以下结果 −
$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40
Variable Context
在 Perl 中,变量的处理方式因上下文而异,即变量的使用场景。我们来看一下以下示例 −
#!/usr/bin/perl
@names = ('John Paul', 'Lisa', 'Kumar');
@copy = @names;
$size = @names;
print "Given names are : @copy\n";
print "Number of names are : $size\n";
这会产生以下结果 −
Given names are : John Paul Lisa Kumar
Number of names are : 3
在此示例中,@name 是一个数组,它已在两个不同的上下文中使用。首先,我们把它复制到另一个数组中,即列表,因此它返回了所有元素,假设上下文是列表上下文。接下来,我们使用了相同的数组并尝试将此数组存储为标量,因此在这种情况下,它只返回了此数组中的元素数量,假设上下文是标量上下文。下表列出了不同的上下文 −
Sr.No. |
Context & Description |
1 |
Scalar 对标量变量进行赋值,在标量上下文中计算右值。 |
2 |
List 对数组或哈希进行赋值,在列表上下文中计算右值。 |
3 |
Boolean 布尔上下文,即对表达式进行计算以确定它为真还是为假。 |
4 |
Void 此上下文不仅不关心返回值是什么,它甚至不需要返回值。 |
5 |
Interpolative 此上下文只在引号中或与引号类似的事物中发生。 |
Perl - Scalars
标量是单个数据单元。该数据可以是整数、浮点数、字符、字符串、段落或整个网页。
下面是使用标量变量的一个简单示例−
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
这会产生以下结果 −
Age = 25
Name = John Paul
Salary = 1445.5
Numeric Scalars
标量最常见的是数字或字符串。以下示例演示各种类型的数字标量的用途 −
#!/usr/bin/perl
$integer = 200;
$negative = -300;
$floating = 200.340;
$bigfloat = -1.2E-23;
# 377 octal, same as 255 decimal
$octal = 0377;
# FF hex, also 255 decimal
$hexa = 0xff;
print "integer = $integer\n";
print "negative = $negative\n";
print "floating = $floating\n";
print "bigfloat = $bigfloat\n";
print "octal = $octal\n";
print "hexa = $hexa\n";
这会产生以下结果 −
integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255
String Scalars
以下示例演示了各种类型字符串标量的用法。请注意单引号字符串和双引号字符串之间的区别 −
#!/usr/bin/perl
$var = "This is string scalar!";
$quote = 'I m inside single quote - $var';
$double = "This is inside single quote - $var";
$escape = "This example of escape -\tHello, World!";
print "var = $var\n";
print "quote = $quote\n";
print "double = $double\n";
print "escape = $escape\n";
这会产生以下结果 −
var = This is string scalar!
quote = I m inside single quote - $var
double = This is inside single quote - This is string scalar!
escape = This example of escape - Hello, World
Scalar Operations
您将在单独的章节中看到 Perl 中可用的各种运算符的详细信息,但我们在这里列出一些数字和字符串操作。
#!/usr/bin/perl
$str = "hello" . "world"; # Concatenates strings.
$num = 5 + 10; # adds two numbers.
$mul = 4 * 5; # multiplies two numbers.
$mix = $str . $num; # concatenates string and number.
print "str = $str\n";
print "num = $num\n";
print "mix = $mix\n";
这会产生以下结果 −
str = helloworld
num = 15
mul = 20
mix = helloworld15
Multiline Strings
如果您想将多行字符串引入程序中,可以使用标准单引号,如下所示 −
#!/usr/bin/perl
$string = 'This is
a multiline
string';
print "$string\n";
这会产生以下结果 −
This is
a multiline
string
您还可以使用“here”文档语法来存储或打印多行,如下所示 −
#!/usr/bin/perl
print <<EOF;
This is
a multiline
string
EOF
这也会产生相同的结果 −
This is
a multiline
string
V-Strings
形式为 v1.20.300.4000 的文本被解析为由指定序数的字符组成的字符串。此形式称为 v 字符串。
与使用不太可读的插形式“\x{1}\x{14}\x{12c}\x{fa0}”相比,v 字符串提供了一种替代的且更具可读性的方式来构造字符串。
它们是从 v 开头的任何文本,后跟一个或多个用点分隔的元素。例如 −
#!/usr/bin/perl
$smile = v9786;
$foo = v102.111.111;
$martin = v77.97.114.116.105.110;
print "smile = $smile\n";
print "foo = $foo\n";
print "martin = $martin\n";
这也会产生相同的结果 −
smile = ☺
foo = foo
martin = Martin
Wide character in print at main.pl line 7.
Special Literals
到目前为止,您一定对字符串标量及其连接和插值操作有所了解。因此,让我告诉你三个特殊的文本 FILE 、 LINE 和 PACKAGE 表示程序中当前的文件名、行号和软件包名称。
它们只能用作单独的标记,并且不会插值到字符串中。查看以下示例 −
#!/usr/bin/perl
print "File name ". __FILE__ . "\n";
print "Line Number " . __LINE__ ."\n";
print "Package " . __PACKAGE__ ."\n";
# they can not be interpolated
print "__FILE__ __LINE__ __PACKAGE__\n";
这会产生以下结果 −
File name hello.pl
Line Number 4
Package main
__FILE__ __LINE__ __PACKAGE__
Perl - Arrays
数组是存储标量值的已排序列表的变量。数组变量前面有“at”(@) 符号。若要引用数组的单个元素,您需在变量名称后加上美元符号 ($),再跟上方括号中的元素索引。
以下是一个使用数组变量的简单示例 -
#!/usr/bin/perl
@ages = (25, 30, 40);
@names = ("John Paul", "Lisa", "Kumar");
print "\$ages[0] = $ages[0]\n";
print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";
这里我们在 $ 符号之前使用了转义符号 (\) 只是为了打印它。其他 Perl 将将其理解为一个变量并将打印其值。执行时,这将产生以下结果 -
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = John Paul
$names[1] = Lisa
$names[2] = Kumar
在 Perl 中,列表和数组术语经常被用来表示它们是可以互换的。但是列表是数据,数组是变量。
Array Creation
数组变量之前带有 @ 符号,并且使用括号或 qw 运算符填充。例如 -
@array = (1, 2, 'Hello');
@array = qw/This is an array/;
第二行使用 qw// 运算符,它返回一个字符串列表,由空格分隔定界字符串。在此示例中,这会导致一个四元素数组;第一个元素是“this”,最后一个(第四个)是“array”。这意味着您可以按如下方式使用不同的行 -
@days = qw/Monday
Tuesday
...
Sunday/;
您还可以在如下所示分别分配每个值来填充数组 -
$array[0] = 'Monday';
...
$array[6] = 'Sunday';
Accessing Array Elements
从数组中访问单个元素时,您必须在变量名前加上美元符号 ($),然后在变量名后方括号内追加元素索引。例如 -
#!/usr/bin/perl
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
print "$days[0]\n";
print "$days[1]\n";
print "$days[2]\n";
print "$days[6]\n";
print "$days[-1]\n";
print "$days[-7]\n";
这会产生以下结果 −
Mon
Tue
Wed
Sun
Sun
Mon
数组索引从 0 开始,因此若要访问第一个元素,您需要给 0 作为索引。您还可以给出一个负索引,在这种情况下,您从数组的末尾而不是开头选择元素。这意味着以下内容 -
print $days[-1]; # outputs Sun
print $days[-7]; # outputs Mon
Sequential Number Arrays
Perl 为顺序数字和字母提供了快捷方式。例如,与其在计数到 100 时键入每个元素,我们还可以执行如下所示的操作 -
#!/usr/bin/perl
@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);
print "@var_10\n"; # Prints number from 1 to 10
print "@var_20\n"; # Prints number from 10 to 20
print "@var_abc\n"; # Prints number from a to z
这里双点 (..) 被称为 range operator 。这将产生以下结果 -
1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z
Array Size
可以使用数组上的标量上下文来确定数组的大小 - 返回值将是数组中的元素数 -
@array = (1,2,3);
print "Size: ",scalar @array,"\n";
返回的值始终是数组的物理大小,而不是有效元素的数量。您可以使用以下片段演示这一点,以及标量 @array 和 $#array 之间的差异 -
#!/usr/bin/perl
@array = (1,2,3);
$array[50] = 4;
$size = @array;
$max_index = $#array;
print "Size: $size\n";
print "Max Index: $max_index\n";
这会产生以下结果 −
Size: 51
Max Index: 50
包含信息的数组中仅有四个元素,但数组长 51 个元素,最高索引为 50。
Adding and Removing Elements in Array
Perl 提供了许多有用的函数来添加和移除数组中的元素。你可能有一个疑问,什么是函数?到目前为止,你已经使用 print 函数来打印各种值。与此类似,还有许多其他的函数或有时称为子例程,这些函数可用于各种其他功能。
Sr.No. |
Types & Description |
1 |
push @ARRAY, LIST 将列表值推送到数组末尾。 |
2 |
pop @ARRAY 弹出并返回数组的最后一个值。 |
3 |
shift @ARRAY 将数组的第一个值移出并返回,将数组缩短 1 并向下移动所有内容。 |
4 |
unshift @ARRAY, LIST 将列表前置到数组的开头,并返回新数组中的元素数量。 |
#!/usr/bin/perl
# create a simple array
@coins = ("Quarter","Dime","Nickel");
print "1. \@coins = @coins\n";
# add one element at the end of the array
push(@coins, "Penny");
print "2. \@coins = @coins\n";
# add one element at the beginning of the array
unshift(@coins, "Dollar");
print "3. \@coins = @coins\n";
# remove one element from the last of the array.
pop(@coins);
print "4. \@coins = @coins\n";
# remove one element from the beginning of the array.
shift(@coins);
print "5. \@coins = @coins\n";
这会产生以下结果 −
1. @coins = Quarter Dime Nickel
2. @coins = Quarter Dime Nickel Penny
3. @coins = Dollar Quarter Dime Nickel Penny
4. @coins = Dollar Quarter Dime Nickel
5. @coins = Quarter Dime Nickel
Slicing Array Elements
你还可以从数组中提取“切片”,即,你可以从数组中选择多个项目以生成另一个数组。
#!/usr/bin/perl
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@weekdays = @days[3,4,5];
print "@weekdays\n";
这会产生以下结果 −
Thu Fri Sat
切片的规范必须具有一个有效的索引列表,可以是正数,也可以是负数,每个索引由逗号分隔。为了加快速度,你还可以使用 .. 范围运算符 −
#!/usr/bin/perl
@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
@weekdays = @days[3..5];
print "@weekdays\n";
这会产生以下结果 −
Thu Fri Sat
Replacing Array Elements
现在,我们将引入另一个称为 splice() 的函数,其语法如下 −
splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]
此函数将移除 @ARRAY 中由 OFFSET 和 LENGTH 指定的元素,并在指定的情况下用 LIST 替换它们。最后,它返回从数组中移除的元素。下面是示例 −
#!/usr/bin/perl
@nums = (1..20);
print "Before - @nums\n";
splice(@nums, 5, 5, 21..25);
print "After - @nums\n";
这会产生以下结果 −
Before - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
After - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20
在此,实际替换从第 6 个数字开始,然后五个元素从 6 到 10 替换为数字 21、22、23、24 和 25。
Transform Strings to Arrays
让我们再来看一个名为 split() 的函数,其语法如下 −
split [ PATTERN [ , EXPR [ , LIMIT ] ] ]
此函数将字符串拆分为字符串数组并返回。如果指定 LIMIT,最多拆分为该数量的字段。如果省略 PATTERN,则按空格拆分。下面是示例 −
#!/usr/bin/perl
# define Strings
$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$var_names = "Larry,David,Roger,Ken,Michael,Tom";
# transform above strings into arrays.
@string = split('-', $var_string);
@names = split(',', $var_names);
print "$string[3]\n"; # This will print Roses
print "$names[4]\n"; # This will print Michael
这会产生以下结果 −
Roses
Michael
Transform Arrays to Strings
我们可以使用 join() 函数重新连接数组元素并形成一个较长的标量字符串。此函数具有以下语法 −
join EXPR, LIST
此函数以用 EXPR 值分隔的字段,将 LIST 的各个字符串连接成一个字符串,并返回该字符串。下面是示例 −
#!/usr/bin/perl
# define Strings
$var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$var_names = "Larry,David,Roger,Ken,Michael,Tom";
# transform above strings into arrays.
@string = split('-', $var_string);
@names = split(',', $var_names);
$string1 = join( '-', @string );
$string2 = join( ',', @names );
print "$string1\n";
print "$string2\n";
这会产生以下结果 −
Rain-Drops-On-Roses-And-Whiskers-On-Kittens
Larry,David,Roger,Ken,Michael,Tom
Sorting Arrays
sort() 函数根据 ASCII 数字标准对数组的每个元素进行排序。此函数具有以下语法 −
sort [ SUBROUTINE ] LIST
此函数对 LIST 进行排序并返回已排序数组值。如果指定了 SUBROUTINE,则在排序元素时应用 SUBTROUTINE 中指定的逻辑。
#!/usr/bin/perl
# define an array
@foods = qw(pizza steak chicken burgers);
print "Before: @foods\n";
# sort this array
@foods = sort(@foods);
print "After: @foods\n";
这会产生以下结果 −
Before: pizza steak chicken burgers
After: burgers chicken pizza steak
请注意,排序是根据单词的 ASCII 数值进行的。因此,最佳选项是先将数组的每个元素转换为小写字母,然后再执行排序函数。
Merging Arrays
因为数组只是一个用逗号分隔的值序列,所以你可以像下面这样将它们组合在一起 −
#!/usr/bin/perl
@numbers = (1,3,(4,5,6));
print "numbers = @numbers\n";
这会产生以下结果 −
numbers = 1 3 4 5 6
嵌入式数组成为主数组的一部分,如下所示−
#!/usr/bin/perl
@odd = (1,3,5);
@even = (2, 4, 6);
@numbers = (@odd, @even);
print "numbers = @numbers\n";
这会产生以下结果 −
numbers = 1 3 5 2 4 6
Selecting Elements from Lists
列表符号与数组的符号相同。您可以通过向列表后追加方括号并指定一个或多个索引来从数组中提取元素−
#!/usr/bin/perl
$var = (5,4,3,2,1)[4];
print "value of var = $var\n"
这会产生以下结果 −
value of var = 1
类似地,我们可以提取切片,但不需要前导@字符−
#!/usr/bin/perl
@list = (5,4,3,2,1)[1..3];
print "Value of list = @list\n";
这会产生以下结果 −
Value of list = 4 3 2
Perl - Hashes
哈希是一组 key/value 对。哈希变量前面带有百分号 (%) 符号。要引用哈希的单个元素,您将使用哈希变量名称,其前面带有“$”符号,后面用花括号括起与值关联的“键”。
以下是一个使用哈希变量的简单示例 −
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
print "\$data{'John Paul'} = $data{'John Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
这会产生以下结果 −
$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40
Creating Hashes
哈希使用以下两种方式之一创建。在第一种方法中,您逐个为命名键分配一个值 −
$data{'John Paul'} = 45;
$data{'Lisa'} = 30;
$data{'Kumar'} = 40;
在第二种情况下,您使用列表,该列表通过从列表中获取各个对来转换:此对中的第一个元素用作键,第二个元素用作值。例如 −
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
为清楚起见,可以使用 ⇒ 作为“,”的别名,以指示键/值对,如下所示 −
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
以下是上述形式的另一个变体,请看一看,此处所有键都已加上了连字符 (-),并且它们的周围不需要引号 −
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
但要注意的是,此种形式的哈希形成中使用了一个没有空格的单词,即键,并且如果您以这种方式建立哈希,则只能使用连字符来访问键,如下所示。
$val = %data{-JohnPaul}
$val = %data{-Lisa}
Accessing Hash Elements
当从哈希中访问各个元素时,您必须在变量前加上美元符号 ($),然后在变量名称后面用花括号括起元素键。例如 −
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
print "$data{'John Paul'}\n";
print "$data{'Lisa'}\n";
print "$data{'Kumar'}\n";
这会产生以下结果 −
45
30
40
Extracting Slices
您可以提取哈希切片,就像提取数组切片一样。您需要为变量使用 @ 前缀来存储返回的值,因为它们将成为值列表 −
#!/uer/bin/perl
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
@array = @data{-JohnPaul, -Lisa};
print "Array : @array\n";
这会产生以下结果 −
Array : 45 30
Extracting Keys and Values
可以用 keys 函数获取散列中所有键的一个列表,该函数具有以下语法:
keys %HASH
此函数返回命名散列中所有键的数组。以下是示例:
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@names = keys %data;
print "$names[0]\n";
print "$names[1]\n";
print "$names[2]\n";
这会产生以下结果 −
Lisa
John Paul
Kumar
同样,可以使用 values 函数来获取所有值的一个列表。此函数具有以下语法:
values %HASH
此函数返回由命名散列中的所有值组成的普通数组。以下是示例:
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@ages = values %data;
print "$ages[0]\n";
print "$ages[1]\n";
print "$ages[2]\n";
这会产生以下结果 −
30
45
40
Checking for Existence
如果你尝试从不存在的散列中访问一个键/值对,通常会获得 undefined 值,并且如果你禁用了警告,则会在运行时收到一个警告。你可以通过使用 exists 函数避免这种情况,无论它的值是什么,该函数在命名键存在时返回 true:
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
if( exists($data{'Lisa'} ) ) {
print "Lisa is $data{'Lisa'} years old\n";
} else {
print "I don't know age of Lisa\n";
}
这里我们引入了 IF…ELSE 语句,我们将在单独的章节中学习该语句。现在只需假设仅当给定条件为真时执行 if( condition ) 部分,否则执行 else 部分。因此,当我们执行上述程序时,它会产生以下结果,因为这里给定的条件存在($data{'Lisa'} 返回 true):
Lisa is 30 years old
Getting Hash Size
你可以通过对键或值使用标量上下文来获取大小,即散列中的元素数。简单来说,你需要先获取键或值的数组,然后可以按照以下方式获取数组的大小:
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
@values = values %data;
$size = @values;
print "2 - Hash size: is $size\n";
这会产生以下结果 −
1 - Hash size: is 3
2 - Hash size: is 3
Add and Remove Elements in Hashes
使用简单的赋值运算符,可以用一行代码添加一个新的键/值对。但是,若要从散列中删除一个元素,则需要使用 delete 函数,如下例所示:
#!/usr/bin/perl
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
@keys = keys %data;
$size = @keys;
print "1 - Hash size: is $size\n";
# adding an element to the hash;
$data{'Ali'} = 55;
@keys = keys %data;
$size = @keys;
print "2 - Hash size: is $size\n";
# delete the same element from the hash;
delete $data{'Ali'};
@keys = keys %data;
$size = @keys;
print "3 - Hash size: is $size\n";
这会产生以下结果 −
1 - Hash size: is 3
2 - Hash size: is 4
3 - Hash size: is 3
Perl Conditional Statements - IF…ELSE
Perl 条件语句有助于决策制定,它要求程序员指定程序要计算或测试的一个或多个条件,以及如果确定条件为真时执行的语句或语句,并且在确定条件为假时可以选择要执行其他语句。
以下是大多数编程语言中典型的决策结构的总体形式 −
数字 0、字符串 '0' 和 ""、空列表 () 和 undef 在布尔上下文中均为 false ,其他所有值均为 true 。通过 ! 或 not 对真值取反将返回一个特殊的假值。
Perl 编程语言提供了以下类型的条件语句。
Sr.No. |
Statement & Description |
1 |
if statement An if statement 包含一个布尔表达式,后跟一个或多个语句。 |
2 |
if…else statement An if statement 后跟一个可选的 else statement 。 |
3 |
if…elsif…else statement An if statement 后跟一个可选的 elsif statement ,然后后跟一个可选的 else statement 。 |
4 |
unless statement An unless statement 包含一个布尔表达式,后跟一个或多个语句。 |
5 |
unless…else statement An unless statement 后跟一个可选的 else statement 。 |
6 |
unless…elsif..else statement An unless statement 后跟一个可选的 elsif statement ,然后后跟一个可选的 else statement 。 |
7 |
switch statement 使用最新版本的 Perl,你可以使用 switch 语句。它允许以简单的方式比较变量值与各种条件。 |
The ? : Operator
让我们检查 conditional operator ? :*which can be used to replace *if…else 语句。其具有以下通用形式:
Exp1 ? Exp2 : Exp3;
其中 Exp1、Exp2 和 Exp3 是表达式。请注意冒号的用处和位置。
一个?表达式的值将以这种方式确定:计算 Exp1。如果为 true,则计算 Exp2 并将其作为整个?表达式的值。如果 Exp1 为 false,则计算 Exp3,并将它的值作为表达式的值。这是一个使用该运算符的简单示例 −
#!/usr/local/bin/perl
$name = "Ali";
$age = 10;
$status = ($age > 60 )? "A senior citizen" : "Not a senior citizen";
print "$name is - $status\n";
这会产生以下结果 −
Ali is - Not a senior citizen
Perl - Loops
您可能需要执行一段代码块多次。通常,语句按顺序执行:函数中的第一个语句先执行,然后是第二个,依此类推。
编程语言提供了各种控制结构,允许执行更复杂的路径。
循环语句允许我们多次执行一个语句或一组语句,以下是大多数编程语言中循环语句的一般形式 −
Perl 编程语言提供了以下类型的循环来处理循环要求。
Sr.No. |
Loop Type & Description |
1 |
@ {s4}重复语句或一组语句,同时给定的条件为真。它在执行循环体之前测试条件。 |
2 |
@ {s5}重复一个语句或一组语句,直到给定的条件变为真。它在执行循环体之前测试条件。 |
3 |
@ {s6}执行一系列语句多次,并缩写管理循环变量的代码。 |
4 |
@ {s7} foreach 循环迭代一个常规列表值,并将变量 VAR 设置为列表中的每个元素。 |
5 |
@ {s8}像一个 while 语句,不同之处在于它在循环体结束时测试条件 |
6 |
@ {s9}你可以在任何另一个 while、for 或 do..while 循环中使用一个或多个循环。 |
Loop Control Statements
循环控制语句会更改从其正常序列执行。当执行离开作用域时,在该作用域中创建的所有自动对象都会被销毁。
Perl 支持以下控制语句。单击以下链接查看它们的详细信息。
Sr.No. |
Control Statement & Description |
1 |
@ {s10}使循环跳过其主体的其余部分,并在重新迭代之前立即重新测试其条件。 |
2 |
last statement 终止循环语句并执行紧跟循环之后的语句。 |
3 |
continue statement 一个 continue BLOCK,它通常在条件再次被评估之前被执行。 |
4 |
redo statement redo 命令重新启动循环块,而不会再次评估条件。continue 块(如果存在)将不会被执行。 |
5 |
goto statement Perl 支持具有三种形式的 goto 命令:goto 标签、goto expr 和 goto &name。 |
Perl - Operators
What is an Operator?
使用表达式4 + 5 等于 9 可以给出简单的答案。这里 4 和 5 称为运算符,+ 称为运算符。Perl 语言支持许多运算符类型,但以下是重要且最常用的运算符列表−
-
Arithmetic Operators
-
Equality Operators
-
Logical Operators
-
Assignment Operators
-
Bitwise Operators
-
Logical Operators
-
Quote-like Operators
-
Miscellaneous Operators
让我们逐个查看所有运算符。
Perl Arithmetic Operators
假设变量 $a 容纳 10,变量 $b 容纳 20,那么以下是 Perl 算术运算符−
Sr.No. |
Operator & Description |
1 |
+ ( Addition ) 在运算符两侧添加值 Example − $a + $b 将给出 30 |
2 |
- (Subtraction) 从左手操作数中减去右手操作数 Example − $a - $b 将给出 -10 |
3 |
* (乘法)*在运算符的两侧乘以值 *Example − $a * $b 将给出 200 |
4 |
/ (Division) 将左手操作数除以右手操作数 Example − $b / $a 将给出 2 |
5 |
% (Modulus) 将左手操作数除以右手操作数并返回余数 Example − $b % $a 将给出 0 |
6 |
* (指数)*对运算符执行指数(幂)计算 *Example − $a*$b 将给出 10 的 20 次幂 |
Perl Equality Operators
这些也称为关系运算符。假设变量 $a 容纳 10,变量 $b 容纳 20,接下来让我们检查以下数字相等运算符−
Sr.No. |
Operator & Description |
1 |
== (equal to) 检查两个操作数的值是否相等,如果相等,则条件变为真。 Example − ($a == $b) 为假。 |
2 |
!= (not equal to) 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 Example − ($a != $b) 为真。 |
3 |
<⇒ 检查两个操作数的值是否相等,并根据左参数在数值上小于、等于还是大于右参数,返回 -1、0 或 1。 Example − ($a <⇒ $b) 返回 -1。 |
4 |
> (greater than) 检查左手操作数的值是否大于右手操作数的值,如果大于,则条件变为真。 Example − ($a > $b) 为假。 |
5 |
< (less than) 检查左操作数的值是否小于右操作数的值,如果是,则条件变为真。 Example − ($a < $b) 为真。 |
6 |
>= (greater than or equal to) 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件变为真。 Example − ($a >= $b) 为假。 |
7 |
⇐ (less than or equal to) 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 Example − ($a ⇐ $b) 为真。 |
以下是一个相等运算符列表。假设变量 $a 的值为“abc”,变量 $b 的值为“xyz”,让我们来检查以下字符串相等运算符 −
Sr.No. |
Operator & Description |
1 |
lt 如果左参数在字符串上小于右参数,则返回真。 Example − ($a lt $b) 为真。 |
2 |
gt 如果左参数在字符串上大于右参数,则返回真。 Example − ($a gt $b) 为假。 |
3 |
le 如果左参数在字符串上小于或等于右参数,则返回真。 Example − ($a le $b) 为真。 |
4 |
ge 如果左参数在字符串上大于或等于右参数,则返回真。 Example − ($a ge $b) 为假。 |
5 |
eq 如果左参数在字符串上等于右参数,则返回真。 Example − ($a eq $b) 为假。 |
6 |
ne 如果左参数在字符串上不等于右参数,则返回真。 Example − ($a ne $b) 为真。 |
7 |
cmp 返回 -1、0 或 1,具体取决于左参数在字符串上分别小于、等于或大于右参数。 Example − ($a cmp $b) 为 -1。 |
Perl Assignment Operators
假设变量 $a 的值为 10,变量 $b 的值为 20,那么以下是 Perl 中可用的赋值运算符及其用法 −
Sr.No. |
Operator & Description |
1 |
= 简单的赋值运算符,将右侧操作数的值赋给左侧操作数 Example − $c = $a + $b 将 $a + $b 的值赋给 $c |
2 |
+= 加 AND 赋值运算符,它将右操作数加到左操作数上,并将结果赋给左操作数 Example − $c += $a 等同于 $c = $c + $a |
3 |
-= 减 AND 赋值运算符,它将右操作数从左操作数中减去,并将结果赋给左操作数 Example − $c -= $a 等同于 $c = $c - $a |
4 |
*= 乘 AND 赋值运算符,它将右操作数与左操作数相乘,并将结果赋给左操作数 Example − $c *= $a 等同于 $c = $c * $a |
5 |
/= 除 AND 赋值运算符,它将左操作数除以右操作数,并将结果赋给左操作数 Example − $c /= $a 等同于 $c = $c / $a |
6 |
%= 模操作符以及赋值运算符,它对两个操作数求模,并将结果分配给左操作数 Example , 即:$c %= $a 等价于 $c = $c % a |
7 |
= Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand Example − $c *= $a is equivalent to $c = $c * $a |
Perl Bitwise Operators
位运算符用于位运算上并逐位执行运算。假设 $a = 60,且 $b = 13,现在以二进制格式它们将变为以下形式:
$a = 0011 1100
$b = 0000 1101
$a&$b = 0000 1100 $a|$b = 0011 1101 $a^$b = 0011 0001 ~$a = 1100 0011 There are following Bitwise operators supported by Perl language, assume if $a = 60; and $b = 13 link:../perl/bitwise_operators_example.html[Show Example] [%autowidth] |=== |Sr.No.|Operator & Description |1|*&* Binary AND Operator copies a bit to the result if it exists in both operands. *Example* − ($a & $b) will give 12 which is 0000 1100 |2|*|* Binary OR Operator copies a bit if it exists in eather operand. *Example* − ($a | $b) will give 61 which is 0011 1101 |3|*^* Binary XOR Operator copies the bit if it is set in one operand but not both. *Example* − ($a ^ $b) will give 49 which is 0011 0001 |4|*~* Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. *Example* − (~$a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number. |5|*<<* Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. *Example* − $a << 2 will give 240 which is 1111 0000 |6|*>>* Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. *Example* − $a >> 2 will give 15 which is 0000 1111 |=== === Perl Logical Operators There are following logical operators supported by Perl language. Assume variable $a holds true and variable $b holds false then − link:../perl/logical_operators_example.html[Show Example] [%autowidth] |=== |Sr.No.|Operator & Description |1|*and* Called Logical AND operator. If both the operands are true then then condition becomes true. *Example* − ($a and $b) is false. |2|*&&* C-style Logical AND operator copies a bit to the result if it exists in both operands. *Example* − ($a && $b) is false. |3|*or* Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. *Example* − ($a or $b) is true. |4|*||* C-style Logical OR operator copies a bit if it exists in eather operand. *Example* − ($a || $b) is true. |5|*not* Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. *Example* − not($a and $b) is true. |=== === Quote-like Operators There are following Quote-like operators supported by Perl language. In the following table, a {} represents any pair of delimiters you choose. link:../perl/quote_like_operators_example.html[Show Example] [%autowidth] |=== |Sr.No.|Operator & Description |1|*q{ }* Encloses a string with-in single quotes *Example* − q{abcd} gives 'abcd' |2|*qq{ }* Encloses a string with-in double quotes *Example* − qq{abcd} gives "abcd" |3|*qx{ }* Encloses a string with-in invert quotes *Example* − qx{abcd} gives `abcd` |=== === Miscellaneous Operators There are following miscellaneous operators supported by Perl language. Assume variable a holds 10 and variable b holds 20 then − link:../perl/miscellaneous_operators_example.html[Show Example] [%autowidth] |=== |Sr.No.|Operator & Description |1|*.* Binary operator dot (.) concatenates two strings. *Example* − If $a = "abc", $b = "def" then $a.$b will give "abcdef" |2|*x* The repetition operator x returns a string consisting of the left operand repeated the number of times specified by the right operand. *Example* − ('-' x 3) will give ---. |3|*..* The range operator .. returns a list of values counting (up by ones) from the left value to the right value *Example* − (2..5) will give (2, 3, 4, 5) |4|*++* Auto Increment operator increases integer value by one *Example* − $a++ will give 11 |5|*--* Auto Decrement operator decreases integer value by one *Example* − $a-- will give 9 |6|*->* The arrow operator is mostly used in dereferencing a method or variable from an object or a class name *Example* − $obj->$a is an example to access variable $a from object $obj. |=== === Perl Operators Precedence The following table lists all operators from highest precedence to lowest. link:../perl/operators_precedence_example.html[Show Example] [source]
left terms and list operators (leftward) left → nonassoc ++ — right ** right ! ~ \ and unary + and - left =~ !~ left * / % x left + - . left << >> nonassoc named unary operators nonassoc < > ⇐ >= lt gt le ge nonassoc == != <⇒ eq ne cmp ~~ left & left | ^ left && left || // nonassoc .. … right ?: right = += -= *= etc. left , ⇒ nonassoc list operators (rightward) right not left and left or xor
== Perl - Date and Time This chapter will give you the basic understanding on how to process and manipulate dates and times in Perl. === Current Date and Time Let's start with *localtime()* function, which returns values for the current date and time if given no arguments. Following is the 9-element list returned by the *localtime* function while using in list context − [source]
sec, # seconds of minutes from 0 to 61 min, # minutes of hour from 0 to 59 hour, # hours of day from 0 to 24 mday, # day of month from 1 to 31 mon, # month of year from 0 to 11 year, # year since 1900 wday, # days since sunday yday, # days since January 1st isdst # hours of daylight savings time
Try the following example to print different elements returned by localtime() function − [source]
#!/usr/local/bin/perl
@months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); @days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); print "$mday $months[$mon] $days[$wday]\n";
When the above code is executed, it produces the following result − [source]
16 Feb Sat
If you will use localtime() function in scalar context, then it will return date and time from the current time zone set in the system. Try the following example to print current date and time in full format − [source]
#!/usr/local/bin/perl
$datestring = localtime(); print "Local date and time $datestring\n";
When the above code is executed, it produces the following result − [source]
Local date and time Sat Feb 16 06:50:45 2013
=== GMT Time The function *gmtime()* works just like localtime() function but the returned values are localized for the standard Greenwich time zone. When called in list context, $isdst, the last value returned by gmtime, is always 0. There is no Daylight Saving Time in GMT. You should make a note on the fact that localtime() will return the current local time on the machine that runs the script and gmtime() will return the universal Greenwich Mean Time, or GMT (or UTC). Try the following example to print the current date and time but on GMT scale − [source]
#!/usr/local/bin/perl
$datestring = gmtime(); print "GMT date and time $datestring\n";
When the above code is executed, it produces the following result − [source]
GMT date and time Sat Feb 16 13:50:45 2013
=== Format Date and Time You can use localtime() function to get a list of 9-elements and later you can use the *printf()* function to format date and time based on your requirements as follows − [source]
#!/usr/local/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
printf("Time Format - HH:MM:SS\n"); printf("%02d:%02d:%02d", $hour, $min, $sec);
When the above code is executed, it produces the following result − [source]
Time Format - HH:MM:SS 06:58:52
=== Epoch time You can use the time() function to get epoch time, i.e., the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970. [source]
#!/usr/local/bin/perl
$epoc = time();
print "Number of seconds since Jan 1, 1970 - $epoc\n";
When the above code is executed, it produces the following result − [source]
Number of seconds since Jan 1, 1970 - 1361022130
You can convert a given number of seconds into date and time string as follows − [source]
#!/usr/local/bin/perl
$datestring = localtime(); print "Current date and time $datestring\n";
$epoc = time(); $epoc = $epoc - 24 * 60 * 60; # one day before of current date.
$datestring = localtime($epoc); print "Yesterday’s date and time $datestring\n";
When the above code is executed, it produces the following result − [source]
Current date and time Tue Jun 5 05:54:43 2018 Yesterday’s date and time Mon Jun 4 05:54:43 2018
=== POSIX Function strftime() You can use the POSIX function *strftime()* to format date and time with the help of the following table. Please note that the specifiers marked with an asterisk (*) are locale-dependent. [%autowidth] |=== |Specifier|Replaced by|Example |%a|Abbreviated weekday name *|Thu |%A|Full weekday name *|Thursday |%b|Abbreviated month name *|Aug |%B|Full month name *|August |%c|Date and time representation *|Thu Aug 23 14:55:02 2001 |%C|Year divided by 100 and truncated to integer (00-99)|20 |%d|Day of the month, zero-padded (01-31)|23 |%D|Short MM/DD/YY date, equivalent to %m/%d/%y|08/23/01 |%e|Day of the month, space-padded ( 1-31)|23 |%F|Short YYYY-MM-DD date, equivalent to %Y-%m-%d|2001-08-23 |%g|Week-based year, last two digits (00-99)|01 |%G|Week-based year|2001 |%h|Abbreviated month name * (same as %b)|Aug |%H|Hour in 24h format (00-23)|14 |%I|Hour in 12h format (01-12)|02 |%j|Day of the year (001-366)|235 |%m|Month as a decimal number (01-12)|08 |%M|Minute (00-59)|55 |%n|New-line character ('\n')| |%p|AM or PM designation|PM |%r|12-hour clock time *|02:55:02 pm |%R|24-hour HH:MM time, equivalent to %H:%M|14:55 |%S|Second (00-61)|02 |%t|Horizontal-tab character ('\t')| |%T|ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S|14:55 |%u|ISO 8601 weekday as number with Monday as 1 (1-7)|4 |%U|Week number with the first Sunday as the first day of week one (00-53)|33 |%V|ISO 8601 week number (00-53)|34 |%w|Weekday as a decimal number with Sunday as 0 (0-6)|4 |%W|Week number with the first Monday as the first day of week one (00-53)|34 |%x|Date representation *|08/23/01 |%X|Time representation *|14:55:02 |%y|Year, last two digits (00-99)|01 |%Y|Year|2001 |%z|ISO 8601 offset from UTC in timezone (1 minute = 1, 1 hour = 100) If timezone cannot be termined, no characters |+100 |%Z|Timezone name or abbreviation * If timezone cannot be termined, no characters |CDT |%%|A % sign|% |=== Let's check the following example to understand the usage − [source]
#!/usr/local/bin/perl use POSIX qw(strftime);
$datestring = strftime "%a %b %e %H:%M:%S %Y", localtime; printf("date and time - $datestring\n");
or for GMT formatted appropriately for your locale:
$datestring = strftime "%a %b %e %H:%M:%S %Y", gmtime; printf("date and time - $datestring\n");
When the above code is executed, it produces the following result − [source]
date and time - Sat Feb 16 07:10:23 2013 date and time - Sat Feb 16 14:10:23 2013
== Perl - Subroutines A Perl subroutine or function is a group of statements that together performs a task. You can divide up your code into separate subroutines. How you divide up your code among different subroutines is up to you, but logically the division usually is so each function performs a specific task. Perl uses the terms subroutine, method and function interchangeably. === Define and Call a Subroutine The general form of a subroutine definition in Perl programming language is as follows − [source]
sub subroutine_name { body of the subroutine }
The typical way of calling that Perl subroutine is as follows − [source]
subroutine_name( list of arguments );
In versions of Perl before 5.0, the syntax for calling subroutines was slightly different as shown below. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes. [source]
&subroutine_name( list of arguments );
Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine. [source]
#!/usr/bin/perl
Function definition
sub Hello { print "Hello, World!\n"; }
Function call
Hello();
When above program is executed, it produces the following result − [source]
Hello, World!
=== Passing Arguments to a Subroutine You can pass various arguments to a subroutine like you do in any other programming language and they can be acessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on. You can pass arrays and hashes as arguments like any scalar but passing more than one array or hash normally causes them to lose their separate identities. So we will use references ( explained in the next chapter ) to pass any array or hash. Let's try the following example, which takes a list of numbers and then prints their average − [source]
#!/usr/bin/perl
Function definition
sub Average { # get total number of arguments passed. $n = scalar(@_); $sum = 0;
foreach $item (@_) { $sum += $item; } $average = $sum / $n;
print "Average for the given numbers : $average\n"; }
Function call
Average(10, 20, 30);
When above program is executed, it produces the following result − [source]
Average for the given numbers : 20
=== Passing Lists to Subroutines Because the @_ variable is an array, it can be used to supply lists to a subroutine. However, because of the way in which Perl accepts and parses lists and arrays, it can be difficult to extract the individual elements from @_. If you have to pass a list along with other scalar arguments, then make list as the last argument as shown below − [source]
#!/usr/bin/perl
Function definition
sub PrintList { my @list = @_; print "Given list is @list\n"; } $a = 10; @b = (1, 2, 3, 4);
Function call with list parameter
PrintList($a, @b);
When above program is executed, it produces the following result − [source]
Given list is 10 1 2 3 4
=== Passing Hashes to Subroutines When you supply a hash to a subroutine or operator that accepts a list, then hash is automatically translated into a list of key/value pairs. For example − [source]
#!/usr/bin/perl
Function definition
sub PrintHash { my (%hash) = @_;
foreach my $key ( keys %hash ) { my $value = $hash{$key}; print "$key : $value\n"; } } %hash = ('name' => 'Tom', 'age' => 19);
Function call with hash parameter
PrintHash(%hash);
When above program is executed, it produces the following result − [source]
name : Tom age : 19
=== Returning Value from a Subroutine You can return a value from subroutine like you do in any other programming language. If you are not returning a value from a subroutine then whatever calculation is last performed in a subroutine is automatically also the return value. You can return arrays and hashes from the subroutine like any scalar but returning more than one array or hash normally causes them to lose their separate identities. So we will use references ( explained in the next chapter ) to return any array or hash from a function. Let's try the following example, which takes a list of numbers and then returns their average − [source]
#!/usr/bin/perl
Function definition
sub Average { # get total number of arguments passed. $n = scalar(@_); $sum = 0;
foreach $item (@_) { $sum += $item; } $average = $sum / $n;
return $average; }
Function call
$num = Average(10, 20, 30); print "Average for the given numbers : $num\n";
When above program is executed, it produces the following result − [source]
Average for the given numbers : 20
=== Private Variables in a Subroutine By default, all variables in Perl are global variables, which means they can be accessed from anywhere in the program. But you can create *private* variables called *lexical variables* at any time with the *my* operator. The *my* operator confines a variable to a particular region of code in which it can be used and accessed. Outside that region, this variable cannot be used or accessed. This region is called its scope. A lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code blocks of if, while, for, foreach, and eval statements. Following is an example showing you how to define a single or multiple private variables using *my* operator − [source]
sub somefunc { my $variable; # $variable is invisible outside somefunc() my ($another, @an_array, %a_hash); # declaring many variables at once }
Let's check the following example to distinguish between global and private variables − [source]
#!/usr/bin/perl
Global variable
$string = "Hello, World!";
Function definition
sub PrintHello { # Private variable for PrintHello function my $string; $string = "Hello, Perl!"; print "Inside the function $string\n"; } # Function call PrintHello(); print "Outside the function $string\n";
When above program is executed, it produces the following result − [source]
Inside the function Hello, Perl! Outside the function Hello, World!
=== Temporary Values via local() The *local* is mostly used when the current value of a variable must be visible to called subroutines. A local just gives temporary values to global (meaning package) variables. This is known as dynamic scoping. Lexical scoping is done with my, which works more like C's auto declarations. If more than one variable or expression is given to local, they must be placed in parentheses. This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval. Let's check the following example to distinguish between global and local variables − [source]
#!/usr/bin/perl
Global variable
$string = "Hello, World!";
sub PrintHello { # Private variable for PrintHello function local $string; $string = "Hello, Perl!"; PrintMe(); print "Inside the function PrintHello $string\n"; } sub PrintMe { print "Inside the function PrintMe $string\n"; }
Function call
PrintHello(); print "Outside the function $string\n";
When above program is executed, it produces the following result − [source]
Inside the function PrintMe Hello, Perl! Inside the function PrintHello Hello, Perl! Outside the function Hello, World!
=== State Variables via state() There are another type of lexical variables, which are similar to private variables but they maintain their state and they do not get reinitialized upon multiple calls of the subroutines. These variables are defined using the *state* operator and available starting from Perl 5.9.4. Let's check the following example to demonstrate the use of *state* variables − [source]
#!/usr/bin/perl
use feature 'state';
sub PrintCount { state $count = 0; # initial value
print "Value of counter is $count\n"; $count++; }
for (1..5) { PrintCount(); }
When above program is executed, it produces the following result − [source]
Value of counter is 0 Value of counter is 1 Value of counter is 2 Value of counter is 3 Value of counter is 4
Prior to Perl 5.10, you would have to write it like this − [source]
#!/usr/bin/perl
{ my $count = 0; # initial value
sub PrintCount { print "Value of counter is $count\n"; $count++; } }
for (1..5) { PrintCount(); }
=== Subroutine Call Context The context of a subroutine or statement is defined as the type of return value that is expected. This allows you to use a single function that returns different values based on what the user is expecting to receive. For example, the following localtime() returns a string when it is called in scalar context, but it returns a list when it is called in list context. [source]
my $datestring = localtime( time );
In this example, the value of $timestr is now a string made up of the current date and time, for example, Thu Nov 30 15:21:33 2000. Conversely − [source]
($sec,$min,$hour,$mday,$mon, $year,$wday,$yday,$isdst) = localtime(time);
Now the individual variables contain the corresponding values returned by localtime() subroutine. == Perl - References A Perl reference is a scalar data type that holds the location of another value which could be scalar, arrays, or hashes. Because of its scalar nature, a reference can be used anywhere, a scalar can be used. You can construct lists containing references to other lists, which can contain references to hashes, and so on. This is how the nested data structures are built in Perl. === Create References It is easy to create a reference for any variable, subroutine or value by prefixing it with a backslash as follows − [source]
$scalarref = \$foo; $arrayref = \@ARGV; $hashref = \%ENV; $coderef = &handler; $globref = \*foo;
You cannot create a reference on an I/O handle (filehandle or dirhandle) using the backslash operator but a reference to an anonymous array can be created using the square brackets as follows − [source]
$arrayref = [1, 2, ['a', 'b', 'c']];
Similar way you can create a reference to an anonymous hash using the curly brackets as follows − [source]
$hashref = { 'Adam' ⇒ 'Eve', 'Clyde' ⇒ 'Bonnie', };
A reference to an anonymous subroutine can be created by using sub without a subname as follows − [source]
$coderef = sub { print "Boink!\n" };
=== Dereferencing Dereferencing returns the value from a reference point to the location. To dereference a reference simply use $, @ or % as prefix of the reference variable depending on whether the reference is pointing to a scalar, array, or hash. Following is the example to explain the concept − [source]
#!/usr/bin/perl
$var = 10;
Now $r has reference to $var scalar.
$r = \$var;
Print value available at the location stored in $r.
print "Value of $var is : ", $$r, "\n";
@var = (1, 2, 3); # Now $r has reference to @var array. $r = \@var; # Print values available at the location stored in $r. print "Value of @var is : ", @$r, "\n";
%var = ('key1' ⇒ 10, 'key2' ⇒ 20); # Now $r has reference to %var hash. $r = \%var; # Print values available at the location stored in $r. print "Value of %var is : ", %$r, "\n";
When above program is executed, it produces the following result − [source]
Value of 10 is : 10 Value of 1 2 3 is : 123 Value of %var is : key220key110
If you are not sure about a variable type, then its easy to know its type using *ref*, which returns one of the following strings if its argument is a reference. Otherwise, it returns false − [source]
SCALAR ARRAY HASH CODE GLOB REF
Let's try the following example − [source]
#!/usr/bin/perl
$var = 10; $r = \$var; print "Reference type in r : ", ref($r), "\n";
@var = (1, 2, 3); $r = \@var; print "Reference type in r : ", ref($r), "\n";
%var = ('key1' ⇒ 10, 'key2' ⇒ 20); $r = \%var; print "Reference type in r : ", ref($r), "\n";
When above program is executed, it produces the following result − [source]
Reference type in r : SCALAR Reference type in r : ARRAY Reference type in r : HASH
=== Circular References A circular reference occurs when two references contain a reference to each other. You have to be careful while creating references otherwise a circular reference can lead to memory leaks. Following is an example − [source]
#!/usr/bin/perl
my $foo = 100; $foo = \$foo;
print "Value of foo is : ", $$foo, "\n";
When above program is executed, it produces the following result − [source]
Value of foo is : REF(0x9aae38)
=== References to Functions This might happen if you need to create a signal handler so you can produce a reference to a function by preceding that function name with \& and to dereference that reference you simply need to prefix reference variable using ampersand &. Following is an example − [source]
#!/usr/bin/perl
Function definition
sub PrintHash { my (%hash) = @_;
foreach $item (%hash) { print "Item : $item\n"; } } %hash = ('name' => 'Tom', 'age' => 19);
Create a reference to above function.
$cref = &PrintHash;
Function call using reference.
&$cref(%hash);
When above program is executed, it produces the following result − [source]
Item : name Item : Tom Item : age Item : 19
== Perl - Formats Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data. === Define a Format Following is the syntax to define a Perl format − [source]
format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .
Here *FormatName* represents the name of the format. The *fieldline* is the specific way, the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period. Next *fieldline* can contain any text or fieldholders. The fieldholders hold space for data that will be placed there at a later date. A fieldholder has the format − [source]
@<<<<
This fieldholder is left-justified, with a field space of 5. You must count the @ sign and the < signs to know the number of spaces in the field. Other field holders include − [source]
@>>>> right-justified @|||| centered @##. numeric field holder @* multiline field holder
An example format would be − [source]
format EMPLOYEE =
@<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @.# $salary
.
In this example, $name would be written as left justify within 22 character spaces and after that age will be written in two spaces. === Using the Format In order to invoke this format declaration, we would use the *write* keyword − [source]
write EMPLOYEE;
The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function. [source]
select(STDOUT);
We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as follows − [source]
$~ = "EMPLOYEE";
When we now do a write(), the data would be sent to STDOUT. Remember: if you are going to write your report in any other file handle instead of STDOUT then you can use select() function to select that file handle and rest of the logic will remain the same. Let's take the following example. Here we have hard coded values just for showing the usage. In actual usage you will read values from a file or database to generate actual reports and you may need to write final report again into a file. [source]
#!/usr/bin/perl
format EMPLOYEE =
@<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @.# $salary
.
select(STDOUT); $~ = EMPLOYEE;
@n = ("Ali", "Raza", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000);
$i = 0; foreach (@n) { $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }
When executed, this will produce the following result − [source]
Ali 20 2000.00
Raza 30 2500.00
Jaffer 40 4000.00
=== Define a Report Header Everything looks fine. But you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable − [source]
#!/usr/bin/perl
format EMPLOYEE =
@<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @.# $salary
.
format EMPLOYEE_TOP =
Name Age
.
select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP;
@n = ("Ali", "Raza", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000);
$i = 0; foreach (@n) { $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }
Now your report will look like − [source]
Name Age
Ali 20 2000.00
Raza 30 2500.00
Jaffer 40 4000.00
=== Define a Pagination What about if your report is taking more than one page? You have a solution for that, simply use *$%* or $FORMAT_PAGE_NUMBER vairable along with header as follows − [source]
format EMPLOYEE_TOP =
Name Age Page @< $%
.
Now your output will look like as follows − [source]
Name Age Page 1
Ali 20 2000.00
Raza 30 2500.00
Jaffer 40 4000.00
=== Number of Lines on a Page You can set the number of lines per page using special variable *$=* ( or $FORMAT_LINES_PER_PAGE ), By default $= will be 60. === Define a Report Footer While $^ or $FORMAT_TOP_NAME contains the name of the current header format, there is no corresponding mechanism to automatically do the same thing for a footer. If you have a fixed-size footer, you can get footers by checking variable $- or $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary using another format defined as follows − [source]
format EMPLOYEE_BOTTOM = End of Page @< $% .
For a complete set of variables related to formating, please refer to the link:../perl/perl_special_variables.html[Perl Special Variables] section. == Perl - File I/O The basics of handling files are simple: you associate a *filehandle* with an external entity (usually a file) and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle. A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened. Three basic file handles are - *STDIN*, *STDOUT*, and *STDERR,* which represent standard input, standard output and standard error devices respectively. === Opening and Closing Files There are following two functions with multiple forms, which can be used to open any new or existing file in Perl. [source]
open FILEHANDLE, EXPR open FILEHANDLE
sysopen FILEHANDLE, FILENAME, MODE, PERMS sysopen FILEHANDLE, FILENAME, MODE
Here FILEHANDLE is the file handle returned by the *open* function and EXPR is the expression having file name and mode of opening the file. === Open Function Following is the syntax to open *file.txt* in read-only mode. Here less than < sign indicates that file has to be opend in read-only mode. [source]
open(DATA, "<file.txt");
Here DATA is the file handle, which will be used to read the file. Here is the example, which will open a file and will print its content over the screen. [source]
#!/usr/bin/perl
open(DATA, "<file.txt") or die "Couldn’t open file file.txt, $!";
while(<DATA>) { print "$_"; }
Following is the syntax to open file.txt in writing mode. Here less than > sign indicates that file has to be opend in the writing mode. [source]
open(DATA, ">file.txt") or die "Couldn’t open file file.txt, $!";
This example actually truncates (empties) the file before opening it for writing, which may not be the desired effect. If you want to open a file for reading and writing, you can put a plus sign before the > or < characters. For example, to open a file for updating without truncating it − [source]
open(DATA, "+<file.txt"); or die "Couldn’t open file file.txt, $!";
To truncate the file first − [source]
open DATA, "+>file.txt" or die "Couldn’t open file file.txt, $!";
You can open a file in the append mode. In this mode, writing point will be set to the end of the file. [source]
open(DATA,">>file.txt") || die "Couldn’t open file file.txt, $!";
A double >> opens the file for appending, placing the file pointer at the end, so that you can immediately start appending information. However, you can't read from it unless you also place a plus sign in front of it − [source]
open(DATA,"+>>file.txt") || die "Couldn’t open file file.txt, $!";
Following is the table, which gives the possible values of different modes [%autowidth] |=== |Sr.No.|Entities & Definition |1|*< or r* Read Only Access |2|*> or w* Creates, Writes, and Truncates |3|*>> or a* Writes, Appends, and Creates |4|*+< or r+* Reads and Writes |5|*+> or w+* Reads, Writes, Creates, and Truncates |6|*+>> or a+* Reads, Writes, Appends, and Creates |=== === Sysopen Function The *sysopen* function is similar to the main open function, except that it uses the system *open()* function, using the parameters supplied to it as the parameters for the system function − For example, to open a file for updating, emulating the *+<filename* format from open − [source]
sysopen(DATA, "file.txt", O_RDWR);
Or to truncate the file before updating − [source]
sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );
You can use O_CREAT to create a new file and O_WRONLY- to open file in write only mode and O_RDONLY - to open file in read only mode. The *PERMS* argument specifies the file permissions for the file specified, if it has to be created. By default it takes *0x666*. Following is the table, which gives the possible values of MODE. [%autowidth] |=== |Sr.No.|Entities & Definition |1|*O_RDWR* Read and Write |2|*O_RDONLY* Read Only |3|*O_WRONLY* Write Only |4|*O_CREAT* Create the file |5|*O_APPEND* Append the file |6|*O_TRUNC* Truncate the file |7|*O_EXCL* Stops if file already exists |8|*O_NONBLOCK* Non-Blocking usability |=== === Close Function To close a filehandle, and therefore disassociate the filehandle from the corresponding file, you use the *close* function. This flushes the filehandle's buffers and closes the system's file descriptor. [source]
close FILEHANDLE close
If no FILEHANDLE is specified, then it closes the currently selected filehandle. It returns true only if it could successfully flush the buffers and close the file. [source]
close(DATA) || die "Couldn’t close file properly";
=== Reading and Writing Files Once you have an open filehandle, you need to be able to read and write information. There are a number of different ways of reading and writing data into the file. === The <FILEHANDL> Operator The main method of reading the information from an open filehandle is the <FILEHANDLE> operator. In a scalar context, it returns a single line from the filehandle. For example − [source]
#!/usr/bin/perl
print "What is your name?\n"; $name = <STDIN>; print "Hello $name\n";
When you use the <FILEHANDLE> operator in a list context, it returns a list of lines from the specified filehandle. For example, to import all the lines from a file into an array − [source]
#!/usr/bin/perl
open(DATA,"<import.txt") or die "Can’t open data"; @lines = <DATA>; close(DATA);
=== getc Function The getc function returns a single character from the specified FILEHANDLE, or STDIN if none is specified − [source]
getc FILEHANDLE getc
If there was an error, or the filehandle is at end of file, then undef is returned instead. === read Function The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file. [source]
read FILEHANDLE, SCALAR, LENGTH, OFFSET read FILEHANDLE, SCALAR, LENGTH
The length of the data read is defined by LENGTH, and the data is placed at the start of SCALAR if no OFFSET is specified. Otherwise data is placed after OFFSET bytes in SCALAR. The function returns the number of bytes read on success, zero at end of file, or undef if there was an error. === print Function For all the different methods used for reading information from filehandles, the main function for writing information back is the print function. [source]
print FILEHANDLE LIST print LIST print
The print function prints the evaluated value of LIST to FILEHANDLE, or to the current output filehandle (STDOUT by default). For example − [source]
print "Hello World!\n";
=== Copying Files Here is the example, which opens an existing file file1.txt and read it line by line and generate another copy file file2.txt. [source]
#!/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 );
=== Renaming a file Here is an example, which shows how we can rename a file file1.txt to file2.txt. Assuming file is available in /usr/test directory. [source]
#!/usr/bin/perl
rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );
This function *renames* takes two arguments and it just renames the existing file. === Deleting an Existing File Here is an example, which shows how to delete a file file1.txt using the *unlink* function. [source]
#!/usr/bin/perl
unlink ("/usr/test/file1.txt");
=== Positioning inside a File You can use to *tell* function to know the current position of a file and *seek* function to point a particular position inside the file. ==== tell Function The first requirement is to find your position within a file, which you do using the tell function − [source]
tell FILEHANDLE tell
This returns the position of the file pointer, in bytes, within FILEHANDLE if specified, or the current default selected filehandle if none is specified. ==== seek Function The seek function positions the file pointer to the specified number of bytes within a file − [source]
seek FILEHANDLE, POSITION, WHENCE
The function uses the fseek system function, and you have the same ability to position relative to three different points: the start, the end, and the current position. You do this by specifying a value for WHENCE. Zero sets the positioning relative to the start of the file. For example, the line sets the file pointer to the 256th byte in the file. [source]
seek DATA, 256, 0;
=== File Information You can test certain features very quickly within Perl using a series of test operators known collectively as -X tests. For example, to perform a quick test of the various permissions on a file, you might use a script like this − [source]
#/usr/bin/perl
my $file = "/usr/test/file1.txt"; my (@description, $size); if (-e $file) { push @description, 'binary' if (-B _); push @description, 'a socket' if (-S _); push @description, 'a text file' if (-T _); push @description, 'a block special file' if (-b _); push @description, 'a character special file' if (-c _); push @description, 'a directory' if (-d _); push @description, 'executable' if (-x _); push @description, $size = -s _ ? "$size bytes" : 'empty'; print "$file is ", join(', ',@description),"\n"; }
Here is the list of features, which you can check for a file or directory − [%autowidth] |=== |Sr.No.|Operator & Definition |1|*-A* Script start time minus file last access time, in days. |2|*-B* Is it a binary file? |3|*-C* Script start time minus file last inode change time, in days. |3|*-M* Script start time minus file modification time, in days. |4|*-O* Is the file owned by the real user ID? |5|*-R* Is the file readable by the real user ID or real group? |6|*-S* Is the file a socket? |7|*-T* Is it a text file? |8|*-W* Is the file writable by the real user ID or real group? |9|*-X* Is the file executable by the real user ID or real group? |10|*-b* Is it a block special file? |11|*-c* Is it a character special file? |12|*-d* Is the file a directory? |13|*-e* Does the file exist? |14|*-f* Is it a plain file? |15|*-g* Does the file have the setgid bit set? |16|*-k* Does the file have the sticky bit set? |17|*-l* Is the file a symbolic link? |18|*-o* Is the file owned by the effective user ID? |19|*-p* Is the file a named pipe? |20|*-r* Is the file readable by the effective user or group ID? |21|*-s* Returns the size of the file, zero size = empty file. |22|*-t* Is the filehandle opened by a TTY (terminal)? |23|*-u* Does the file have the setuid bit set? |24|*-w* Is the file writable by the effective user or group ID? |25|*-x* Is the file executable by the effective user or group ID? |26|*-z* Is the file size zero? |=== == Perl - Directories Following are the standard functions used to play with directories. [source]
opendir DIRHANDLE, EXPR # To open a directory readdir DIRHANDLE # To read a directory rewinddir DIRHANDLE # Positioning pointer to the begining telldir DIRHANDLE # Returns current position of the dir seekdir DIRHANDLE, POS # Pointing pointer to POS inside dir closedir DIRHANDLE # Closing a directory.
=== Display all the Files There are various ways to list down all the files available in a particular directory. First let's use the simple way to get and list down all the files using the *glob* operator − [source]
#!/usr/bin/perl
Display all the files in /tmp directory.
$dir = "/tmp/*"; my @files = glob( $dir );
foreach (@files ) { print $_ . "\n"; }
Display all the C source files in /tmp directory.
$dir = "/tmp/*.c"; @files = glob( $dir );
foreach (@files ) { print $_ . "\n"; }
Display all the hidden files.
$dir = "/tmp/.*"; @files = glob( $dir ); foreach (@files ) { print $_ . "\n"; }
Display all the files from /tmp and /home directories.
$dir = "/tmp/* /home/*"; @files = glob( $dir );
foreach (@files ) { print $_ . "\n"; }
Here is another example, which opens a directory and list out all the files available inside this directory. [source]
#!/usr/bin/perl
opendir (DIR, '.') or die "Couldn’t open directory, $!"; while ($file = readdir DIR) { print "$file\n"; } closedir DIR;
One more example to print the list of C source files you might use is − [source]
#!/usr/bin/perl
opendir(DIR, '.') or die "Couldn’t open directory, $!"; foreach (sort grep(/^.*\.c$/,readdir(DIR))) { print "$_\n"; } closedir DIR;
=== Create new Directory You can use *mkdir* function to create a new directory. You will need to have the required permission to create a directory. [source]
#!/usr/bin/perl
$dir = "/tmp/perl";
This creates perl directory in /tmp directory.
mkdir( $dir ) or die "Couldn’t create $dir directory, $!"; print "Directory created successfully\n";
=== Remove a directory You can use *rmdir* function to remove a directory. You will need to have the required permission to remove a directory. Additionally this directory should be empty before you try to remove it. [source]
#!/usr/bin/perl
$dir = "/tmp/perl";
This removes perl directory from /tmp directory.
rmdir( $dir ) or die "Couldn’t remove $dir directory, $!"; print "Directory removed successfully\n";
=== Change a Directory You can use *chdir* function to change a directory and go to a new location. You will need to have the required permission to change a directory and go inside the new directory. [source]
#!/usr/bin/perl
$dir = "/home";
This changes perl directory and moves you inside /home directory.
chdir( $dir ) or die "Couldn’t go inside $dir directory, $!"; print "Your new location is $dir\n";
== Perl - Error Handling The execution and the errors always go together. If you are opening a file which does not exist. then if you did not handle this situation properly then your program is considered to be of bad quality. The program stops if an error occurs. So a proper error handling is used to handle various type of errors, which may occur during a program execution and take appropriate action instead of halting program completely. You can identify and trap an error in a number of different ways. Its very easy to trap errors in Perl and then handling them properly. Here are few methods which can be used. === The if statement The *if statement* is the obvious choice when you need to check the return value from a statement; for example − [source]
if(open(DATA, $file)) { … } else { die "Error: Couldn’t open the file - $!"; }
Here variable $! returns the actual error message. Alternatively, we can reduce the statement to one line in situations where it makes sense to do so; for example − [source]
open(DATA, $file) || die "Error: Couldn’t open the file $!";
=== The unless Function The *unless* function is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false. For example − [source]
unless(chdir("/etc")) { die "Error: Can’t change directory - $!"; }
The *unless* statement is best used when you want to raise an error or alternative only if the expression fails. The statement also makes sense when used in a single-line statement − [source]
die "Error: Can’t change directory!: $!" unless(chdir("/etc"));
Here we die only if the chdir operation fails, and it reads nicely. === The ternary Operator For very short tests, you can use the conditional operator *?:* [source]
print(exists($hash{value}) ? 'There' : 'Missing',"\n");
It's not quite so clear here what we are trying to achieve, but the effect is the same as using an *if* or *unless* statement. The conditional operator is best used when you want to quickly return one of the two values within an expression or statement. === The warn Function The warn function just raises a warning, a message is printed to STDERR, but no further action is taken. So it is more useful if you just want to print a warning for the user and proceed with rest of the operation − [source]
chdir('/etc') or warn "Can’t change directory";
=== The die Function The die function works just like warn, except that it also calls exit. Within a normal script, this function has the effect of immediately terminating execution. You should use this function in case it is useless to proceed if there is an error in the program − [source]
chdir('/etc') or die "Can’t change directory";
=== Errors within Modules There are two different situations we should be able to handle − . Reporting an error in a module that quotes the module's filename and line number - this is useful when debugging a module, or when you specifically want to raise a module-related, rather than script-related, error. . Reporting an error within a module that quotes the caller's information so that you can debug the line within the script that caused the error. Errors raised in this fashion are useful to the end-user, because they highlight the error in relation to the calling script's origination line. The *warn* and *die* functions work slightly differently than you would expect when called from within a module. For example, the simple module − [source]
package T;
require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp;
sub function { warn "Error in module!"; } 1;
When called from a script like below − [source]
use T; function();
It will produce the following result − [source]
Error in module! at T.pm line 9.
This is more or less what you might expected, but not necessarily what you want. From a module programmer's perspective, the information is useful because it helps to point to a bug within the module itself. For an end-user, the information provided is fairly useless, and for all but the hardened programmer, it is completely pointless. The solution for such problems is the Carp module, which provides a simplified method for reporting errors within modules that return information about the calling script. The Carp module provides four functions: carp, cluck, croak, and confess. These functions are discussed below. === The carp Function The carp function is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name. [source]
package T;
require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp;
sub function { carp "Error in module!"; } 1;
When called from a script like below − [source]
use T; function();
It will produce the following result − [source]
Error in module! at test.pl line 4
=== The cluck Function The cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including the information on the original script. [source]
package T;
require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp qw(cluck);
sub function { cluck "Error in module!"; } 1;
When called from a script like below − [source]
use T; function();
It will produce the following result − [source]
Error in module! at T.pm line 9 T::function() called at test.pl line 4
=== The croak Function The *croak* function is equivalent to *die*, except that it reports the caller one level up. Like die, this function also exits the script after reporting the error to STDERR − [source]
package T;
require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp;
sub function { croak "Error in module!"; } 1;
When called from a script like below − [source]
use T; function();
It will produce the following result − [source]
Error in module! at test.pl line 4
As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions. === The confess Function The *confess* function is like *cluck*; it calls die and then prints a stack trace all the way up to the origination script. [source]
package T;
require Exporter; @ISA = qw/Exporter/; @EXPORT = qw/function/; use Carp;
sub function { confess "Error in module!"; } 1;
When called from a script like below − [source]
use T; function();
It will produce the following result − [source]
Error in module! at T.pm line 9 T::function() called at test.pl line 4
== Perl - Special Variables There are some variables which have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_ ( explained below ). Most of the special variables have an english like long name, e.g., Operating System Error variable $! can be written as $OS_ERROR. But if you are going to use english like names, then you would have to put one line *use English;* at the top of your program file. This guides the interpreter to pickup exact meaning of the variable. The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − [source]
#!/usr/bin/perl
foreach ('hickory','dickory','doc') { print $_; print "\n"; }
When executed, this will produce the following result − [source]
hickory dickory doc
Again, let's check the same example without using $_ variable explicitly − [source]
#!/usr/bin/perl
foreach ('hickory','dickory','doc') { print; print "\n"; }
When executed, this will also produce the following result − [source]
hickory dickory doc
The first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed in $_, and is used by default by print. Here are the places where Perl will assume $_ even if you don't specify it − . Various unary functions, including functions like ord and int, as well as the all file tests (-f, -d) except for -t, which defaults to STDIN. . Various list functions like print and unlink. . The pattern-matching operations m//, s///, and tr/// when used without an =~ operator. . The default iterator variable in a foreach loop if no other variable is supplied. . The implicit iterator variable in the grep and map functions. . The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a while test (i.e., ). Note that outside of a while test, this will not happen. === Special Variable Types Based on the usage and nature of special variables, we can categorize them in the following categories − . Global Scalar Special Variables. . Global Array Special Variables. . Global Hash Special Variables. . Global Special Filehandles. . Global Special Constants. . Regular Expression Special Variables. . Filehandle Special Variables. === Global Scalar Special Variables Here is the list of all the scalar special variables. We have listed corresponding english like names along with the symbolic names. [%autowidth] |=== |$_|The default input and pattern-searching space. |$ARG |$.|The current input line number of the last filehandle that was read. An explicit close on the filehandle resets the line number. |$NR |$/|The input record separator; newline by default. If set to the null string, it treats blank lines as delimiters. |$RS |$,|The output field separator for the print operator. |$OFS |$\|The output record separator for the print operator. |$ORS |$"|Like "$," except that it applies to list values interpolated into a double-quoted string (or similar interpreted string). Default is a space. |$LIST_SEPARATOR |$;|The subscript separator for multidimensional array emulation. Default is "\034". |$SUBSCRIPT_SEPARATOR |$^L|What a format outputs to perform a formfeed. Default is "\f". |$FORMAT_FORMFEED |$:|The current set of characters after which a string may be broken to fill continuation fields (starting with ^) in a format. Default is "\n"". |$FORMAT_LINE_BREAK_CHARACTERS |$^A|The current value of the write accumulator for format lines. |$ACCUMULATOR |$#|Contains the output format for printed numbers (deprecated). |$OFMT |$?|The status returned by the last pipe close, backtick (``) command, or system operator. |$CHILD_ERROR |$!|If used in a numeric context, yields the current value of the errno variable, identifying the last system call error. If used in a string context, yields the corresponding system error string. |$OS_ERROR or $ERRNO |$@|The Perl syntax error message from the last eval command. |$EVAL_ERROR |$$|The pid of the Perl process running this script. |$PROCESS_ID or $PID |$<|The real user ID (uid) of this process. |$REAL_USER_ID or $UID |$>|The effective user ID of this process. |$EFFECTIVE_USER_ID or $EUID |$(|The real group ID (gid) of this process. |$REAL_GROUP_ID or $GID |$)|The effective gid of this process. |$EFFECTIVE_GROUP_ID or $EGID |$0|Contains the name of the file containing the Perl script being executed. |$PROGRAM_NAME |$[|The index of the first element in an array and of the first character in a substring. Default is 0. |$]|Returns the version plus patchlevel divided by 1000. |$PERL_VERSION |$^D|The current value of the debugging flags. |$DEBUGGING |$^E|Extended error message on some platforms. |$EXTENDED_OS_ERROR |$^F|The maximum system file descriptor, ordinarily 2. |$SYSTEM_FD_MAX |$^H|Contains internal compiler hints enabled by certain pragmatic modules. |$^I|The current value of the inplace-edit extension. Use undef to disable inplace editing. |$INPLACE_EDIT |$^M|The contents of $M can be used as an emergency memory pool in case Perl dies with an out-of-memory error. Use of $M requires a special compilation of Perl. See the INSTALL document for more information. |$^O|Contains the name of the operating system that the current Perl binary was compiled for. |$OSNAME |$^P|The internal flag that the debugger clears so that it doesn't debug itself. |$PERLDB |$^T|The time at which the script began running, in seconds since the epoch. |$BASETIME |$^W|The current value of the warning switch, either true or false. |$WARNING |$^X|The name that the Perl binary itself was executed as. |$EXECUTABLE_NAME |$ARGV|Contains the name of the current file when reading from <ARGV>. |=== === Global Array Special Variables [%autowidth] |=== |@ARGV|The array containing the command-line arguments intended for the script. |@INC|The array containing the list of places to look for Perl scripts to be evaluated by the do, require, or use constructs. |@F|The array into which the input lines are split when the -a command-line switch is given. |=== === Global Hash Special Variables [%autowidth] |=== |%INC|The hash containing entries for the filename of each file that has been included via do or require. |%ENV|The hash containing your current environment. |%SIG|The hash used to set signal handlers for various signals. |=== === Global Special Filehandles [%autowidth] |=== |ARGV|The special filehandle that iterates over command line filenames in @ARGV. Usually written as the null filehandle in <>. |STDERR|The special filehandle for standard error in any package. |STDIN|The special filehandle for standard input in any package. |STDOUT|The special filehandle for standard output in any package. |DATA|The special filehandle that refers to anything following the __END__ token in the file containing the script. Or, the special filehandle for anything following the __DATA__ token in a required file, as long as you're reading data in the same package __DATA__ was found in. |_ (underscore)|The special filehandle used to cache the information from the last stat, lstat, or file test operator. |=== === Global Special Constants [%autowidth] |=== |__END__|Indicates the logical end of your program. Any following text is ignored, but may be read via the DATA filehandle. |__FILE__|Represents the filename at the point in your program where it's used. Not interpolated into strings. |__LINE__|Represents the current line number. Not interpolated into strings. |__PACKAGE__|Represents the current package name at compile time, or undefined if there is no current package. Not interpolated into strings. |=== === Regular Expression Special Variables [%autowidth] |=== |$digit|Contains the text matched by the corresponding set of parentheses in the last pattern matched. For example, $1 matches whatever was contained in the first set of parentheses in the previous regular expression. |$&|The string matched by the last successful pattern match. |$MATCH |$`|The string preceding whatever was matched by the last successful pattern match. |$PREMATCH |$'|The string following whatever was matched by the last successful pattern match. |$POSTMATCH |$+|The last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns was matched. For example : /Version: (.*)|Revision: (.*)/ && ($rev = $+); |$LAST_PAREN_MATCH |=== === Filehandle Special Variables [%autowidth] |=== |$||If set to nonzero, forces an fflush(3) after every write or print on the currently selected output channel. |$OUTPUT_AUTOFLUSH |$%|The current page number of the currently selected output channel. |$FORMAT_PAGE_NUMBER |$=|The current page length (printable lines) of the currently selected output channel. Default is 60. |$FORMAT_LINES_PER_PAGE |$-|The number of lines left on the page of the currently selected output channel. |$FORMAT_LINES_LEFT |$~|The name of the current report format for the currently selected output channel. Default is the name of the filehandle. |$FORMAT_NAME |$^|The name of the current top-of-page format for the currently selected output channel. Default is the name of the filehandle with _TOP appended. |$FORMAT_TOP_NAME |=== == Perl - Coding Standard Each programmer will, of course, have his or her own preferences in regards to formatting, but there are some general guidelines that will make your programs easier to read, understand, and maintain. The most important thing is to run your programs under the -w flag at all times. You may turn it off explicitly for particular portions of code via the no warnings pragma or the $^W variable if you must. You should also always run under use strict or know the reason why not. The use sigtrap and even use diagnostics pragmas may also prove useful. Regarding aesthetics of code lay out, about the only thing Larry cares strongly about is that the closing curly bracket of a multi-line BLOCK should line up with the keyword that started the construct. Beyond that, he has other preferences that aren't so strong − . 4-column indent. . Opening curly on same line as keyword, if possible, otherwise line up. . Space before the opening curly of a multi-line BLOCK. . One-line BLOCK may be put on one line, including curlies. . No space before the semicolon. . Semicolon omitted in "short" one-line BLOCK. . Space around most operators. . Space around a "complex" subscript (inside brackets). . Blank lines between chunks that do different things. . Uncuddled elses. . No space between function name and its opening parenthesis. . Space after each comma. . Long lines broken after an operator (except and and or). . Space after last parenthesis matching on current line. . Line up corresponding items vertically. . Omit redundant punctuation as long as clarity doesn't suffer. Here are some other more substantive style issues to think about: Just because you CAN do something a particular way doesn't mean that you SHOULD do it that way. Perl is designed to give you several ways to do anything, so consider picking the most readable one. For instance − [source]
open(FOO,$foo) || die "Can’t open $foo: $!";
Is better than − [source]
die "Can’t open $foo: $!" unless open(FOO,$foo);
Because the second way hides the main point of the statement in a modifier. On the other hand, [source]
print "Starting analysis\n" if $verbose;
Is better than − [source]
$verbose && print "Starting analysis\n";
Because the main point isn't whether the user typed -v or not. Don't go through silly contortions to exit a loop at the top or the bottom, when Perl provides the last operator so you can exit in the middle. Just "outdent" it a little to make it more visible − [source]
LINE: for (;;) { statements; last LINE if $foo; next LINE if /^#/; statements; }
Let's see few more important points − . Don't be afraid to use loop labels--they're there to enhance readability as well as to allow multilevel loop breaks. See the previous example. . Avoid using grep() (or map()) or `backticks` in a void context, that is, when you just throw away their return values. Those functions all have return values, so use them. Otherwise use a foreach() loop or the system() function instead. . For portability, when using features that may not be implemented on every machine, test the construct in an eval to see if it fails. If you know what version or patchlevel a particular feature was implemented, you can test $] ($PERL_VERSION in English) to see if it will be there. The Config module will also let you interrogate values determined by the Configure program when Perl was installed. . Choose mnemonic identifiers. If you can't remember what mnemonic means, you've got a problem. . While short identifiers like $gotit are probably ok, use underscores to separate words in longer identifiers. It is generally easier to read $var_names_like_this than $VarNamesLikeThis, especially for non-native speakers of English. It's also a simple rule that works consistently with VAR_NAMES_LIKE_THIS. . Package names are sometimes an exception to this rule. Perl informally reserves lowercase module names for "pragma" modules like integer and strict. Other modules should begin with a capital letter and use mixed case, but probably without underscores due to limitations in primitive file systems' representations of module names as files that must fit into a few sparse bytes. . If you have a really hairy regular expression, use the /x modifier and put in some whitespace to make it look a little less like line noise. Don't use slash as a delimiter when your regexp has slashes or backslashes. . Always check the return codes of system calls. Good error messages should go to STDERR, include which program caused the problem, what the failed system call and arguments were, and (VERY IMPORTANT) should contain the standard system error message for what went wrong. Here's a simple but sufficient example − [source]
opendir(D, $dir) or die "can’t opendir $dir: $!";
. Think about reusability. Why waste brainpower on a one-shot when you might want to do something like it again? Consider generalizing your code. Consider writing a module or object class. Consider making your code run cleanly with use strict and use warnings (or -w) in effect. Consider giving away your code. Consider changing your whole world view. Consider... oh, never mind. . Be consistent. . Be nice. == Perl - Regular Expressions A regular expression is a string of characters that defines the pattern or patterns you are viewing. The syntax of regular expressions in Perl is very similar to what you will find within other regular expression.supporting programs, such as *sed*, *grep*, and *awk*. The basic method for applying a regular expression is to use the pattern binding operators =~ and *!*~. The first operator is a test and assignment operator. There are three regular expression operators within Perl. . Match Regular Expression - m// . Substitute Regular Expression - s/// . Transliterate Regular Expression - tr/// The forward slashes in each case act as delimiters for the regular expression (regex) that you are specifying. If you are comfortable with any other delimiter, then you can use in place of forward slash. === The Match Operator The match operator, m//, is used to match a string or statement to a regular expression. For example, to match the character sequence "foo" against the scalar $bar, you might use a statement like this − [source]
#!/usr/bin/perl
$bar = "This is foo and again foo"; if ($bar =~ /foo/) { print "First time is matching\n"; } else { print "First time is not matching\n"; }
$bar = "foo"; if ($bar =~ /foo/) { print "Second time is matching\n"; } else { print "Second time is not matching\n"; }
When above program is executed, it produces the following result − [source]
First time is matching Second time is matching
The m// actually works in the same fashion as the q// operator series.you can use any combination of naturally matching characters to act as delimiters for the expression. For example, m{}, m(), and m>< are all valid. So above example can be re-written as follows − [source]
#!/usr/bin/perl
$bar = "This is foo and again foo"; if ($bar =~ m[foo]) { print "First time is matching\n"; } else { print "First time is not matching\n"; }
$bar = "foo"; if ($bar =~ m{foo}) { print "Second time is matching\n"; } else { print "Second time is not matching\n"; }
You can omit m from m// if the delimiters are forward slashes, but for all other delimiters you must use the m prefix. Note that the entire match expression, that is the expression on the left of =~ or !~ and the match operator, returns true (in a scalar context) if the expression matches. Therefore the statement − [source]
$true = ($foo =~ m/foo/);
will set $true to 1 if $foo matches the regex, or 0 if the match fails. In a list context, the match returns the contents of any grouped expressions. For example, when extracting the hours, minutes, and seconds from a time string, we can use − [source]
my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);
=== Match Operator Modifiers The match operator supports its own set of modifiers. The /g modifier allows for global matching. The /i modifier will make the match case insensitive. Here is the complete list of modifiers [%autowidth] |=== |Sr.No.|Modifier & Description |1|*i* Makes the match case insensitive. |2|*m* Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary. |3|*o* Evaluates the expression only once. |4|*s* Allows use of . to match a newline character. |5|*x* Allows you to use white space in the expression for clarity. |6|*g* Globally finds all matches. |7|*cg* Allows the search to continue even after a global match fails. |=== === Matching Only Once There is also a simpler version of the match operator - the ?PATTERN? operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset. For example, you can use this to get the first and last elements within a list − [source]
#!/usr/bin/perl
@list = qw/food foosball subeo footnote terfoot canic footbrdige/;
foreach (@list) { $first = $1 if /(foo.?)/; $last = $1 if /(foo.)/; } print "First: $first, Last: $last\n";
When above program is executed, it produces the following result − [source]
First: foo, Last: footbrdige
=== Regular Expression Variables Regular expression variables include *$*, which contains whatever the last grouping match matched; *$&*, which contains the entire matched string; *$`*, which contains everything before the matched string; and *$'*, which contains everything after the matched string. Following code demonstrates the result − [source]
#!/usr/bin/perl
$string = "The food is in the salad bar"; $string =~ m/foo/; print "Before: $`\n"; print "Matched: $&\n"; print "After: $'\n";
When above program is executed, it produces the following result − [source]
Before: The Matched: foo After: d is in the salad bar
=== The Substitution Operator The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − [source]
s/PATTERN/REPLACEMENT/;
The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of *dog* with *cat* using the following regular expression − [source]
#/user/bin/perl
$string = "The cat sat on the mat"; $string =~ s/cat/dog/;
print "$string\n";
When above program is executed, it produces the following result − [source]
The dog sat on the mat
=== Substitution Operator Modifiers Here is the list of all the modifiers used with substitution operator. [%autowidth] |=== |Sr.No.|Modifier & Description |1|*i* Makes the match case insensitive. |2|*m* Specifies that if the string has newline or carriage return characters, the ^ and $ operators will now match against a newline boundary, instead of a string boundary. |3|*o* Evaluates the expression only once. |4|*s* Allows use of . to match a newline character. |5|*x* Allows you to use white space in the expression for clarity. |6|*g* Replaces all occurrences of the found expression with the replacement text. |7|*e* Evaluates the replacement as if it were a Perl statement, and uses its return value as the replacement text. |=== === The Translation Operator Translation is similar, but not identical, to the principles of substitution, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are − [source]
tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds
The translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been using in this chapter − [source]
#/user/bin/perl
$string = 'The cat sat on the mat'; $string =~ tr/a/o/;
print "$string\n";
When above program is executed, it produces the following result − [source]
The cot sot on the mot.
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 the following syntax in place of the *uc* function. [source]
$string =~ tr/a-z/A-Z/;
=== Translation Operator Modifiers Following is the list of operators related to translation. [%autowidth] |=== |Sr.No.|Modifier & Description |1|*c* Complements SEARCHLIST. |2|*d* Deletes found but unreplaced characters. |3|*s* Squashes duplicate replaced characters. |=== The /d modifier deletes the characters matching SEARCHLIST that do not have a corresponding entry in REPLACEMENTLIST. For example − [source]
#!/usr/bin/perl
$string = 'the cat sat on the mat.'; $string =~ tr/a-z/b/d;
print "$string\n";
When above program is executed, it produces the following result − [source]
b b b.
The last modifier, /s, removes the duplicate sequences of characters that were replaced, so − [source]
#!/usr/bin/perl
$string = 'food'; $string = 'food'; $string =~ tr/a-z/a-z/s;
print "$string\n";
When above program is executed, it produces the following result − [source]
fod
=== More Complex Regular Expressions You don't just have to match on fixed strings. In fact, you can match on just about anything you could dream of by using more complex regular expressions. Here's a quick cheat sheet − Following table lists the regular expression syntax that is available in Python. [%autowidth] |=== |Sr.No.|Pattern & Description |1|*^* Matches beginning of line. |2|*$* Matches end of line. |3|*.* Matches any single character except newline. Using m option allows it to match newline as well. |4|*[...]* Matches any single character in brackets. |5|*[^...]* Matches any single character not in brackets. |6|*** Matches 0 or more occurrences of preceding expression. |7|*+* Matches 1 or more occurrence of preceding expression. |8|*?* Matches 0 or 1 occurrence of preceding expression. |9|*{ n}* Matches exactly n number of occurrences of preceding expression. |10|*{ n,}* Matches n or more occurrences of preceding expression. |11|*{ n, m}* Matches at least n and at most m occurrences of preceding expression. |12|*a| b* Matches either a or b. |13|*\w* Matches word characters. |14|*\W* Matches nonword characters. |15|*\s* Matches whitespace. Equivalent to [\t\n\r\f]. |16|*\S* Matches nonwhitespace. |17|*\d* Matches digits. Equivalent to [0-9]. |18|*\D* Matches nondigits. |19|*\A* Matches beginning of string. |20|*\Z* Matches end of string. If a newline exists, it matches just before newline. |21|*\z* Matches end of string. |22|*\G* Matches point where last match finished. |23|*\b* Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |24|*\B* Matches nonword boundaries. |25|*\n, \t, etc.* Matches newlines, carriage returns, tabs, etc. |26|*\1...\9* Matches nth grouped subexpression. |27|*\10* Matches nth grouped subexpression if it matched already. Otherwise refers to the octal representation of a character code. |28|*[aeiou]* Matches a single character in the given set |29|*[^aeiou]* Matches a single character outside the given set |=== The ^ metacharacter matches the beginning of the string and the $ metasymbol matches the end of the string. Here are some brief examples. [source]
nothing in the string (start and end are adjacent)
/^$/
a three digits, each followed by a whitespace
character (eg "3 4 5 ")
/(\d\s) {3}/
matches a string in which every
odd-numbered letter is a (eg "abacadaf")
/(a.)+/
string starts with one or more digits
/^\d+/
string that ends with one or more digits
/\d+$/
Lets have a look at another example. [source]
#!/usr/bin/perl
$string = "Cats go Catatonic\nWhen given Catnip"; ($start) = ($string =~ /\A(.?) /); @lines = $string =~ /^(.?) /gm; print "First word: $start\n","Line starts: @lines\n";
When above program is executed, it produces the following result − [source]
First word: Cats Line starts: Cats When
=== Matching Boundaries The *\b* matches at any word boundary, as defined by the difference between the \w class and the \W class. Because \w includes the characters for a word, and \W the opposite, this normally means the termination of a word. The *\B* assertion matches any position that is not a word boundary. For example − [source]
/\bcat\b/ # Matches 'the cat sat' but not 'cat on the mat' /\Bcat\B/ # Matches 'verification' but not 'the cat on the mat' /\bcat\B/ # Matches 'catatonic' but not 'polecat' /\Bcat\b/ # Matches 'polecat' but not 'catatonic'
=== Selecting Alternatives The | character is just like the standard or bitwise OR within Perl. It specifies alternate matches within a regular expression or group. For example, to match "cat" or "dog" in an expression, you might use this − [source]
if ($string =~ /cat|dog/)
You can group individual elements of an expression together in order to support complex matches. Searching for two people’s names could be achieved with two separate tests, like this − [source]
if $string =~ /Martin Brown/) || ($string =~ /Sharon Brown/
This could be written as follows
if ($string =~ /(Martin|Sharon) Brown/)
=== Grouping Matching From a regular-expression point of view, there is no difference between except, perhaps, that the former is slightly clearer. [source]
$string =~ /(\S+)\s+(\S+)/;
and
$string =~ /\S+\s+\S+/;
However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string. [source]
my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);
As well as this direct method, matched groups are also available within the special $x variables, where x is the number of the group within the regular expression. We could therefore rewrite the preceding example as follows − [source]
#!/usr/bin/perl
$time = "12:05:30";
$time =~ m/(\d+):(\d+):(\d+)/; my ($hours, $minutes, $seconds) = ($1, $2, $3);
print "Hours : $hours, Minutes: $minutes, Second: $seconds\n";
When above program is executed, it produces the following result − [source]
Hours : 12, Minutes: 05, Second: 30
When groups are used in substitution expressions, the $x syntax can be used in the replacement text. Thus, we could reformat a date string using this − [source]
#!/usr/bin/perl
$date = '03/26/1999'; $date =~ s#(\d+)/(\d+)/(\d+)$3/$1/$2;
print "$date\n";
When above program is executed, it produces the following result − [source]
1999/03/26
=== The \G Assertion The \G assertion allows you to continue searching from the point where the last match occurred. For example, in the following code, we have used \G so that we can search to the correct position and then extract some information, without having to create a more complex, single regular expression − [source]
#!/usr/bin/perl
$string = "The time is: 12:31:02 on 4/12/00";
$string =~ /:\s+/g; ($time) = ($string =~ /\G(\d+:\d+:\d+)/); $string =~ /.\s/g; ($date) = ($string =~ m{\G(\d+/\d+/\d+)});
print "Time: $time, Date: $date\n";
When above program is executed, it produces the following result − [source]
Time: 12:31:02, Date: 4/12/00
The \G assertion is actually just the metasymbol equivalent of the pos function, so between regular expression calls you can continue to use pos, and even modify the value of pos (and therefore \G) by using pos as an lvalue subroutine. === Regular-expression Examples ==== Literal Characters [%autowidth] |=== |Sr.No.|Example & Description |1|*Perl* Match "Perl". |=== ==== Character Classes [%autowidth] |=== |Sr.No.|Example & Description |1|*[Pp]ython* Matches "Python" or "python" |2|*rub[ye]* Matches "ruby" or "rube" |3|*[aeiou]* Matches any one lowercase vowel |4|*[0-9]* Matches any digit; same as [0123456789] |5|*[a-z]* Matches any lowercase ASCII letter |6|*[A-Z]* Matches any uppercase ASCII letter |7|*[a-zA-Z0-9]* Matches any of the above |8|*[^aeiou]* Matches anything other than a lowercase vowel |9|*[^0-9]* Matches anything other than a digit |=== ==== Special Character Classes [%autowidth] |=== |Sr.No.|Example & Description |1|*.* Matches any character except newline |2|*\d* Matches a digit: [0-9] |3|*\D* Matches a nondigit: [^0-9] |4|*\s* Matches a whitespace character: [ \t\r\n\f] |5|*\S* Matches nonwhitespace: [^ \t\r\n\f] |6|*\w* Matches a single word character: [A-Za-z0-9_] |7|*\W* Matches a nonword character: [^A-Za-z0-9_] |=== ==== Repetition Cases [%autowidth] |=== |Sr.No.|Example & Description |1|*ruby?* Matches "rub" or "ruby": the y is optional |2|*ruby** Matches "rub" plus 0 or more ys |3|*ruby+* Matches "rub" plus 1 or more ys |4|*\d{3}* Matches exactly 3 digits |5|*\d{3,}* Matches 3 or more digits |6.|*\d{3,5}* Matches 3, 4, or 5 digits |=== ==== Nongreedy Repetition This matches the smallest number of repetitions − [%autowidth] |=== |Sr.No.|Example & Description |1|*<.*>* Greedy repetition: matches "<python>perl>" |2|*<.*?>* Nongreedy: matches "<python>" in "<python>perl>" |=== ==== Grouping with Parentheses [%autowidth] |=== |Sr.No.|Example & Description |1|*\D\d+* No group: + repeats \d |2|*(\D\d)+* Grouped: + repeats \D\d pair |3|*([Pp]ython(, )?)+* Match "Python", "Python, python, python", etc. |=== ==== Backreferences This matches a previously matched group again − [%autowidth] |=== |Sr.No.|Example & Description |1|*([Pp])ython&\1ails* Matches python&pails or Python&Pails |2|*(['"])[^\1]*\1* Single or double-quoted string. \1 matches whatever the 1st group matched. \2 matches whatever the 2nd group matched, etc. |=== ==== Alternatives [%autowidth] |=== |Sr.No.|Example & Description |1|*python|perl* Matches "python" or "perl" |2|*rub(y|le))* Matches "ruby" or "ruble" |3|*Python(!+|\?)* "Python" followed by one or more ! or one ? |=== ==== Anchors This need to specify match positions. [%autowidth] |=== |Sr.No.|Example & Description |1|*^Python* Matches "Python" at the start of a string or internal line |2|*Python$* Matches "Python" at the end of a string or line |3|*\APython* Matches "Python" at the start of a string |4|*Python\Z* Matches "Python" at the end of a string |5|*\bPython\b* Matches "Python" at a word boundary |6|*\brub\B* \B is nonword boundary: match "rub" in "rube" and "ruby" but not alone |7|*Python(?=!)* Matches "Python", if followed by an exclamation point |8|*Python(?!!)* Matches "Python", if not followed by an exclamation point |=== ==== Special Syntax with Parentheses [%autowidth] |=== |Sr.No.|Example & Description |1|*R(?#comment)* Matches "R". All the rest is a comment |2|*R(?i)uby* Case-insensitive while matching "uby" |3|*R(?i:uby)* Same as above |4|*rub(?:y|le))* Group only without creating \1 backreference |=== == Perl - Sending Email === Using sendmail Utility ==== Sending a Plain Message If you are working on Linux/Unix machine then you can simply use *sendmail* utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct. This may be different for your Linux/Unix machine. [source]
#!/usr/bin/perl
$to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script';
open(MAIL, "|/usr/sbin/sendmail -t");
Email Header
print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; # Email Body print MAIL $message;
close(MAIL); print "Email Sent Successfully\n";
Actually, the above script is a client email script, which will draft email and submit to the server running locally on your Linux/Unix machine. This script will not be responsible for sending email to actual destination. So you have to make sure email server is properly configured and running on your machine to send email to the given email ID. ==== Sending an HTML Message If you want to send HTML formatted email using sendmail, then you simply need to add *Content-type: text/html\n* in the header part of the email as follows − [source]
#!/usr/bin/perl
$to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = '<h1>This is test email sent by Perl Script</h1>';
open(MAIL, "|/usr/sbin/sendmail -t");
Email Header
print MAIL "To: $to\n"; print MAIL "From: $from\n"; print MAIL "Subject: $subject\n\n"; print MAIL "Content-type: text/html\n"; # Email Body print MAIL $message;
close(MAIL); print "Email Sent Successfully\n";
=== Using MIME::Lite Module If you are working on windows machine, then you will not have access on sendmail utility. But you have alternate to write your own email client using MIME:Lite perl module. You can download this module from link:http://search.cpan.org/CPAN/authors/id/Y/YV/YVES/MIME-Lite-3.01.tar.gz[MIME-Lite-3.01.tar.gz] and install it on your either machine Windows or Linux/Unix. To install it follow the simple steps − [source]
$tar xvfz MIME-Lite-3.01.tar.gz $cd MIME-Lite-3.01 $perl Makefile.PL $make $make install
That's it and you will have MIME::Lite module installed on your machine. Now you are ready to send your email with simple scripts explained below. ==== Sending a Plain Message Now following is a script which will take care of sending email to the given email ID − [source]
#!/usr/bin/perl use MIME::Lite;
$to = 'abcd@gmail.com'; $cc = 'efgh@mail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script';
$msg = MIME::Lite→new( From ⇒ $from, To ⇒ $to, Cc ⇒ $cc, Subject ⇒ $subject, Data ⇒ $message );
$msg→send; print "Email Sent Successfully\n";
==== Sending an HTML Message If you want to send HTML formatted email using sendmail, then you simply need to add *Content-type: text/html\n* in the header part of the email. Following is the script, which will take care of sending HTML formatted email − [source]
#!/usr/bin/perl use MIME::Lite;
$to = 'abcd@gmail.com'; $cc = 'efgh@mail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = '<h1>This is test email sent by Perl Script</h1>';
$msg = MIME::Lite→new( From ⇒ $from, To ⇒ $to, Cc ⇒ $cc, Subject ⇒ $subject, Data ⇒ $message );
$msg→attr("content-type" ⇒ "text/html"); $msg→send; print "Email Sent Successfully\n";
==== Sending an Attachment If you want to send an attachment, then following script serves the purpose − [source]
#!/usr/bin/perl use MIME::Lite;
$to = 'abcd@gmail.com'; $cc = 'efgh@mail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script';
$msg = MIME::Lite→new( From ⇒ $from, To ⇒ $to, Cc ⇒ $cc, Subject ⇒ $subject, Type ⇒ 'multipart/mixed' );
Add your text message.
$msg→attach(Type ⇒ 'text', Data ⇒ $message );
Specify your file as attachement.
$msg→attach(Type ⇒ 'image/gif', Path ⇒ '/tmp/logo.gif', Filename ⇒ 'logo.gif', Disposition ⇒ 'attachment' ); $msg→send; print "Email Sent Successfully\n";
You can attach as many files as you like in your email using attach() method. === Using SMTP Server If your machine is not running an email server then you can use any other email server available at the remote location. But to use any other email server you will need to have an id, its password, URL, etc. Once you have all the required information, you simple need to provide that information in *send()* method as follows − [source]
$msg→send('smtp', "smtp.myisp.net", AuthUser⇒"id", AuthPass⇒"password" );
You can contact your email server administrator to have the above used information and if a user id and password is not already available then your administrator can create it in minutes. == Perl - Socket Programming === What is a Socket? Socket is a Berkeley UNIX mechanism of creating a virtual duplex connection between different processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never ever have happened. Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it. This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications, which listen on different ports. For example an internet browser listens on port 80 for information received from the web server. Also we can write our custom applications which may listen and send/receive information on a specific port number. For now, let's sum up that a socket is an IP address and a port, enabling connection to send and recieve data over a network. To explain above mentioned socket concept we will take an example of Client - Server Programming using Perl. To complete a client server architecture we would have to go through the following steps − === To Create a Server . Create a socket using *socket* call. . Bind the socket to a port address using *bind* call. . Listen to the socket at the port address using *listen* call. . Accept client connections using *accept* call. === To Create a Client . Create a socket with *socket* call. . Connect (the socket) to the server using *connect* call. Following diagram shows the complete sequence of the calls used by Client and Server to communicate with each other − image::https://www.iokays.com/tutorialspoint/perl/_images/perl_socket.jpg[Perl Socket] === Server Side Socket Calls ==== The socket() call The *socket()* call is the first call in establishing a network connection is creating a socket. This call has the following syntax − [source]
socket( SOCKET, DOMAIN, TYPE, PROTOCOL );
The above call creates a SOCKET and other three arguments are integers which should have the following values for TCP/IP connections. . *DOMAIN* should be PF_INET. It's probable 2 on your computer. . *TYPE* should be SOCK_STREAM for TCP/IP connection. . *PROTOCOL* should be *(getprotobyname('tcp'))[2]*. It is the particular protocol such as TCP to be spoken over the socket. So socket function call issued by the server will be something like this − [source]
use Socket # This defines PF_INET and SOCK_STREAM
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]);
==== The bind() call The sockets created by socket() call are useless until they are bound to a hostname and a port number. Server uses the following *bind()* function to specify the port at which they will be accepting connections from the clients. [source]
bind( SOCKET, ADDRESS );
Here SOCKET is the descriptor returned by socket() call and ADDRESS is a socket address ( for TCP/IP ) containing three elements − . The address family (For TCP/IP, that's AF_INET, probably 2 on your system). . The port number (for example 21). . The internet address of the computer (for example 10.12.12.168). As the bind() is used by a server, which does not need to know its own address so the argument list looks like this − [source]
use Socket # This defines PF_INET and SOCK_STREAM
$port = 12345; # The unique port used by the sever to listen requests $server_ip_address = "10.12.12.168"; bind( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address))) or die "Can’t bind to port $port! \n";
The *or die* clause is very important because if a server dies without outstanding connections, the port won't be immediately reusable unless you use the option SO_REUSEADDR using *setsockopt()* function. Here *pack_sockaddr_in()* function is being used to pack the Port and IP address into binary format. ==== The listen() call If this is a server program, then it is required to issue a call to *listen()* on the specified port to listen, i.e., wait for the incoming requests. This call has the following syntax − [source]
listen( SOCKET, QUEUESIZE );
The above call uses SOCKET descriptor returned by socket() call and QUEUESIZE is the maximum number of outstanding connection request allowed simultaneously. ==== The accept() call If this is a server program then it is required to issue a call to the *access()* function to accept the incoming connections. This call has the following syntax − [source]
accept( NEW_SOCKET, SOCKET );
The accept call receive SOCKET descriptor returned by socket() function and upon successful completion, a new socket descriptor NEW_SOCKET is returned for all future communication between the client and the server. If access() call fails, then it returns FLASE which is defined in Socket module which we have used initially. Generally, accept() is used in an infinite loop. As soon as one connection arrives the server either creates a child process to deal with it or serves it himself and then goes back to listen for more connections. [source]
while(1) { accept( NEW_SOCKET, SOCKT ); ……. }
Now all the calls related to server are over and let us see a call which will be required by the client. === Client Side Socket Calls ==== The connect() call If you are going to prepare client program, then first you will use *socket()* call to create a socket and then you would have to use *connect()* call to connect to the server. You already have seen socket() call syntax and it will remain similar to server socket() call, but here is the syntax for *connect()* call − [source]
connect( SOCKET, ADDRESS );
Here SCOKET is the socket descriptor returned by socket() call issued by the client and ADDRESS is a socket address similar to bind call, except that it contains the IP address of the remote server. [source]
$port = 21; # For example, the ftp port $server_ip_address = "10.12.12.168"; connect( SOCKET, pack_sockaddr_in($port, inet_aton($server_ip_address))) or die "Can’t connect to port $port! \n";
If you connect to the server successfully, then you can start sending your commands to the server using SOCKET descriptor, otherwise your client will come out by giving an error message. === Client - Server Example Following is a Perl code to implement a simple client-server program using Perl socket. Here server listens for incoming requests and once connection is established, it simply replies Smile from the server. The client reads that message and print on the screen. Let's see how it has been done, assuming we have our server and client on the same machine. ==== Script to Create a Server [source]
#!/usr/bin/perl -w # Filename : server.pl
use strict; use Socket;
use port 7890 as default
my $port = shift || 7890; my $proto = getprotobyname('tcp'); my $server = "localhost"; # Host IP running the server
create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "Can’t open socket $!\n"; setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1) or die "Can’t set socket option to SO_REUSEADDR $!\n";
bind to a port, then listen
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can’t bind to port $port! \n";
listen(SOCKET, 5) or die "listen: $!"; print "SERVER started on port $port\n";
accepting a connection
my $client_addr; while ($client_addr = accept(NEW_SOCKET, SOCKET)) { # send them a message, close connection my $name = gethostbyaddr($client_addr, AF_INET ); print NEW_SOCKET "Smile from the server"; print "Connection recieved from $name\n"; close NEW_SOCKET; }
To run the server in background mode issue the following command on Unix prompt − [source]
$perl sever.pl&
==== Script to Create a Client [source]
!/usr/bin/perl -w # Filename : client.pl
use strict; use Socket;
initialize host and port
my $host = shift || 'localhost'; my $port = shift || 7890; my $server = "localhost"; # Host IP running the server
create the socket, connect to the port
socket(SOCKET,PF_INET,SOCK_STREAM,(getprotobyname('tcp'))[2]) or die "Can’t create a socket $!\n"; connect( SOCKET, pack_sockaddr_in($port, inet_aton($server))) or die "Can’t connect to port $port! \n";
my $line; while ($line = <SOCKET>) { print "$line\n"; } close SOCKET or die "close: $!";
Now let's start our client at the command prompt, which will connect to the server and read message sent by the server and displays the same on the screen as follows − [source]
$perl client.pl Smile from the server
*NOTE* − If you are giving the actual IP address in dot notation, then it is recommended to provide IP address in the same format in both client as well as server to avoid any confusion. == Object Oriented Programming in PERL We have already studied references in Perl and Perl anonymous arrays and hashes. Object Oriented concept in Perl is very much based on references and anonymous array and hashes. Let's start learning basic concepts of Object Oriented Perl. === Object Basics There are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method. . An *object* within Perl is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes. . A *class* within Perl is a package that contains the corresponding methods required to create and manipulate objects. . A *method* within Perl is a subroutine, defined with the package. The first argument to the method is an object reference or a package name, depending on whether the method affects the current object or the class. Perl provides a *bless()* function, which is used to return a reference which ultimately becomes an object. === Defining a Class It is very simple to define a class in Perl. A class is corresponding to a Perl Package in its simplest form. To create a class in Perl, we first build a package. A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again. Perl Packages provide a separate namespace within a Perl program which keeps subroutines and variables independent from conflicting with those in other packages. To declare a class named Person in Perl we do − [source]
package Person;
The scope of the package definition extends to the end of the file, or until another package keyword is encountered. === Creating and Using Objects To create an instance of a class (an object) we need an object constructor. This constructor is a method defined within the package. Most programmers choose to name this object constructor method new, but in Perl you can use any name. You can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes. Let's create our constructor for our Person class using a Perl hash reference. When creating an object, you need to supply a constructor, which is a subroutine within a package that returns an object reference. The object reference is created by blessing a reference to the package's class. For example − [source]
package Person; sub new { my $class = shift; my $self = { _firstName ⇒ shift, _lastName ⇒ shift, _ssn ⇒ shift, }; # Print all the values just for clarification. print "First Name is $self→{_firstName}\n"; print "Last Name is $self→{_lastName}\n"; print "SSN is $self→{_ssn}\n"; bless $self, $class; return $self; }
Now Let us see how to create an Object. [source]
$object = new Person( "Mohammad", "Saleem", 23234345);
You can use simple hash in your consturctor if you don't want to assign any value to any class variable. For example − [source]
package Person; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; }
=== Defining Methods Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and they provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper methods to manipulate object data. Lets define a helper method to get person’s first name − [source]
sub getFirstName { return $self→{_firstName}; }
Another helper function to set person’s first name − [source]
sub setFirstName { my ( $self, $firstName ) = @_; $self→{_firstName} = $firstName if defined($firstName); return $self→{_firstName}; }
Now lets have a look into complete example: Keep Person package and helper functions into Person.pm file. [source]
#!/usr/bin/perl
package Person;
sub new { my $class = shift; my $self = { firstName ⇒ shift, _lastName ⇒ shift, _ssn ⇒ shift, }; # Print all the values just for clarification. print "First Name is $self→{_firstName}\n"; print "Last Name is $self→{_lastName}\n"; print "SSN is $self→{_ssn}\n"; bless $self, $class; return $self; } sub setFirstName { my ( $self, $firstName ) = @; $self→{_firstName} = $firstName if defined($firstName); return $self→{_firstName}; }
sub getFirstName { my( $self ) = @_; return $self→{_firstName}; } 1;
Now let's make use of Person object in employee.pl file as follows − [source]
#!/usr/bin/perl
use Person;
$object = new Person( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName = $object→getFirstName();
print "Before Setting First Name is : $firstName\n";
Now Set first name using helper function.
$object→setFirstName( "Mohd." );
Now get first name set by helper function.
$firstName = $object→getFirstName(); print "Before Setting First Name is : $firstName\n";
When we execute above program, it produces the following result − [source]
First Name is Mohammad Last Name is Saleem SSN is 23234345 Before Setting First Name is : Mohammad Before Setting First Name is : Mohd.
=== Inheritance Object-oriented programming has very good and useful concept called inheritance. Inheritance simply means that properties and methods of a parent class will be available to the child classes. So you don't have to write the same code again and again, you can just inherit a parent class. For example, we can have a class Employee, which inherits from Person. This is referred to as an "isa" relationship because an employee is a person. Perl has a special variable, @ISA, to help with this. @ISA governs (method) inheritance. Following are the important points to be considered while using inheritance − . Perl searches the class of the specified object for the given method or attribute, i.e., variable. . Perl searches the classes defined in the object class's @ISA array. . If no method is found in steps 1 or 2, then Perl uses an AUTOLOAD subroutine, if one is found in the @ISA tree. . If a matching method still cannot be found, then Perl searches for the method within the UNIVERSAL class (package) that comes as part of the standard Perl library. . If the method still has not found, then Perl gives up and raises a runtime exception. So to create a new Employee class that will inherit methods and attributes from our Person class, we simply code as follows: Keep this code into Employee.pm. [source]
#!/usr/bin/perl
package Employee; use Person; use strict; our @ISA = qw(Person); # inherits from Person
Now Employee Class has all the methods and attributes inherited from Person class and you can use them as follows: Use main.pl file to test it − [source]
#!/usr/bin/perl
use Employee;
$object = new Employee( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName = $object→getFirstName();
print "Before Setting First Name is : $firstName\n";
Now Set first name using helper function.
$object→setFirstName( "Mohd." );
Now get first name set by helper function.
$firstName = $object→getFirstName(); print "After Setting First Name is : $firstName\n";
When we execute above program, it produces the following result − [source]
First Name is Mohammad Last Name is Saleem SSN is 23234345 Before Setting First Name is : Mohammad Before Setting First Name is : Mohd.
=== Method Overriding The child class Employee inherits all the methods from the parent class Person. But if you would like to override those methods in your child class then you can do it by giving your own implementation. You can add your additional functions in child class or you can add or modify the functionality of an existing methods in its parent class. It can be done as follows: modify Employee.pm file. [source]
#!/usr/bin/perl
package Employee; use Person; use strict; our @ISA = qw(Person); # inherits from Person
Override constructor
sub new { my ($class) = @_;
# Call the constructor of the parent class, Person. my $self = $class->SUPER::new( $_[1], $_[2], $_[3] ); # Add few more attributes $self->{_id} = undef; $self->{_title} = undef; bless $self, $class; return $self; }
Override helper function
sub getFirstName { my( $self ) = @_; # This is child class function. print "This is child class helper function\n"; return $self→{_firstName}; }
Add more methods
sub setLastName{ my ( $self, $lastName ) = @_; $self→{_lastName} = $lastName if defined($lastName); return $self→{_lastName}; }
sub getLastName { my( $self ) = @_; return $self→{_lastName}; }
1;
Now let's again try to use Employee object in our main.pl file and execute it. [source]
#!/usr/bin/perl
use Employee;
$object = new Employee( "Mohammad", "Saleem", 23234345); # Get first name which is set using constructor. $firstName = $object→getFirstName();
print "Before Setting First Name is : $firstName\n";
Now Set first name using helper function.
$object→setFirstName( "Mohd." );
Now get first name set by helper function.
$firstName = $object→getFirstName(); print "After Setting First Name is : $firstName\n";
When we execute above program, it produces the following result − [source]
First Name is Mohammad Last Name is Saleem SSN is 23234345 This is child class helper function Before Setting First Name is : Mohammad This is child class helper function After Setting First Name is : Mohd.
=== Default Autoloading Perl offers a feature which you would not find in any other programming languages: a default subroutine. Which means, if you define a function called *AUTOLOAD(),* then any calls to undefined subroutines will call AUTOLOAD() function automatically. The name of the missing subroutine is accessible within this subroutine as $AUTOLOAD. Default autoloading functionality is very useful for error handling. Here is an example to implement AUTOLOAD, you can implement this function in your own way. [source]
sub AUTOLOAD { my $self = shift; my $type = ref ($self) || croak "$self is not an object"; my $field = $AUTOLOAD; $field =~ s/.*://; unless (exists $self→{$field}) { croak "$field does not exist in object/class $type"; } if (@_) { return $self→($name) = shift; } else { return $self→($name); } }
=== Destructors and Garbage Collection If you have programmed using object oriented programming before, then you will be aware of the need to create a *destructor* to free the memory allocated to the object when you have finished using it. Perl does this automatically for you as soon as the object goes out of scope. In case you want to implement your destructor, which should take care of closing files or doing some extra processing then you need to define a special method called *DESTROY*. This method will be called on the object just before Perl frees the memory allocated to it. In all other respects, the DESTROY method is just like any other method, and you can implement whatever logic you want inside this method. A destructor method is simply a member function (subroutine) named DESTROY, which will be called automatically in following cases − . When the object reference's variable goes out of scope. . When the object reference's variable is undef-ed. . When the script terminates . When the perl interpreter terminates For Example, you can simply put the following method DESTROY in your class − [source]
package MyClass; … sub DESTROY { print "MyClass::DESTROY called\n"; }
=== Object Oriented Perl Example Here is another nice example, which will help you to understand Object Oriented Concepts of Perl. Put this source code into any perl file and execute it. [source]
#!/usr/bin/perl
Following is the implementation of simple Class.
package MyClass;
sub new { print "MyClass::new called\n"; my $type = shift; # The package/type name my $self = {}; # Reference to empty hash return bless $self, $type; }
sub DESTROY { print "MyClass::DESTROY called\n"; }
sub MyMethod { print "MyClass::MyMethod called!\n"; }
Following is the implemnetation of Inheritance.
package MySubClass;
@ISA = qw( MyClass );
sub new { print "MySubClass::new called\n"; my $type = shift; # The package/type name my $self = MyClass→new; # Reference to empty hash return bless $self, $type; }
sub DESTROY { print "MySubClass::DESTROY called\n"; }
sub MyMethod { my $self = shift; $self→SUPER::MyMethod(); print " MySubClass::MyMethod called!\n"; }
Here is the main program using above classes.
package main;
print "Invoke MyClass method\n";
$myObject = MyClass→new(); $myObject→MyMethod();
print "Invoke MySubClass method\n";
$myObject2 = MySubClass→new(); $myObject2→MyMethod();
print "Create a scoped object\n"; { my $myObject2 = MyClass→new(); } # Destructor is called automatically here
print "Create and undef an object\n"; $myObject3 = MyClass→new(); undef $myObject3;
print "Fall off the end of the script…\n"; # Remaining destructors are called automatically here
When we execute above program, it produces the following result − [source]
Invoke MyClass method MyClass::new called MyClass::MyMethod called! Invoke MySubClass method MySubClass::new called MyClass::new called MyClass::MyMethod called! MySubClass::MyMethod called! Create a scoped object MyClass::new called MyClass::DESTROY called Create and undef an object MyClass::new called MyClass::DESTROY called Fall off the end of the script… MyClass::DESTROY called MySubClass::DESTROY called
== Perl - Database Access This chapter teaches you how to access a database inside your Perl script. Starting from Perl 5 has become very easy to write database applications using *DBI* module. DBI stands for *Database Independent Interface* for Perl, which means DBI provides an abstraction layer between the Perl code and the underlying database, allowing you to switch database implementations really easily. The DBI is a database access module for the Perl programming language. It provides a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used. === Architecture of a DBI Application DBI is independent of any database available in backend. You can use DBI whether you are working with Oracle, MySQL or Informix, etc. This is clear from the following architure diagram. image::https://www.iokays.com/tutorialspoint/perl/_images/dbiarch.gif[Perl Database Module DBI Architecture] Here DBI is responsible of taking all SQL commands through the API, (i.e., Application Programming Interface) and to dispatch them to the appropriate driver for actual execution. And finally, DBI is responsible of taking results from the driver and giving back it to the calling scritp. === Notation and Conventions Throughout this chapter following notations will be used and it is recommended that you should also follow the same convention. [source]
$dsn Database source name $dbh Database handle object $sth Statement handle object $h Any of the handle types above ($dbh, $sth, or $drh) $rc General Return Code (boolean: true=ok, false=error) $rv General Return Value (typically an integer) @ary List of values returned from the database. $rows Number of rows processed (if available, else -1) $fh A filehandle undef NULL values are represented by undefined values in Perl \%attr Reference to a hash of attribute values passed to methods
=== Database Connection Assuming we are going to work with MySQL database. Before connecting to a database make sure of the followings. You can take help of our MySQL tutorial in case you are not aware about how to create database and tables in MySQL database. . You have created a database with a name TESTDB. . You have created a table with a name TEST_TABLE in TESTDB. . This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME. . User ID "testuser" and password "test123" are set to access TESTDB. . Perl Module DBI is installed properly on your machine. . You have gone through MySQL tutorial to understand MySQL Basics. Following is the example of connecting with MySQL database "TESTDB" − [source]
#!/usr/bin/perl
use DBI use strict;
my $driver = "mysql"; my $database = "TESTDB"; my $dsn = "DBI:$driver:database=$database"; my $userid = "testuser"; my $password = "test123";
my $dbh = DBI→connect($dsn, $userid, $password ) or die $DBI::errstr;
If a connection is established with the datasource then a Database Handle is returned and saved into $dbh for further use otherwise $dbh is set to undef value and $DBI::errstr returns an error string. === INSERT Operation INSERT operation is required when you want to create some records into a table. Here we are using table TEST_TABLE to create our records. So once our database connection is established, we are ready to create records into TEST_TABLE. Following is the procedure to create single record into TEST_TABLE. You can create as many as records you like using the same concept. Record creation takes the following steps − . Preparing SQL statement with INSERT statement. This will be done using *prepare()* API. . Executing SQL query to select all the results from the database. This will be done using *execute()* API. . Releasing Stattement handle. This will be done using *finish()* API. . If everything goes fine then *commit* this operation otherwise you can *rollback* complete transaction. Commit and Rollback are explained in next sections. [source]
my $sth = $dbh→prepare("INSERT INTO TEST_TABLE (FIRST_NAME, LAST_NAME, SEX, AGE, INCOME ) values ('john', 'poul', 'M', 30, 13000)"); $sth→execute() or die $DBI::errstr; $sth→finish(); $dbh→commit or die $DBI::errstr;
=== Using Bind Values There may be a case when values to be entered is not given in advance. So you can use bind variables which will take the required values at run time. Perl DBI modules make use of a question mark in place of actual value and then actual values are passed through execute() API at the run time. Following is the example − [source]
my $first_name = "john"; my $last_name = "poul"; my $sex = "M"; my $income = 13000; my $age = 30; my $sth = $dbh→prepare("INSERT INTO TEST_TABLE (FIRST_NAME, LAST_NAME, SEX, AGE, INCOME ) values (?,?,?,?)"); $sth→execute($first_name,$last_name,$sex, $age, $income) or die $DBI::errstr; $sth→finish(); $dbh→commit or die $DBI::errstr;
=== READ Operation READ Operation on any databasse means to fetch some useful information from the database, i.e., one or more records from one or more tables. So once our database connection is established, we are ready to make a query into this database. Following is the procedure to query all the records having AGE greater than 20. This will take four steps − . Preparing SQL SELECT query based on required conditions. This will be done using *prepare()* API. . Executing SQL query to select all the results from the database. This will be done using *execute()* API. . Fetching all the results one by one and printing those results.This will be done using *fetchrow_array()* API. . Releasing Stattement handle. This will be done using *finish()* API. [source]
my $sth = $dbh→prepare("SELECT FIRST_NAME, LAST_NAME FROM TEST_TABLE WHERE AGE > 20"); $sth→execute() or die $DBI::errstr; print "Number of rows found :" + $sth→rows; while (my @row = $sth→fetchrow_array()) { my ($first_name, $last_name ) = @row; print "First Name = $first_name, Last Name = $last_name\n"; } $sth→finish();
=== Using Bind Values There may be a case when condition is not given in advance. So you can use bind variables, which will take the required values at run time. Perl DBI modules makes use of a question mark in place of actual value and then the actual values are passed through execute() API at the run time. Following is the example − [source]
$age = 20; my $sth = $dbh→prepare("SELECT FIRST_NAME, LAST_NAME FROM TEST_TABLE WHERE AGE > ?"); $sth→execute( $age ) or die $DBI::errstr; print "Number of rows found :" + $sth→rows; while (my @row = $sth→fetchrow_array()) { my ($first_name, $last_name ) = @row; print "First Name = $first_name, Last Name = $last_name\n"; } $sth→finish();
=== UPDATE Operation UPDATE Operation on any database means to update one or more records already available in the database tables. Following is the procedure to update all the records having SEX as 'M'. Here we will increase AGE of all the males by one year. This will take three steps − . Preparing SQL query based on required conditions. This will be done using *prepare()* API. . Executing SQL query to select all the results from the database. This will be done using *execute()* API. . Releasing Stattement handle. This will be done using *finish()* API. . If everything goes fine then *commit* this operation otherwise you can *rollback* complete transaction. See next section for commit and rollback APIs. [source]
my $sth = $dbh→prepare("UPDATE TEST_TABLE SET AGE = AGE + 1 WHERE SEX = 'M'"); $sth→execute() or die $DBI::errstr; print "Number of rows updated :" + $sth→rows; $sth→finish(); $dbh→commit or die $DBI::errstr;
=== Using Bind Values There may be a case when condition is not given in advance. So you can use bind variables, which will take required values at run time. Perl DBI modules make use of a question mark in place of actual value and then the actual values are passed through execute() API at the run time. Following is the example − [source]
$sex = 'M'; my $sth = $dbh→prepare("UPDATE TEST_TABLE SET AGE = AGE + 1 WHERE SEX = ?"); $sth→execute('$sex') or die $DBI::errstr; print "Number of rows updated :" + $sth→rows; $sth→finish(); $dbh→commit or die $DBI::errstr;
In some case you would like to set a value, which is not given in advance so you can use binding value as follows. In this example income of all males will be set to 10000. [source]
$sex = 'M'; $income = 10000; my $sth = $dbh→prepare("UPDATE TEST_TABLE SET INCOME = ? WHERE SEX = ?"); $sth→execute( $income, '$sex') or die $DBI::errstr; print "Number of rows updated :" + $sth→rows; $sth→finish();
=== DELETE Operation DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from TEST_TABLE where AGE is equal to 30. This operation will take the following steps. . Preparing SQL query based on required conditions. This will be done using *prepare()* API. . Executing SQL query to delete required records from the database. This will be done using *execute()* API. . Releasing Stattement handle. This will be done using *finish()* API. . If everything goes fine then *commit* this operation otherwise you can *rollback* complete transaction. [source]
$age = 30; my $sth = $dbh→prepare("DELETE FROM TEST_TABLE WHERE AGE = ?"); $sth→execute( $age ) or die $DBI::errstr; print "Number of rows deleted :" + $sth→rows; $sth→finish(); $dbh→commit or die $DBI::errstr;
=== Using do Statement If you're doing an UPDATE, INSERT, or DELETE there is no data that comes back from the database, so there is a short cut to perform this operation. You can use *do* statement to execute any of the command as follows. [source]
$dbh→do('DELETE FROM TEST_TABLE WHERE age =30');
*do* returns a true value if it succeeded, and a false value if it failed. Actually, if it succeeds it returns the number of affected rows. In the example it would return the number of rows that were actually deleted. === COMMIT Operation Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted to its orignal position. Here is a simple example to call *commit* API. [source]
$dbh→commit or die $dbh→errstr;
=== ROLLBACK Operation If you are not satisfied with all the changes or you encounter an error in between of any operation , you can revert those changes to use *rollback* API. Here is a simple example to call *rollback* API. [source]
$dbh→rollback or die $dbh→errstr;
=== Begin Transaction Many databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the changes are actually made. Then at the end, you issue the special SQL query *COMMIT*, and all the changes are made simultaneously. Alternatively, you can issue the query ROLLBACK, in which case all the changes are thrown away and database remains unchanged. Perl DBI module provided *begin_work* API, which enables transactions (by turning AutoCommit off) until the next call to commit or rollback. After the next commit or rollback, AutoCommit will automatically be turned on again. [source]
$rc = $dbh→begin_work or die $dbh→errstr;
=== AutoCommit Option If your transactions are simple, you can save yourself the trouble of having to issue a lot of commits. When you make the connect call, you can specify an *AutoCommit* option which will perform an automatic commit operation after every successful query. Here's what it looks like − [source]
my $dbh = DBI→connect($dsn, $userid, $password, {AutoCommit ⇒ 1}) or die $DBI::errstr;
Here AutoCommit can take value 1 or 0, where 1 means AutoCommit is on and 0 means AutoCommit is off. === Automatic Error Handling When you make the connect call, you can specify a RaiseErrors option that handles errors for you automatically. When an error occurs, DBI will abort your program instead of returning a failure code. If all you want is to abort the program on an error, this can be convenient. Here's what it looks like − [source]
my $dbh = DBI→connect($dsn, $userid, $password, {RaiseError ⇒ 1}) or die $DBI::errstr;
Here RaiseError can take value 1 or 0. === Disconnecting Database To disconnect Database connection, use *disconnect* API as follows − [source]
$rc = $dbh→disconnect or warn $dbh→errstr;
The transaction behaviour of the disconnect method is, sadly, undefined. Some database systems (such as Oracle and Ingres) will automatically commit any outstanding changes, but others (such as Informix) will rollback any outstanding changes. Applications not using AutoCommit should explicitly call commit or rollback before calling disconnect. === Using NULL Values Undefined values, or undef, are used to indicate NULL values. You can insert and update columns with a NULL value as you would a non-NULL value. These examples insert and update the column age with a NULL value − [source]
$sth = $dbh→prepare(qq { INSERT INTO TEST_TABLE (FIRST_NAME, AGE) VALUES (?, ?) }); $sth→execute("Joe", undef);
Here *qq{}* is used to return a quoted string to *prepare* API. However, care must be taken when trying to use NULL values in a WHERE clause. Consider − [source]
SELECT FIRST_NAME FROM TEST_TABLE WHERE age = ?
Binding an undef (NULL) to the placeholder will not select rows, which have a NULL age! At least for database engines that conform to the SQL standard. Refer to the SQL manual for your database engine or any SQL book for the reasons for this. To explicitly select NULLs you have to say "WHERE age IS NULL". A common issue is to have a code fragment handle a value that could be either defined or undef (non-NULL or NULL) at runtime. A simple technique is to prepare the appropriate statement as needed, and substitute the placeholder for non-NULL cases − [source]
$sql_clause = defined $age? "age = ?" : "age IS NULL"; $sth = $dbh→prepare(qq { SELECT FIRST_NAME FROM TEST_TABLE WHERE $sql_clause }); $sth→execute(defined $age ? $age : ());
=== Some Other DBI Functions ==== available_drivers [source]
@ary = DBI→available_drivers; @ary = DBI→available_drivers($quiet);
Returns a list of all available drivers by searching for DBD::* modules through the directories in @INC. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning. ==== installed_drivers [source]
%drivers = DBI→installed_drivers();
Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix. ==== data_sources [source]
@ary = DBI→data_sources($driver);
Returns a list of data sources (databases) available via the named driver. If $driver is empty or undef, then the value of the DBI_DRIVER environment variable is used. ==== quote [source]
$sql = $dbh→quote($value); $sql = $dbh→quote($value, $data_type);
Quote a string literal for use as a literal value in an SQL statement, by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks. [source]
$sql = sprintf "SELECT foo FROM bar WHERE baz = %s", $dbh→quote("Don’t");
For most database types, quote would return 'Don''t' (including the outer quotation marks). It is valid for the quote() method to return an SQL expression that evaluates to the desired string. For example − [source]
$quoted = $dbh→quote("one\ntwo\0three")
may produce results which will be equivalent to
CONCAT('one', CHAR(12), 'two', CHAR(0), 'three')
=== Methods Common to All Handles ==== err [source]
$rv = $h→err; or $rv = $DBI::err or $rv = $h→err
Returns the native database engine error code from the last driver method called. The code is typically an integer but you should not assume that. This is equivalent to $DBI::err or $h->err. ==== errstr [source]
$str = $h→errstr; or $str = $DBI::errstr or $str = $h→errstr
Returns the native database engine error message from the last DBI method called. This has the same lifespan issues as the "err" method described above. This is equivalent to $DBI::errstr or $h->errstr. ==== rows [source]
$rv = $h→rows; or $rv = $DBI::rows
This returns the number of rows effected by previous SQL statement and equivalent to $DBI::rows. ==== trace [source]
$h→trace($trace_settings);
DBI sports an extremely useful ability to generate runtime tracing information of what it's doing, which can be a huge time-saver when trying to track down strange problems in your DBI programs. You can use different values to set trace level. These values varies from 0 to 4. The value 0 means disable trace and 4 means generate complete trace. === Interpolated Statements are Prohibited It is highly recommended not to use interpolated statements as follows − [source]
while ($first_name = <>) { my $sth = $dbh→prepare("SELECT * FROM TEST_TABLE WHERE FIRST_NAME = '$first_name'"); $sth→execute(); # and so on … }
Thus don't use interpolated statement instead use *bind value* to prepare dynamic SQL statement. == Perl - CGI Programming === What is CGI ? . A Common Gateway Interface, or CGI, is a set of standards that defines how information is exchanged between the web server and a custom script. . The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows − . The Common Gateway Interface, or CGI, is a standard for external gateway programs to interface with information servers such as HTTP servers. . The current version is CGI/1.1 and CGI/1.2 is under progress. === Web Browsing To understand the concept of CGI, lets see what happens when we click a hyper link available on a web page to browse a particular web page or URL. . Your browser contacts web server using HTTP protocol and demands for the URL, i.e., web page filename. . Web Server will check the URL and will look for the filename requested. If web server finds that file then it sends the file back to the browser without any further execution otherwise sends an error message indicating that you have requested a wrong file. . Web browser takes response from web server and displays either the received file content or an error message in case file is not found. However, it is possible to set up HTTP server in such a way so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs as a result, that is sent back for your browser to display. This can be done by using a special functionality available in the web server and it is called *Common Gateway Interface* or CGI and such programs which are executed by the server to produce final result, are called CGI scripts. These CGI programs can be a PERL Script, Shell Script, C or C++ program, etc. === CGI Architecture Diagram image::https://www.iokays.com/tutorialspoint/perl/_images/cgiarch.gif[CGI Architecture] === Web Server Support and Configuration Before you proceed with CGI Programming, make sure that your Web Server supports CGI functionality and it is configured to handle CGI programs. All the CGI programs to be executed by the web server are kept in a pre-configured directory. This directory is called CGI directory and by convention it is named as /cgi-bin. By convention Perl CGI files will have extention as *.cgi*. === First CGI Program Here is a simple link which is linked to a CGI script called link:../cgi-bin/hello.cgi[hello.cgi]. This file has been kept in */cgi-bin/* directory and it has the following content. Before running your CGI program, make sure you have change mode of file using *chmod 755 hello.cgi* UNIX command. [source]
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n"; print '<html>'; print '<head>'; print '<title>Hello Word - First CGI Program</title>'; print '</head>'; print '<body>'; print '<h2>Hello Word! This is my first CGI program</h2>'; print '</body>'; print '</html>';
1;
Now if you click *hello.cgi* link then request goes to web server who search for hello.cgi in /cgi-bin directory, execute it and whatever result got generated, web server sends that result back to the web browser, which is as follows − [source]
Hello Word! This is my first CGI program
This hello.cgi script is a simple Perl script which is writing its output on STDOUT file, i.e., screen. There is one important and extra feature available which is first line to be printed *Content-type:text/html\r\n\r\n*. This line is sent back to the browser and specifies the content type to be displayed on the browser screen. Now you must have undertood basic concept of CGI and you can write many complicated CGI programs using Perl. This script can interact with any other exertnal system also to exchange information such as a database, web services, or any other complex interfaces. === Understanding HTTP Header The very first line *Content-type:text/html\r\n\r\n* is a part of HTTP header, which is sent to the browser so that browser can understand the incoming content from server side. All the HTTP header will be in the following form − [source]
HTTP Field Name: Field Content
For Example − [source]
Content-type:text/html\r\n\r\n
There are few other important HTTP headers, which you will use frequently in your CGI Programming. [%autowidth] |=== |Sr.No.|Header & Description |1|*Content-type: String* A MIME string defining the format of the content being returned. Example is Content-type:text/html |2|*Expires: Date String* The date when the information becomes invalid. This should be used by the browser to decide when a page needs to be refreshed. A valid date string should be in the format 01 Jan 1998 12:00:00 GMT. |3|*Location: URL String* The URL that should be returned instead of the URL requested. You can use this filed to redirect a request to any other location. |4|*Last-modified: String* The date of last modification of the file. |5|*Content-length: String* The length, in bytes, of the data being returned. The browser uses this value to report the estimated download time for a file. |6|*Set-Cookie: String* Set the cookie passed through the string |=== === CGI Environment Variables All the CGI program will have access to the following environment variables. These variables play an important role while writing any CGI program. [%autowidth] |=== |Sr.No.|Variables Names & Description |1|*CONTENT_TYPE* The data type of the content. Used when the client is sending attached content to the server. For example file upload, etc. |2|*CONTENT_LENGTH* The length of the query information. It's available only for POST requests |3|*HTTP_COOKIE* Returns the set cookies in the form of key & value pair. |4|*HTTP_USER_AGENT* The User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser. |5|*PATH_INFO* The path for the CGI script. |6|*QUERY_STRING* The URL-encoded information that is sent with GET method request. |7|*REMOTE_ADDR* The IP address of the remote host making the request. This can be useful for logging or for authentication purpose. |8|*REMOTE_HOST* The fully qualified name of the host making the request. If this information is not available then REMOTE_ADDR can be used to get IR address. |9|*REQUEST_METHOD* The method used to make the request. The most common methods are GET and POST. |10|*SCRIPT_FILENAME* The full path to the CGI script. |11|*SCRIPT_NAME* The name of the CGI script. |12|*SERVER_NAME* The server's hostname or IP Address. |13|*SERVER_SOFTWARE* The name and version of the software the server is running. |=== Here is a small CGI program to list down all the CGI variables supported by your Web server. Click this link to see the result link:https://www.tutorialspoint.com/cgi-bin/get_env.cgi[Get Environment] [source]
#!/usr/bin/perl
print "Content-type: text/html\n\n"; print "<font size=+1>Environment</font>\n"; foreach (sort keys %ENV) { print "<b>$</b>: $ENV{$}<br>\n"; }
1;
=== Raise a "File Download" Dialog Box? Sometime it is desired that you want to give option where a user will click a link and it will pop up a "File Download" dialogue box to the user instead of displaying actual content. This is very easy and will be achived through HTTP header. This HTTP header will be different from the header mentioned in previous section. For example, if you want to make a *FileName* file downloadable from a given link then it's syntax will be as follows − [source]
#!/usr/bin/perl
HTTP Header
print "Content-Type:application/octet-stream; name = \"FileName\"\r\n"; print "Content-Disposition: attachment; filename = \"FileName\"\r\n\n";
Actual File Content will go hear.
open( FILE, "<FileName" ); while(read(FILE, $buffer, 100) ) { print("$buffer"); }
=== GET and POST Methods You must have come across many situations when you need to pass some information from your browser to the web server and ultimately to your CGI Program handling your requests. Most frequently browser uses two methods to pass this information to the web server. These methods are *GET* Method and *POST* Method. Let's check them one by one. === Passing Information using GET Method The GET method sends the encoded user information appended to the page URL itself. The page and the encoded information are separated by the ? character as follows − [source]
The GET method is the defualt method to pass information from a browser to the web server and it produces a long string that appears in your browser's Location:box. You should never use GET method if you have password or other sensitive information to pass to the server. The GET method has size limitation: only 1024 characters can be passed in a request string. This information is passed using *QUERY_STRING* header and will be accessible in your CGI Program through QUERY_STRING environment variable which you can parse and use in your CGI program. You can pass information by simply concatenating key and value pairs alongwith any URL or you can use HTML <FORM> tags to pass information using GET method. === Simple URL Example: Get Method Here is a simple URL which will pass two values to hello_get.cgi program using GET method. Below is *hello_get.cgi* script to handle input given by web browser. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Hello - Second CGI Program</title>"; print "</head>"; print "<body>"; print "<h2>Hello $first_name $last_name - Second CGI Program</h2>"; print "</body>"; print "</html>";
1;
=== Simple FORM Example: GET Method Here is a simple example, which passes two values using HTML FORM and submit button. We are going to use the same CGI script hello_get.cgi to handle this input. [source]
<FORM action = "/cgi-bin/hello_get.cgi" method = "GET"> First Name: <input type = "text" name = "first_name"> <br>
Last Name: <input type = "text" name = "last_name"> <input type = "submit" value = "Submit"> </FORM>
Here is the actual output of the above form coding. Now you can enter First and Last Name and then click submit button to see the result. === Passing Information using POST Method A more reliable method of passing information to a CGI program is the *POST* method. This packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a *?* in the URL, it sends it as a separate message as a part of HTTP header. Web server provides this message to the CGI script in the form of the standard input. Below is the modified *hello_post.cgi* script to handle input given by the web browser. This script will handle GET as well as POST method. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Hello - Second CGI Program</title>"; print "</head>"; print "<body>"; print "<h2>Hello $first_name $last_name - Second CGI Program</h2>"; print "</body>"; print "</html>";
1;
Let us take again same examle as above, which passes two values using HTML FORM and submit button. We are going to use CGI script hello_post.cgi to handle this input. [source]
<FORM action = "/cgi-bin/hello_post.cgi" method = "POST"> First Name: <input type = "text" name = "first_name"> <br>
Last Name: <input type = "text" name = "last_name">
<input type = "submit" value = "Submit"> </FORM>
Here is the actual output of the above form coding, You enter First and Last Name and then click submit button to see the result. === Passing Checkbox Data to CGI Program Checkboxes are used when more than one option is required to be selected. Here is an example HTML code for a form with two checkboxes. [source]
<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank"> <input type = "checkbox" name = "maths" value = "on"> Maths <input type = "checkbox" name = "physics" value = "on"> Physics <input type = "submit" value = "Select Subject"> </form>
The result of this code is the following form − Below is *checkbox.cgi* script to handle input given by web browser for radio button. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } if( $FORM{maths} ) { $maths_flag ="ON"; } else { $maths_flag ="OFF"; } if( $FORM{physics} ) { $physics_flag ="ON"; } else { $physics_flag ="OFF"; }
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Checkbox - Third CGI Program</title>"; print "</head>"; print "<body>"; print "<h2> CheckBox Maths is : $maths_flag</h2>"; print "<h2> CheckBox Physics is : $physics_flag</h2>"; print "</body>"; print "</html>";
1;
=== Passing Radio Button Data to CGI Program Radio Buttons are used when only one option is required to be selected. Here is an example HTML code for a form with two radio button − [source]
<form action = "/cgi-bin/radiobutton.cgi" method = "POST" target = "_blank"> <input type = "radio" name = "subject" value = "maths"> Maths <input type = "radio" name = "subject" value = "physics"> Physics <input type = "submit" value = "Select Subject"> </form>
The result of this code is the following form − Below is *radiobutton.cgi* script to handle input given by the web browser for radio button. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $subject = $FORM{subject};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Radio - Fourth CGI Program</title>"; print "</head>"; print "<body>"; print "<h2> Selected Subject is $subject</h2>"; print "</body>"; print "</html>";
1;
=== Passing Text Area Data to CGI Program A textarea element is used when multiline text has to be passed to the CGI Program. Here is an example HTML code for a form with a TEXTAREA box − [source]
<form action = "/cgi-bin/textarea.cgi" method = "POST" target = "_blank"> <textarea name = "textcontent" cols = 40 rows = 4> Type your text here… </textarea> <input type = "submit" value = "Submit"> </form>
The result of this code is the following form − Below is the *textarea.cgi* script to handle input given by the web browser. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $text_content = $FORM{textcontent};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Text Area - Fifth CGI Program</title>"; print "</head>"; print "<body>"; print "<h2> Entered Text Content is $text_content</h2>"; print "</body>"; print "</html>";
1;
=== Passing Drop Down Box Data to CGI Program A drop down box is used when we have many options available but only one or two will be selected. Here is example HTML code for a form with one drop down box [source]
<form action = "/cgi-bin/dropdown.cgi" method = "POST" target = "_blank"> <select name = "dropdown"> <option value = "Maths" selected>Maths</option> <option value = "Physics">Physics</option> </select> <input type = "submit" value = "Submit"> </form>
The result of this code is the following form − Below is the *dropdown.cgi* script to handle input given by web browser. [source]
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $subject = $FORM{dropdown};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>Dropdown Box - Sixth CGI Program</title>"; print "</head>"; print "<body>"; print "<h2> Selected Subject is $subject</h2>"; print "</body>"; print "</html>";
1;
=== Using Cookies in CGI HTTP protocol is a stateless protocol. But for a commercial website it is required to maintain session information among different pages. For example one user registration ends after transactions which spans through many pages. But how to maintain user's session information across all the web pages? In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics. === How It Works Your server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as a plain text record on the visitor's hard drive. Now, when the visitor arrives at another page on your site, the cookie is available for retrieval. Once retrieved, your server knows/remembers what was stored. Cookies are a plain text data record of 5 variable-length fields − . *Expires* − The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser. . *Domain* − The domain name of your site. . *Path* − The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page. . *Secure* − If this field contains the word "secure" then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists. . *Name = Value* − Cookies are set and retrviewed in the form of key and value pairs. === Setting up Cookies It is very easy to send cookies to browser. These cookies will be sent along with the HTTP Header. Assuming you want to set UserID and Password as cookies. So it will be done as follows − [source]
#!/usr/bin/perl
print "Set-Cookie:UserID = XYZ;\n"; print "Set-Cookie:Password = XYZ123;\n"; print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT";\n"; print "Set-Cookie:Domain = www.tutorialspoint.com;\n"; print "Set-Cookie:Path = /perl;\n"; print "Content-type:text/html\r\n\r\n"; ………..Rest of the HTML Content goes here….
Here we used *Set-Cookie* HTTP header to set cookies. It is optional to set cookies attributes like Expires, Domain, and Path. It is important to note that cookies are set before sending magic line *"Content-type:text/html\r\n\r\n*. === Retrieving Cookies It is very easy to retrieve all the set cookies. Cookies are stored in CGI environment variable HTTP_COOKIE and they will have following form. [source]
key1 = value1;key2 = value2;key3 = value3….
Here is an example of how to retrieve cookies. [source]
#!/usr/bin/perl $rcvd_cookies = $ENV{'HTTP_COOKIE'}; @cookies = split /;/, $rcvd_cookies; foreach $cookie ( @cookies ) { ($key, $val) = split(/=/, $cookie); # splits on the first =. $key =~ s/^\s+//; $val =~ s/^\s+//; $key =~ s/\s+$//; $val =~ s/\s+$//; if( $key eq "UserID" ) { $user_id = $val; } elsif($key eq "Password") { $password = $val; } } print "User ID = $user_id\n"; print "Password = $password\n";
This will produce the following result, provided above cookies have been set before calling retrieval cookies script. [source]
User ID = XYZ Password = XYZ123
=== CGI Modules and Libraries You will find many built-in modules over the internet which provides you direct functions to use in your CGI program. Following are the important once. . link:http://search.cpan.org/src/LDS/CGI.pm-3.25/cgi_docs.html[CGI Module] . link:http://cgi-lib.berkeley.edu/[Berkeley cgi-lib.pl] == Perl - Packages and Modules === What are Packages? The *package* statement switches the current naming context to a specified namespace (symbol table). Thus − . A package is a collection of code which lives in its own namespace. . A namespace is a named collection of unique variable names (also called a symbol table). . Namespaces prevent variable name collisions between packages. . Packages enable the construction of modules which, when used, won't clobber variables and functions outside of the modules's own namespace. . The package stays in effect until either another package statement is invoked, or until the end of the current block or file. . You can explicitly refer to variables within a package using the *::* package qualifier. Following is an example having main and Foo packages in a file. Here special variable __PACKAGE__ has been used to print the package name. [source]
#!/usr/bin/perl
This is main package
$i = 1; print "Package name : " , PACKAGE , " $i\n";
package Foo; # This is Foo package $i = 10; print "Package name : " , PACKAGE , " $i\n";
package main; # This is again main package $i = 100; print "Package name : " , PACKAGE , " $i\n"; print "Package name : " , PACKAGE , " $Foo::i\n";
1;
When above code is executed, it produces the following result − [source]
Package name : main 1 Package name : Foo 10 Package name : main 100 Package name : main 10
=== BEGIN and END Blocks You may define any number of code blocks named BEGIN and END, which act as constructors and destructors respectively. [source]
BEGIN { … } END { … } BEGIN { … } END { … }
. Every *BEGIN* block is executed after the perl script is loaded and compiled but before any other statement is executed. . Every END block is executed just before the perl interpreter exits. . The BEGIN and END blocks are particularly useful when creating Perl modules. Following example shows its usage − [source]
#!/usr/bin/perl
package Foo; print "Begin and Block Demo\n";
BEGIN { print "This is BEGIN Block\n" }
END { print "This is END Block\n" }
1;
When above code is executed, it produces the following result − [source]
This is BEGIN Block Begin and Block Demo This is END Block
=== What are Perl Modules? A Perl module is a reusable package defined in a library file whose name is the same as the name of the package with a .pm as extension. A Perl module file called *Foo.pm* might contain statements like this. [source]
#!/usr/bin/perl
package Foo; sub bar { print "Hello $_[0]\n" }
sub blat { print "World $_[0]\n" } 1;
Few important points about Perl modules . The functions *require* and *use* will load a module. . Both use the list of search paths in *@INC* to find the module. . Both functions *require* and *use* call the *eval* function to process the code. . The *1;* at the bottom causes eval to evaluate to TRUE (and thus not fail). === The Require Function A module can be loaded by calling the *require* function as follows − [source]
#!/usr/bin/perl
require Foo;
Foo::bar( "a" ); Foo::blat( "b" );
You must have noticed that the subroutine names must be fully qualified to call them. It would be nice to enable the subroutine *bar* and *blat* to be imported into our own namespace so we wouldn't have to use the Foo:: qualifier. === The Use Function A module can be loaded by calling the *use* function. [source]
#!/usr/bin/perl
use Foo;
bar( "a" ); blat( "b" );
Notice that we didn't have to fully qualify the package's function names. The *use* function will export a list of symbols from a module given a few added statements inside a module. [source]
require Exporter; @ISA = qw(Exporter);
Then, provide a list of symbols (scalars, lists, hashes, subroutines, etc) by filling the list variable named *@EXPORT*: For Example − [source]
package Module;
require Exporter; @ISA = qw(Exporter); @EXPORT = qw(bar blat);
sub bar { print "Hello $[0]\n" } sub blat { print "World $[0]\n" } sub splat { print "Not $_[0]\n" } # Not exported!
1;
=== Create the Perl Module Tree When you are ready to ship your Perl module, then there is standard way of creating a Perl Module Tree. This is done using *h2xs* utility. This utility comes along with Perl. Here is the syntax to use h2xs − [source]
$h2xs -AX -n ModuleName
For example, if your module is available in *Person.pm* file, then simply issue the following command − [source]
$h2xs -AX -n Person
This will produce the following result − [source]
Writing Person/lib/Person.pm Writing Person/Makefile.PL Writing Person/README Writing Person/t/Person.t Writing Person/Changes Writing Person/MANIFEST
Here is the descritpion of these options − . *-A* omits the Autoloader code (best used by modules that define a large number of infrequently used subroutines). . *-X* omits XS elements (eXternal Subroutine, where eXternal means external to Perl, i.e., C). . *-n* specifies the name of the module. So above command creates the following structure inside Person directory. Actual result is shown above. . Changes . Makefile.PL . MANIFEST (contains the list of all files in the package) . README . t/ (test files) . lib/ ( Actual source code goes here So finally, you *tar* this directory structure into a file Person.tar.gz and you can ship it. You will have to update README file with the proper instructions. You can also provide some test examples files in t directory. === Installing Perl Module Download a Perl module in the form tar.gz file. Use the following sequence to install any Perl Module *Person.pm* which has been downloaded in as *Person.tar.gz* file. [source]
tar xvfz Person.tar.gz cd Person perl Makefile.PL make make install
The Perl interpreter has a list of directories in which it searches for modules (global array @INC). == Perl - Process Management You can use Perl in various ways to create new processes as per your requirements. This tutorial will list down few important and most frequently used methods of creating and managing Perl processes. . You can use special variables *$$* or *$PROCESS_ID* to get current process ID. . Every process created using any of the mentioned methods, maintains its own virtual environment with-in *%ENV* variable. . The *exit()* function always exits just the child process which executes this function and the main process as a whole will not exit unless all running child-processes have exited. . All open handles are dup()-ed in child-processes, so that closing any handles in one process does not affect the others. === Backstick Operator This simplest way of executing any Unix command is by using backstick operator. You simply put your command inside the backstick operator, which will result in execution of the command and returns its result which can be stored as follows − [source]
#!/usr/bin/perl
@files = ls -l
;
foreach $file (@files) { print $file; }
1;
When the above code is executed, it lists down all the files and directories available in the current directory − [source]
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14 drwxr-xr-x 4 root root 4096 Sep 13 07:54 android -rw-r—r-- 1 root root 574 Sep 17 15:16 index.htm drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01 -rw-r—r-- 1 root root 71 Sep 17 15:16 test.pl drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
=== The system() Function You can also use *system()* function to execute any Unix command, whose output will go to the output of the perl script. By default, it is the screen, i.e., STDOUT, but you can redirect it to any file by using redirection operator > − [source]
#!/usr/bin/perl
system( "ls -l")
1;
When above code is executed, it lists down all the files and directories available in the current directory − [source]
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14 drwxr-xr-x 4 root root 4096 Sep 13 07:54 android -rw-r—r-- 1 root root 574 Sep 17 15:16 index.htm drwxr-xr-x 3 544 401 4096 Jul 6 16:49 MIME-Lite-3.01 -rw-r—r-- 1 root root 71 Sep 17 15:16 test.pl drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
Be careful when your command contains shell environmental variables like $PATH or $HOME. Try following three scenarios − [source]
#!/usr/bin/perl
$PATH = "I am Perl Variable";
system('echo $PATH'); # Treats $PATH as shell variable system("echo $PATH"); # Treats $PATH as Perl variable system("echo \$PATH"); # Escaping $ works.
1;
When above code is executed, it produces the following result depending on what is set in shell variable $PATH. [source]
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin I am Perl Variable /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
=== The fork() Function Perl provides a *fork()* function that corresponds to the Unix system call of the same name. On most Unix-like platforms where the fork() system call is available, Perl's fork() simply calls it. On some platforms such as Windows where the fork() system call is not available, Perl can be built to emulate fork() at the interpreter level. The fork() function is used to clone a current process. This call create a new process running the same program at the same point. It returns the child pid to the parent process, 0 to the child process, or undef if the fork is unsuccessful. You can use *exec()* function within a process to launch the requested executable, which will be executed in a separate process area and exec() will wait for it to complete before exiting with the same exit status as that process. [source]
#!/usr/bin/perl
if(!defined($pid = fork())) { # fork returned undef, so unsuccessful die "Cannot fork a child: $!"; } elsif ($pid == 0) { print "Printed by child process\n"; exec("date") || die "can’t exec date: $!";
} else { # fork returned 0 nor undef # so this branch is parent print "Printed by parent process\n"; $ret = waitpid($pid, 0); print "Completed process id: $ret\n";
}
1;
When above code is executed, it produces the following result − [source]
Printed by parent process Printed by child process Tue Sep 17 15:41:08 CDT 2013 Completed process id: 17777
The *wait()* and *waitpid()* can be passed as a pseudo-process ID returned by fork(). These calls will properly wait for the termination of the pseudo-process and return its status. If you fork without ever waiting on your children using *waitpid()* function, you will accumulate zombies. On Unix systems, you can avoid this by setting $SIG{CHLD} to "IGNORE" as follows − [source]
#!/usr/bin/perl
local $SIG{CHLD} = "IGNORE";
if(!defined($pid = fork())) { # fork returned undef, so unsuccessful die "Cannot fork a child: $!"; } elsif ($pid == 0) { print "Printed by child process\n"; exec("date") || die "can’t exec date: $!";
} else { # fork returned 0 nor undef # so this branch is parent print "Printed by parent process\n"; $ret = waitpid($pid, 0); print "Completed process id: $ret\n";
}
1;
When above code is executed, it produces the following result − [source]
Printed by parent process Printed by child process Tue Sep 17 15:44:07 CDT 2013 Completed process id: -1
=== The kill() Function Perl *kill('KILL', (Process List))* function can be used to terminate a pseudo-process by passing it the ID returned by fork(). Note that using kill('KILL', (Process List)) on a pseudo-process() may typically cause memory leaks, because the thread that implements the pseudo-process does not get a chance to clean up its resources. You can use *kill()* function to send any other signal to target processes, for example following will send SIGINT to a process IDs 104 and 102 − [source]
#!/usr/bin/perl
kill('INT', 104, 102);
1;
== Perl - Embedded Documentation You can embed Pod (Plain Old Text) documentation in your Perl modules and scripts. Following is the rule to use embedded documentation in your Perl Code − Start your documentation with an empty line, a =*head1* command at the beginning, and end it with a =*cut* Perl will ignore the Pod text you entered in the code. Following is a simple example of using embedded documentation inside your Perl code − [source]
#!/usr/bin/perl
print "Hello, World\n";
=head1 Hello, World Example This example demonstrate very basic syntax of Perl. =cut
print "Hello, Universe\n";
When above code is executed, it produces the following result − [source]
Hello, World Hello, Universe
If you're going to put your Pod at the end of the file, and you're using an __END__ or __DATA__ cut mark, make sure to put an empty line there before the first Pod command as follows, otherwise without an empty line before the =*head1*, many translators wouldn't have recognized the =*head1* as starting a Pod block. [source]
#!/usr/bin/perl
print "Hello, World\n";
while(<DATA>) { print $_; }
END
=head1 Hello, World Example This example demonstrate very basic syntax of Perl. print "Hello, Universe\n";
When above code is executed, it produces the following result − [source]
Hello, World
=head1 Hello, World Example This example demonstrate very basic syntax of Perl. print "Hello, Universe\n";
Let's take one more example for the same code without reading DATA part − [source]
#!/usr/bin/perl
print "Hello, World\n";
END
=head1 Hello, World Example This example demonstrate very basic syntax of Perl. print "Hello, Universe\n";
When above code is executed, it produces the following result − [source]
Hello, World
=== What is POD? Pod is a simple-to-use markup language used for writing documentation for Perl, Perl programs, and Perl modules. There are various translators available for converting Pod to various formats like plain text, HTML, man pages, and more. Pod markup consists of three basic kinds of paragraphs − . *Ordinary Paragraph* − You can use formatting codes in ordinary paragraphs, for bold, italic, code-style , hyperlinks, and more. . *Verbatim Paragraph* − Verbatim paragraphs are usually used for presenting a codeblock or other text which does not require any special parsing or formatting, and which shouldn't be wrapped. . *Command Paragraph* − A command paragraph is used for special treatment of whole chunks of text, usually as headings or parts of lists. All command paragraphs start with =, followed by an identifier, followed by arbitrary text that the command can use however it pleases. Currently recognized commands are − [source]
=pod =head1 Heading Text =head2 Heading Text =head3 Heading Text =head4 Heading Text =over indentlevel =item stuff =back =begin format =end format =for format text… =encoding type =cut
=== POD Examples Consider the following POD − [source]
=head1 SYNOPSIS Copyright 2005 [TUTORIALSOPOINT]. =cut
You can use *pod2html* utility available on Linux to convert above POD into HTML, so it will produce following result − == Copyright 2005 [TUTORIALSOPOINT]. Next, consider the following example − [source]
=head2 An Example List
=over 4 =item * This is a bulleted list. =item * Here’s another item. =back =begin html <p> Here’s some embedded HTML. In this block I can include images, apply <span style="color: green"> styles</span>, or do anything else I can do with HTML. pod parsers that aren’t outputting HTML will completely ignore it. </p>
=end html
When you convert the above POD into HTML using pod2html, it will produce the following result − [source]
An Example List This is a bulleted list. Here’s another item. Here’s some embedded HTML. In this block I can include images, apply styles, or do anything else I can do with HTML. pod parsers that aren’t outputting HTML will completely ignore it.
== Perl - Functions References Here is the list of all the important functions supported by standard Perl. . link:../perl/perl_abs.html[abs] - absolute value function . link:../perl/perl_accept.html[accept] - accept an incoming socket connect . link:../perl/perl_alarm.html[alarm] - schedule a SIGALRM . link:../perl/perl_atan2.html[atan2] - arctangent of Y/X in the range -PI to PI . link:../perl/perl_bind.html[bind] - binds an address to a socket . link:../perl/perl_binmode.html[binmode] - prepare binary files for I/O . link:../perl/perl_bless.html[bless] - create an object . link:../perl/perl_caller.html[caller] - get context of the current subroutine call . link:../perl/perl_chdir.html[chdir] - change your current working directory . link:../perl/perl_chmod.html[chmod] - changes the permissions on a list of files . link:../perl/perl_chomp.html[chomp] - remove a trailing record separator from a string . link:../perl/perl_chop.html[chop] - remove the last character from a string . link:../perl/perl_chown.html[chown] - change the owership on a list of files . link:../perl/perl_chr.html[chr] - get character this number represents . link:../perl/perl_chroot.html[chroot] - make directory new root for path lookups . link:../perl/perl_close.html[close] - close file (or pipe or socket) handle . link:../perl/perl_closedir.html[closedir] - close directory handle . link:../perl/perl_connect.html[connect] - connect to a remote socket . link:../perl/perl_continue.html[continue] - optional trailing block in a while or foreach . link:../perl/perl_cos.html[cos] - cosine function . link:../perl/perl_crypt.html[crypt] - one-way passwd-style encryption . link:../perl/perl_dbmclose.html[dbmclose] - breaks binding on a tied dbm file . link:../perl/perl_dbmopen.html[dbmopen] - create binding on a tied dbm file . link:../perl/perl_defined.html[defined] - test whether a value, variable, or function is defined or not . link:../perl/perl_delete.html[delete] - deletes a value from a hash . link:../perl/perl_die.html[die] - raise an exception or bail out . link:../perl/perl_do.html[do] - turn a BLOCK into a TERM . link:../perl/perl_dump.html[dump] - create an immediate core dump . link:../perl/perl_each.html[each] - retrieve the next key/value pair from a hash . link:../perl/perl_endgrent.html[endgrent] - be done using group file . link:../perl/perl_endhostent.html[endhostent] - be done using hosts file . link:../perl/perl_endnetent.html[endnetent] - be done using networks file . link:../perl/perl_endprotoent.html[endprotoent] - be done using protocols file . link:../perl/perl_endpwent.html[endpwent] - be done using passwd file . link:../perl/perl_endservent.html[endservent] - be done using services file . link:../perl/perl_eof.html[eof] - test a filehandle for its end . link:../perl/perl_eval.html[eval] - catch exceptions or compile and run code . link:../perl/perl_exec.html[exec] - abandon this program to run another . link:../perl/perl_exists.html[exists] - test whether a hash key is present . link:../perl/perl_exit.html[exit] - terminate this program . link:../perl/perl_exp.html[exp] - raise I to a power . link:../perl/perl_fcntl.html[fcntl] - file control system call . link:../perl/perl_fileno.html[fileno] - return file descriptor from filehandle . link:../perl/perl_flock.html[flock] - lock an entire file with an advisory lock . link:../perl/perl_fork.html[fork] - create a new process just like this one . link:../perl/perl_format.html[format] - declare a picture format with use by the write() function . link:../perl/perl_formline.html[formline] - internal function used for formats . link:../perl/perl_getc.html[getc] - get the next character from the filehandle . link:../perl/perl_getgrent.html[getgrent] - get next group record . link:../perl/perl_getgrgid.html[getgrgid] - get group record given group user ID . link:../perl/perl_getgrnam.html[getgrnam] - get group record given group name . link:../perl/perl_gethostbyaddr.html[gethostbyaddr] - get host record given its address . link:../perl/perl_gethostbyname.html[gethostbyname] - get host record given name . link:../perl/perl_gethostent.html[gethostent] - get next hosts record . link:../perl/perl_getlogin.html[getlogin] - return who logged in at this tty . link:../perl/perl_getnetbyaddr.html[getnetbyaddr] - get network record given its address . link:../perl/perl_getnetbyname.html[getnetbyname] - get networks record given name . link:../perl/perl_getnetent.html[getnetent] - get next networks record . link:../perl/perl_getpeername.html[getpeername] - find the other end of a socket connection . link:../perl/perl_getpgrp.html[getpgrp] - get process group . link:../perl/perl_getppid.html[getppid] - get parent process ID . link:../perl/perl_getpriority.html[getpriority] - get current nice value . link:../perl/perl_getprotobyname.html[getprotobyname] - get protocol record given name . link:../perl/perl_getprotobynumber.html[getprotobynumber] - get protocol record numeric protocol . link:../perl/perl_getprotoent.html[getprotoent] - get next protocols record . link:../perl/perl_getpwent.html[getpwent] - get next passwd record . link:../perl/perl_getpwnam.html[getpwnam] - get passwd record given user login name . link:../perl/perl_getpwuid.html[getpwuid] - get passwd record given user ID . link:../perl/perl_getservbyname.html[getservbyname] - get services record given its name . link:../perl/perl_getservbyport.html[getservbyport] - get services record given numeric port . link:../perl/perl_getservent.html[getservent] - get next services record . link:../perl/perl_getsockname.html[getsockname] - retrieve the sockaddr for a given socket . link:../perl/perl_getsockopt.html[getsockopt] - get socket options on a given socket . link:../perl/perl_glob.html[glob] - expand filenames using wildcards . link:../perl/perl_gmtime.html[gmtime] - convert UNIX time into record or string using Greenwich time format. . link:../perl/perl_goto.html[goto] - create spaghetti code . link:../perl/perl_grep.html[grep] - locate elements in a list test true against a given criterion . link:../perl/perl_hex.html[hex] - convert a string to a hexadecimal number . link:../perl/perl_import.html[import] - patch a module's namespace into your own . link:../perl/perl_index.html[index] - find a substring within a string . link:../perl/perl_int.html[int] - get the integer portion of a number . link:../perl/perl_ioctl.html[ioctl] - system-dependent device control system call . link:../perl/perl_join.html[join] - join a list into a string using a separator . link:../perl/perl_keys.html[keys] - retrieve list of indices from a hash . link:../perl/perl_kill.html[kill] - send a signal to a process or process group . link:../perl/perl_last.html[last] - exit a block prematurely . link:../perl/perl_lc.html[lc] - return lower-case version of a string . link:../perl/perl_lcfirst.html[lcfirst] - return a string with just the next letter in lower case . link:../perl/perl_length.html[length] - return the number of bytes in a string . link:../perl/perl_link.html[link] - create a hard link in the filesytem . link:../perl/perl_listen.html[listen] - register your socket as a server . link:../perl/perl_local.html[local] - create a temporary value for a global variable (dynamic scoping) . link:../perl/perl_localtime.html[localtime] - convert UNIX time into record or string using local time . link:../perl/perl_lock.html[lock] - get a thread lock on a variable, subroutine, or method . link:../perl/perl_log.html[log] - retrieve the natural logarithm for a number . link:../perl/perl_lstat.html[lstat] - stat a symbolic link . link:../perl/perl_m.html[m] - match a string with a regular expression pattern . link:../perl/perl_map.html[map] - apply a change to a list to get back a new list with the changes . link:../perl/perl_mkdir.html[mkdir] - create a directory . link:../perl/perl_msgctl.html[msgctl] - SysV IPC message control operations . link:../perl/perl_msgget.html[msgget] - get SysV IPC message queue . link:../perl/perl_msgrcv.html[msgrcv] - receive a SysV IPC message from a message queue . link:../perl/perl_msgsnd.html[msgsnd] - send a SysV IPC message to a message queue . link:../perl/perl_my.html[my] - declare and assign a local variable (lexical scoping) . link:../perl/perl_next.html[next] - iterate a block prematurely . link:../perl/perl_no.html[no] - unimport some module symbols or semantics at compile time . link:../perl/perl_oct.html[oct] - convert a string to an octal number . link:../perl/perl_open.html[open] - open a file, pipe, or descriptor . link:../perl/perl_opendir.html[opendir] - open a directory . link:../perl/perl_ord.html[ord] - find a character's numeric representation . link:../perl/perl_our.html[our] - declare and assign a package variable (lexical scoping) . link:../perl/perl_pack.html[pack] - convert a list into a binary representation . link:../perl/perl_package.html[package] - declare a separate global namespace . link:../perl/perl_pipe.html[pipe] - open a pair of connected filehandles . link:../perl/perl_pop.html[pop] - remove the last element from an array and return it . link:../perl/perl_pos.html[pos] - find or set the offset for the last/next m//g search . link:../perl/perl_print.html[print] - output a list to a filehandle . link:../perl/perl_printf.html[printf] - output a formatted list to a filehandle . link:../perl/perl_prototype.html[prototype] - get the prototype (if any) of a subroutine . link:../perl/perl_push.html[push] - append one or more elements to an array . link:../perl/perl_q.html[q] - singly quote a string . link:../perl/perl_qq.html[qq] - doubly quote a string . link:../perl/perl_qr.html[qr] - Compile pattern . link:../perl/perl_quotemeta.html[quotemeta] - quote regular expression magic characters . link:../perl/perl_qw.html[qw] - quote a list of words . link:../perl/perl_qx.html[qx] - backquote quote a string . link:../perl/perl_rand.html[rand] - retrieve the next pseudorandom number . link:../perl/perl_read.html[read] - fixed-length buffered input from a filehandle . link:../perl/perl_readdir.html[readdir] - get a directory from a directory handle . link:../perl/perl_readline.html[readline] - fetch a record from a file . link:../perl/perl_readlink.html[readlink] - determine where a symbolic link is pointing . link:../perl/perl_readpipe.html[readpipe] - execute a system command and collect standard output . link:../perl/perl_recv.html[recv] - receive a message over a Socket . link:../perl/perl_redo.html[redo] - start this loop iteration over again . link:../perl/perl_ref.html[ref] - find out the type of thing being referenced . link:../perl/perl_rename.html[rename] - change a filename . link:../perl/perl_require.html[require] - load in external functions from a library at runtime . link:../perl/perl_reset.html[reset] - clear all variables of a given name . link:../perl/perl_return.html[return] - get out of a function early . link:../perl/perl_reverse.html[reverse] - flip a string or a list . link:../perl/perl_rewinddir.html[rewinddir] - reset directory handle . link:../perl/perl_rindex.html[rindex] - right-to-left substring search . link:../perl/perl_rmdir.html[rmdir] - remove a directory . link:../perl/perl_s.html[s] - replace a pattern with a string . link:../perl/perl_scalar.html[scalar] - force a scalar context . link:../perl/perl_seek.html[seek] - reposition file pointer for random-access I/O . link:../perl/perl_seekdir.html[seekdir] - reposition directory pointer . link:../perl/perl_select.html[select] - reset default output or do I/O multiplexing . link:../perl/perl_semctl.html[semctl] - SysV semaphore control operations . link:../perl/perl_semget.html[semget] - get set of SysV semaphores . link:../perl/perl_semop.html[semop] - SysV semaphore operations . link:../perl/perl_send.html[send] - send a message over a socket . link:../perl/perl_setgrent.html[setgrent] - prepare group file for use . link:../perl/perl_sethostent.html[sethostent] - prepare hosts file for use . link:../perl/perl_setnetent.html[setnetent] - prepare networks file for use . link:../perl/perl_setpgrp.html[setpgrp] - set the process group of a process . link:../perl/perl_setpriority.html[setpriority] - set a process's nice value . link:../perl/perl_setprotoent.html[setprotoent] - prepare protocols file for use . link:../perl/perl_setpwent.html[setpwent] - prepare passwd file for use . link:../perl/perl_setservent.html[setservent] - prepare services file for use . link:../perl/perl_setsockopt.html[setsockopt] - set some socket options . link:../perl/perl_shift.html[shift] - remove the first element of an array, and return it . link:../perl/perl_shmctl.html[shmctl] - SysV shared memory operations . link:../perl/perl_shmget.html[shmget] - get SysV shared memory segment identifier . link:../perl/perl_shmread.html[shmread] - read SysV shared memory . link:../perl/perl_shmwrite.html[shmwrite] - write SysV shared memory . link:../perl/perl_shutdown.html[shutdown] - close down just half of a socket connection . link:../perl/perl_sin.html[sin] - return the sine of a number . link:../perl/perl_sleep.html[sleep] - block for some number of seconds . link:../perl/perl_socket.html[socket] - create a socket . link:../perl/perl_socketpair.html[socketpair] - create a pair of sockets . link:../perl/perl_sort.html[sort] - sort a list of values . link:../perl/perl_splice.html[splice] - add or remove elements anywhere in an array . link:../perl/perl_split.html[split] - split up a string using a regexp delimiter . link:../perl/perl_sprintf.html[sprintf] - formatted print into a string . link:../perl/perl_sqrt.html[sqrt] - square root function . link:../perl/perl_srand.html[srand] - seed the random number generator . link:../perl/perl_stat.html[stat] - get a file's status information . link:../perl/perl_study.html[study] - optimize input data for repeated searches . link:../perl/perl_sub.html[sub] - declare a subroutine, possibly anonymously . link:../perl/perl_substr.html[substr] - get or alter a portion of a stirng . link:../perl/perl_symlink.html[symlink] - create a symbolic link to a file . link:../perl/perl_syscall.html[syscall] - execute an arbitrary system call . link:../perl/perl_sysopen.html[sysopen] - open a file, pipe, or descriptor . link:../perl/perl_sysread.html[sysread] - fixed-length unbuffered input from a filehandle . link:../perl/perl_sysseek.html[sysseek] - position I/O pointer on handle used with sysread and syswrite . link:../perl/perl_system.html[system] - run a separate program . link:../perl/perl_syswrite.html[syswrite] - fixed-length unbuffered output to a filehandle . link:../perl/perl_tell.html[tell] - get current seekpointer on a filehandle . link:../perl/perl_telldir.html[telldir] - get current seekpointer on a directory handle . link:../perl/perl_tie.html[tie] - bind a variable to an object class . link:../perl/perl_tied.html[tied] - get a reference to the object underlying a tied variable . link:../perl/perl_time.html[time] - return number of seconds since 1970 . link:../perl/perl_times.html[times] - return elapsed time for self and child processes . link:../perl/perl_tr.html[tr] - transliterate a string . link:../perl/perl_truncate.html[truncate] - shorten a file . link:../perl/perl_uc.html[uc] - return upper-case version of a string . link:../perl/perl_ucfirst.html[ucfirst] - return a string with just the next letter in upper case . link:../perl/perl_umask.html[umask] - set file creation mode mask . link:../perl/perl_undef.html[undef] - remove a variable or function definition . link:../perl/perl_unlink.html[unlink] - remove one link to a file . link:../perl/perl_unpack.html[unpack] - convert binary structure into normal perl variables . link:../perl/perl_unshift.html[unshift] - prepend more elements to the beginning of a list . link:../perl/perl_untie.html[untie] - break a tie binding to a variable . link:../perl/perl_use.html[use] - load in a module at compile time . link:../perl/perl_utime.html[utime] - set a file's last access and modify times . link:../perl/perl_values.html[values] - return a list of the values in a hash . link:../perl/perl_vec.html[vec] - test or set particular bits in a string . link:../perl/perl_wait.html[wait] - wait for any child process to die . link:../perl/perl_waitpid.html[waitpid] - wait for a particular child process to die . link:../perl/perl_wantarray.html[wantarray] - get void vs scalar vs list context of current subroutine call . link:../perl/perl_warn.html[warn] - print debugging info . link:../perl/perl_write.html[write] - print a picture record . link:../perl/perl_-X.html[-X] - a file test (-r, -x, etc) . link:../perl/perl_y.html[y] - transliterate a string