内容文稿教程pl_第1页
内容文稿教程pl_第2页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1Vine’s

Perl

PrimePerl

入门和提高Lesson

8courses@Vine’s

Perl

PrimeNamespaces

&

Packages

命名空间A

namespace

stores

names

(of

allits

var,

sub,

fileHd,etc.

Eachnamespace

has

its

own

symbol

table.In

perl,

namespace

held

ina

package.$a

=

4;

my

$b

=

'X';

#Don't

say

my

$a…print

"In

main:

$a,

$::a,

$main::a\n";package

A;$a

=6;package

A::B;$a

=7;package

B;$a

=8;print

"A,

$a,

$b\n";print

"A::B,

$a,

$b\n";print

"B,

$a,

$b\n";$C::a=9;print

"$::a,

$A::a,

$A::B::a,

$B::a\n";print

join(",

",

keys

%main::),

"\n";print

join(",

",

keys

%A::),"\n";print

join(",

",

keys

%A::B::),

"\n";print

join(",

",

keys

%B::),"\n";print

join(",

",

keys

%C::),"\n";Full

qualified

name:

$@%pack_name::var_nameAll

names

of

a

package

inhash

%pack_name::"my"

vars

are

lexical,

not

belongs

to

any

namespaces.

2Screen

DumpIn

main:

4,

4,

4A,

6,

XA::B,

7,

XB,

8,

X4,

6,

7,

8STDOUT,

@,

ARGV,…,

A::,

C::,

_,…B::,

STDERR,main::B::,aaaa3Vine’s

Perl

PrimePerl

modulesRule

1:

Put

each

package

(i.e.

module,

namespace)

in

its

own

file(*.pm)Rule

2:

special

variables

are

always

global.

Localize

(esp.

$_,

i.e.say

"local

$_;"

in

your

code)

if

necessory.Rule

3:

Package

with

nested

name.Store

package

FDU

in

./FDU.pmStore

package

FDU::ME

in

./FDU/ME.pmStore

package

FDU::ME::SENIORin

./FDU/ME/SENIOR.pmPerl

sear odules

in

pathsgiven

in

@INC,例如Activeperl缺省安装的@INC是(C:/Perl/si

ib,C:/Perl/lib)Add

my

modules

to

perl

search

path:use

lib

Mod_Baseuse

lib"path"

例如use

lib'./lib',

'./blib/lib',

'./blib/arch';4Vine’s

Perl

PrimePerl

modules,

@INC,

%INCTo

include

a

module,

say

use/require

module;'use

module'

includes

a

module

at

compile

time.'require

module'

includes

one

at

run

time.

(avoid)@INC

is

just

like

the

UNIX

$path,

where

perl

search

formodules.

Run

this

and

get:perl

-e"print

join

',

',

@INC;";C:/Perl/lib,C:/Perl/si ib,

.Perl

will

search

“mylib”

in

c:/perl/lib/mylib.pm,c:/perl/si ib/mylib.pm,

./mylib.pm,

one

by

one.%INC

(module_name=>file_location)

contains

allmodules

included

via

do,

require,

use.5Vine’s

Perl

PrimeDon’treinvent

the

wheelCheck

existence检查库路径

检查特定模块perl

–e

"print

join

'

',

@INC"perl

-e

"use

Tk;"查看文档perldoc

perlmodref查看CPANInstall

new

modulesFind

and

download

from

CPAN,

unpack

itperl

Makefile.PLnmakenmake

testnmake

install#产生Makefile#或make,运行Makefile#测试#安装ActivePerl

5.6/5.8已经预装了大量模块6Standard

module——Ben-m

BenVine’s

Perl

PrimearkarkManual

page:

perldocUsage:#!/usr/bin/perl

-wuse

strict;useBen

ark;timethis(10000000,

'sqrt(12.34)');1;=>timethis

10000000: 1

wallclock

secs(0.45

usr

+ 0.00

sys= 0.45

CPU)参考"Instant

Perl

Modules""即时应用Perl模块"人民邮电

2001年7Vine’s

Perl

PrimeArbitrary

Precision--Math::BigInt大数整数运算,重载算术运算符use

Math::BigInt;#注意下面面相对象的写法$i

=

Math::BigInt->new('1234567890');$j

=

new

Math::BigInt

'9876543210';print

$i*$j,

"\n";

#

don't

say

"$i*$j"$two

=

new

Math::BigInt

'2';print

2**500,

"\n";print

$two**500,

"\n";作业:学号-08.pl比较2**500和$two**500的速度结果要显示出两者相差的倍数

(同时用到Math::BigInt和Ben

ark)速度差几个数量级,想办法比较喔Vine’s

Perl

Primeuse的其他用法use

Config;print

join

",

",

@Config{'osname',

'cc',

'ccflags'};MSWin32,

cl,

-nologo

-O1

-MD

-Zi...use

Env;print

$PATH;

#as

$ENV{'PATH'}use

vars

qw($a

$b...);#Perl5.6可以用our变量useExporter;

#把package内的名字别名到外面来use

English;

用英文名字代替难读难记的perl变量082use.pl

Simple.pm

运行结果#!/usr/bin/perl

-wusestrict;use

Config;use

Env;use

Simple;print

join

",

",@Config{'osname',

'cc'};print

