Perl: 基本语法参考、配置文件、模板、正则表达式

1 基本语法参考

http://ind.ntou.edu.tw/~dada/cgi/Perlsynx.htm

2 配置文件

http://www.itqun.net/content-detail/93521.html

http://www.9php.com/FAQ/cxsjl/perl/2009/07/4285989147407.html

http://blog.chinaunix.net/u/29291/showart_344126.html

【test.pl】

use Config::IniFiles;

my $cfg = new Config::IniFiles( -file => "test.ini", #配置文件名

-allowcontinue => 1, #是否运行一个参数值写在多行

-reloadwarn => 1,

-nocase => 1 ); #大小写不敏感

@sect = $cfg->Sections;

print "@sect\n";

$a1 = $cfg->val("section1", "a");

$b1 = $cfg->val("section1", "b");

print "$a1 $b1\n\n";

$a2 = $cfg->val("section2", "a");

$b2 = $cfg->val("section2", "b");

@b3 = $cfg->val("section2", "b");

print "a2:$a2\n\nb2:$b2\n\nb3:@b3\n\n";

【test.ini】

[section1]

a=1

b=2

[section2]

a=hello\

world

b=<<EOT

hello

world

EOT

【输出】

section1 section2

1 2

a2:helloworld

b2:hello

world

b3:hello world

3 模板

http://www.lupaworld.com/tutorial-view-aid-8885.html

【test.pl】

print &Template("test.template");

sub Template {

local(*FILE);# filehandle

local($file);# file path

local($HTML);# HTML data

$file = $_[0] || die "Template : No template file specified\n";

open(FILE, "<$file") || die "Template : Couldn't open $file : $!\n";

while (<FILE>) { $HTML .= $_; }

close(FILE);

@contents = qw(小张 小明);

$HTML =~ s/\$(\w+)\$/$contents[$1-1]/g;

return $HTML;

}

【test.template】

$1$你好,我是$2$

【输出】

小张你好,我是小明

4 正则表达式

http://www.chinaunix.net/jh/25/159388.html

9.1 正则表达式的三种形式

首先我们应该知道 Perl 程序中,正则表达式有三种存在形式,他们分别是:

匹配:m/<regexp>;/ (还可以简写为 /<regexp>;/ ,略去 m)

替换:s/<pattern>;/<replacement>;/

转化:tr/<pattern>;/<replacemnt>;/

这篇文章也讲解了正则表达式的语法。