Perl语言入门和提高08

Perl语言入门和提高08

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Spring 2008 Perl 入门和提高 Lesson 8 周晓方 [email protected] 2 Spring 2008 Namespaces & Packages • A namespace stores names (of all its var, sub, fileHd, etc. Each namespace has its own symbol table. • In perl, namespace held in a package . $a = 4; my $b = 'X'; #Don't say my $a … print "In main: $a, $::a, $main::a\n"; package A; $a = 6; print "A, $a, $b\n"; package A::B; $a = 7; print "A::B, $a, $b\n"; package B; $a = 8; 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_name • All names of a package in hash %pack_name:: • " my " vars are lexical , not belongs to any namespaces. Screen Dump In main: 4, 4, 4 A, 6, X A::B, 7, X B, 8, X 4, 6, 7, 8 STDOUT, @, ARGV, … , A::, C::, _, … B::, STDERR, main:: B::, a a a a 3 Spring 2008 Perl modules • Rule 1: Put each package (i.e. module, namespace) in its own file (*.pm) • Rule 2: special variables are always global. Localize (esp. $_) if necessory. • Rule 3: Package with nested name. Store package FDU in ./FDU.pm, store package FDU::ME in ./FDU/ME.pm, and package FDU::ME::SENIOR in ./FDU/ME/SENIOR.pm • Perl search modules in paths given in @INC • Add my modules to perl search path: use lib Mod_Base or use lib "path" 4 Spring 2008 Perl modules, @INC , %INC • To 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 for modules. Run this and get: perl -e "print join ', ', @INC;"; C:/Perl/lib, C:/Perl/site/lib, . Perl will search “ mylib ” in c:/perl/lib/mylib.pm, c:/perl/site/lib/mylib.pm, ./mylib.pm, one by one. • %INC (module_name=>file_location) contains all modules included via do , require , use . 5 Spring 2008 Don ’ t reinvent the wheel • Check existence 检查库路径 perl – e "print join ' ', @INC" 检查特定模块 perl -e "use TK;" 查看文档 perldoc perlmodref 查看 CPAN http://search.CPAN.org/ • Install new modules Find and download from CPAN, unpack it perl Makefile.PL # 产生 Makefile nmake # 或 make, 运行 Makefile nmake test # 测试 nmake install # 安装 • ActivePerl 5.6/5.8 已经预装了大量模块 6 Spring 2008 Standard module —— Benchmark • Manual page: perldoc -m Benchmark • Usage: #!/usr/bin/perl -w use strict; use Benchmark; timethis(10000000, 'sqrt(12.34)'); 1; => timethis 10000000: 1 wallclock secs ( 0.45 usr + 0.00 sys = 0.45 CPU) • 参考 "Instant Perl Modules" " 即时应用...
View Full Document

This note was uploaded on 11/27/2011 for the course CS Perl taught by Professor Guo during the Spring '09 term at Xiamen University.

Page1 / 3

Perl语言入门和提高08

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online