"\nPATH

is

$PATH\n";Simple::True();True();1;#!/usr/bin/perl

-wusestrict;package

Simple;

useExporter;use

vars

qw(@ISA

@EXPORT);@ISA=qw(Exporter);@EXPORT=qw(True);sub

True

{print

"I'm

Simple::True.\n";}1;MSWin32,

cl,-……PATH

is

C:\PERL\I'm

Simple::True.I'm

Simple::True.89Vine’s

Perl

Primeuse

的其他用法,Progma要求Perl版本号use

5.005_03;

use

v5.6.1;等use

constant

PI =>

4

*atan2

1,

1;

print

PI*2;use

integer;print

10/3;

强制整型运算,显示3use

lib

LIST;no

lib

LIST;给@INC添加/删除路径;用@INC=@lib::ORIG_INC;恢复use

strict;(和#!/usr/bin/perl-w

一起用)

use

strict“vars”;no

strict“vars”;以及“subs”use

strict“refs”;

符号use

subs

LIST;预定义子程序名,可不带()调用use

vars

LIST;预定义全局变量名,相当于perl5.6+的our内容,参考

文件的Progma部分10Vine’sPerlPrime易读的日期HTTP::DateUse

HTTP::Date;$string

=

time2str($time);#

Format

as

GMT

ASCII

time$time

=

str2time($string);#

convert

ASCIIdatetomachine

timetime2str参数是机器时间(秒计数),返回字符串print

time2str();#缺省表示当前时间è

Thu,24Apr200801:28:32

GMTstr2time($str,$zone)将字符串转换回机器时间;若$zone不是GMT,需要安装Time::ZoneTime::Zone没有预安装的话,上CPAN网11Vine’s

Perl

PrimeCPAN模块安装实例1搜索主页输入模块名或关键字CPAN模块分类浏览12Vine’s

Perl

Prime点击这里这是模块的使用手册原来这个文件安装后包含很多个模块点击这里点击这里都是.tar.gz文件13Vine’s

Perl

Prime务必先读一下READMEThis

is

the

perl5

TimeDate

distribution.

It

requires

perl

version

5.003

or

laterYou

install

the

library

by

running

these

commands:perlMakefile.PLmake

make

testmake

installPlease

report

any

bugs/suggestions

to

Copyright

1996-2000

Graham

Barr.

s

.This

library

is

free

software;

you

can

redistribute

it

and/or

modify

it

under

the

sametermsas

Perl

itself.Share

and

Enjoy!Graham<

>This

distribution

replaces

my

earlier

GetDate

distribution,

which

was

only

a

date

parser.The

date

parser

contained

in

this

distribution

is

far

superior

to

the

yacc

based

parser,

anda

*lot*

fatser.The

parser

contained

here

will

only

parse

absolute

dates,

if

you

want

a

date

parser

thatcan

parse

relative

dates

then

take

a

look

at

theTime

modules

by

David

Muir

on

CPAN.按照这个顺序安装。注意Win32下没有make.exe,要用VisualC自带的nmake.exe,也就是:

perl

Makefile.PLnmake

testnmake

installVine’s

Perl

PrimeCPAN模块 安装实例2一起来找一个产生超文本格式日历的模块打开浏览器连接""查找"calendar

html"浏览结果,进入HTML-Calendar-Simple-0.04首先查看README然后点击Download.tar.gz文件,参考README进行安装这是模块的使用手册点击这里15Vine’s

Perl

Prime仔细查看READMEHTML/Calendar/Simple

version

0.04=================================ATIONOnce

installed,

doaperldoc

HTML::Calendar::SimpleINSTALLATIONTo

install

this

module

type

the

following:perl

Makefile.PLmakemake

testmakeinstallDEPENDENCIESThis

module

requires

these

other

modules

andlibraries:Date::Simple

-

this

is

A

Good

ModuleTest::More

-

you

do

write

tests,

right?(And

do

them

before

you

write

the

code

too.)Copyright

(C)

2002,mwkThis

module

is

free

software;you

can

redistribute

it

or

modify

itunder

the

same

terms

as

Perl

itself.般Perl模块的安装都是这几个命令,在Win32下面要拷贝个nmake.exe到路径中,用nmake而不是make,如果模块带有C代码,还要安装合适的C编译器,具体版本参考perl-V,比如VC++6.0等.依存关系很重要,这个模块要预安装另外三个模块信息,请使用正版和免费的模块CGI

经有了CGI-Aslong

as

I

have

the

latest

version

of

CGI,

I

knowthatmy

HTML

will

be

the

latest

standard!

I

lurve

CGI.pmCOPYRIGHT

AND

LICENCE这两个要手工 安装Vine’s

Perl

Prime安装的Test-Simple-0.53的Date-Simple-2.05逐一查找Test::More查找Date::Simple如果找不到想要的模块,就到

à

Browsing

Perl

modulesàall

modules

(a

long

list)里面去找(现有七千五百多个)现在共

了三个模块,分别建立临时

,解包安装–

6,479Byte

HTML-Calendar-Simple-0.04.tar.gz–

19,109Byte–

57,969ByteDate-Simple-2.05.tar.gzTest-Simple-0.53.tar.gz如果安装中nmake

test步骤通不过,要仔细查找原因nmake

install会将上述代码

到c:/perl/si ib

或者c:/perl/lib下面。安装好后可以删除临时安装

。16根据依赖关系,要先安装这两个Vine’s

Perl

Prime安装时的问题和解决安装Date-Simple-2.05模块中有XS接口的C代码,要安装C语言编译器,版本应该和编译Perl的C编译器一致VC++5,

if

ActivePerl

5.003

()VC++6,

if

ActivePerl

5.6

(包含了 的模块)VC.net,if

ActivePerl

5.8(unicode支持)对应版本的gcc,if

Perl

onUNIX/Linux,etc安装Test-Simple-0.53test没有完全通过,和win32平台有关忽略这些failure,继续安装安装HTML-Calendar-Simple-0.04test有语法警告,是HTML-Calendar-Simple的问题,继续17马上学习、实践HTML::Calendar::SimpleVine’s

Perl

Prime册点击网页,或者perldoc

HTML::Calendar::Simple编写小程序并运行085calendar.pluse

HTML::Calendar::Simple;my

$cal

=

HTML::Calendar::Simple->new;my

$month

=

$cal->calendar_month;my$year

=

HTML::Calendar::Simple->calendar_year;open

M,

">093month.htm";print

M

$month;closeM;open

Y,

">093year.htm";print

Y

$year;closeY;1;D:>

perl

085calendar.plD:>

start

085month.htmD:>

start

085year.htm在中文版windows下运行······19Vine’s

Perl

Prime为什么结果是错的找到387行(自己试试看,能否找到这一行)my

@seq =

map

$self->_spacer,(1

..

$days{$start->format("%a")});查看Date::Simple实现format的代码return

POSIX::strftime

($format,_gmtime

($self));查看POSIX::strftime的说明(说明过于简单)上网查看C库函数strftime的说明(?用法复杂?)运行简单的

,测试perl的POSIX::strftimeuse

Date::Simple;my

$td

=Date::Simple->new(2009,

4,

2);my

$fmt

=

join

'

',

map

"$_:\%$_\t",

'a'..'z';print

$td->format($fmt),"\n\n",$td->format(uc$fmt);20Vine’s

Perl

Prime找到原因,临时打补丁a:星期四g:

h:c:2009-4-2

0:00:00o:l:u:d:02

e:

f:m:04

n:v:

w:4p:上午

q:x:2009-4-2b:四i:r:y:09j:092

k:s:

t:z:D:B:四C:A:星期四I:12

J:K:L:E:

F:

G:M:00

N:

O:

P:H:00Q:R:S:00

T:

U:13

V:

W:13

X:0:00:00Y:2009

Z:原因查明:中文win32的"%a"返回"星期四",

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论