Scroll to navigation

PERLFAQ(7) Perl Programmers Reference Guide PERLFAQ(7)


perlfaq - Perl 常问问题集 (2003/01/31 17:37:17)


perlfaq 按照主题划分为多个文档,在本文档的末尾有一个目录。
Where to get the perlfaq 如何取得这份文件?
这份文件会定期投到 comp.lang.perl.misc。也可以在许多网站上找到它,例如 还有 译者注:两只老虎工作室的位置是 还有 还有 <>
How to contribute to the perlfaq 如何为 perlfaq 做贡献?
你可以将更正,扩充以及建议以邮件方式发送到 . 这个地址不能用来提问题,它只用来修正当前的 FAQ。如果有问题那么投递到新闻组 comp.lang.perl.misc。可以在 查看源码树 (它是在主源码树之外)。CVS 仓库记录了对 FAQ 的所有改变。
What will happen if you mail your Perl programming problems to the authors 如果把编程问题寄给作者怎么样?
作者大概不会理会您所提的问题,除非您是在建议把一些新问题加进 FAQ去,但如果是这样的话这些问题当初就该寄到 处才对。
您应该已经阅读了这份 FAQ 的第二部分,您应该知道 comp.lang.perl.misc 才是寻求免费建议的适当地方。如果您的问题真的很重要而且您急着要得到正确解答,那麽您该请一个顾问。

Credits 致谢

最初的 perlfaq 由 Tom Christiansen 完成,接着在 Tom 和 Nathan Torkington 的合作下得以扩展。当前的文档由 perlfaq-workers ( 进行维护,很多人贡献了解答,更正和评注。

Author and Copyright Information

Copyright (c) 1997-2003 Tom Christiansen, Nathan Torkington, and other contributors noted in the answers.
All rights reserved.
Bundled Distributions
This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.
Irrespective of its distribution, all code examples in these files are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.
This information is offered in good faith and in the hope that it may be of use, but is not guaranteed to be correct, up to date, or suitable for any particular purpose whatsoever. The authors accept no liability in respect of this information or its use.

Table of Contents 目录

perlfaq - 本文档
perlfaq1 - 有关 Perl 的一般问题
perlfaq2 - 获取和学习 Perl
perlfaq3 - 编程工具
perlfaq4 - 数据操作
perlfaq5 - 文件和格式
perlfaq6 - 正则表达式
perlfaq7 - 综合的问题
perlfaq8 - 系统交互
perlfaq9 - 网络

The Questions 问题

perlfaq1: 有关 Perl 的一般问题
有关 Perl 的一般的,非技术层次的问题
什么是 Perl?
谁对 Perl 提供支持?谁开发了它?为什么它是自由的?
我该用哪一个版本的 Perl?
perl4 和 perl5 各代表什麽?
perl6 是什么?
Perl和其他的程设语言比起来如何?例如 Java, Python, REXX, Scheme,或 Tcl?
我可以用 Perl来做【某种差事】吗?
哪些场合下不适合用 Perl?
Perl程式应算是 program还是 script?
JAPH 是什麽?
到哪儿可拿到 Larry Wall 的智慧讽语 (witticisms)?
我要如何取信、说服我的系统管理者/上司/属下使用第 5/5.8.3 版的 Perl,而不去用其他的语言?
perlfaq2: 获取和学习 Perl
从哪里寻找 Perl 的源程序和文档,支持以及相关事项
哪些平台上有 Perl?要到哪里去找?
要如何取得以二进制形式发行的 Perl?
我的系统里没有 C编译器。要如何编译 perl?
我直接将 Perl的执行档从一台机器上复制到另一台机器上,但是程式跑不起来。
我抓回了原始码,试着编译 perl,但是 gdbm/dynamic loading/malloc/linking/...部分失败。要如何将它搞定?
Perl有哪些模组和延伸? CPAN是什麽? CPAN/src/...又代表什麽?
是不是有一个经 ISO【国际标准局】或 ANSI【美国国家标准局】认可的 Perl版本?
USENET上有哪些专门讨论 Perl的新闻讨论群?问题该投到哪里?
Perl 书籍
和 Perl 有关的杂志
网路上的 Perl:接触 FTP 和 WWW
有哪些讨论 Perl 的邮件列表?
comp.lang.perl.misc 的档案库
如何购买商业版本的 Perl?
如果发现 bugs要向何处报告?
什麽是 Perl Mongers?
perlfaq3: 编程工具
我如何作 (任何事)?
如何以交互的方式使用 Perl?
有 Perl shell吗?
如何替我的 Perl 程式除虫?
如何检测 (profile) 我的 perl 程式?
如何替我的 Perl程式作交叉参考 (cross-reference)?
有 Perl专用的美化列印程式 (pretty-printer)吗?
有 Perl的 ctags 吗?
有没有一个 IDE 或图形界面的 Perl 编辑器?
哪儿有 vi 用的 Perl 宏?
给 emacs用的 perl模式又要去哪抓呢?
如何在 Perl里使用 curses?
X或 Tk如何与 Perl配合呢?
如何不靠 CGI或 Tk 帮助作出简单的目录(选单)?
如何让我的 Perl程式跑得更快些?
如何让我的 Perl 程序少用一些内存?
如何让我的 CGI脚本 (script)执行起来更有效率?
如何隐藏 Perl程式的原始码?
如何把我的 Perl程式码编译成 byte code或 C?
怎样把 Perl 编译成 Java?
如何才能让 "#!perl" 在 [MS-DOS,NT,...] 下起作用?
为何一行的 perl 程式无法在我的 DOS/Mac/VMS系统上运作?
我得去哪里学 Perl的 CGI或是 Web程式设计呢?
从哪里可以学习面向对象的 Perl 编程?
从哪里可以学习将 Perl 与 C 连接?[h2xs, xsubpp]
我已经阅读了 perlembed,perlguts 等等,但是还是不能在我的 C 程序中嵌入 perl;我作错了什么?
什么是 MakeMaker?
perlfaq4: 数据操作
Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
Why isn't my octal data interpreted correctly?
Does Perl have a round() function? What about ceil() and floor()? Trig functions?
How do I convert between numeric representations?
Why doesn't & work the way I want it to?
How do I multiply matrices?
How do I perform an operation on a series of integers?
How can I output Roman numerals?
Why aren't my random numbers random?
How do I get a random number between X and Y?
How do I find the day or week of the year?
How do I find the current century or millennium?
How can I compare two dates and find the difference?
How can I take a string and turn it into epoch seconds?
How can I find the Julian Day?
How do I find yesterday's date?
Does Perl have a Year 2000 problem? Is Perl Y2K compliant?
How do I validate input?
How do I unescape a string?
How do I remove consecutive pairs of characters?
How do I expand function calls in a string?
How do I find matching/nesting anything?
How do I reverse a string?
How do I expand tabs in a string?
How do I reformat a paragraph?
How can I access or change N characters of a string?
How do I change the Nth occurrence of something?
How can I count the number of occurrences of a substring within a string?
How do I capitalize all the words on one line?
How can I split a [character] delimited string except when inside [character]?
How do I strip blank space from the beginning/end of a string?
How do I pad a string with blanks or pad a number with zeroes?
How do I extract selected columns from a string?
How do I find the soundex value of a string?
How can I expand variables in text strings?
What's wrong with always quoting "$vars"?
Why don't my <<HERE documents work?
What is the difference between a list and an array?
What is the difference between $array[1] and @array[1]?
How can I remove duplicate elements from a list or array?
How can I tell whether a certain element is contained in a list or array?
How do I compute the difference of two arrays? How do I compute the intersection of two arrays?
How do I test whether two arrays or hashes are equal?
How do I find the first array element for which a condition is true?
How do I handle linked lists?
How do I handle circular lists?
How do I shuffle an array randomly?
How do I process/modify each element of an array?
How do I select a random element from an array?
How do I permute N elements of a list?
How do I sort an array by (anything)?
How do I manipulate arrays of bits?
Why does defined() return true on empty arrays and hashes?
How do I process an entire hash?
What happens if I add or remove keys from a hash while iterating over it?
How do I look up a hash element by value?
How can I know how many entries are in a hash?
How do I sort a hash (optionally by value instead of key)?
How can I always keep my hash sorted?
What's the difference between "delete" and "undef" with hashes?
Why don't my tied hashes make the defined/exists distinction?
How do I reset an each() operation part-way through?
How can I get the unique keys from two hashes?
How can I store a multidimensional array in a DBM file?
How can I make my hash remember the order I put elements into it?
Why does passing a subroutine an undefined element in a hash create it?
How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
How can I use a reference as a hash key?
How do I handle binary data correctly?
How do I determine whether a scalar is a number/whole/integer/float?
How do I keep persistent data across program calls?
How do I print out or copy a recursive data structure?
How do I define methods for every class/object?
How do I verify a credit card checksum?
How do I pack arrays of doubles or floats for XS code?
perlfaq5: 文件和格式
I/O and the "f" issues: filehandles, flushing, formats, and footers.
How do I flush/unbuffer an output filehandle? Why must I do this?
How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
How do I count the number of lines in a file?
How can I use Perl's "-i" option from within a program?
How do I make a temporary file name?
How can I manipulate fixed-record-length files?
How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles?
How can I use a filehandle indirectly?
How can I set up a footer format to be used with write()?
How can I write() into a string?
How can I output my numbers with commas added?
How can I translate tildes (~) in a filename?
How come when I open a file read-write it wipes it out?
Why do I sometimes get an "Argument list too long" when I use <*>?
Is there a leak/bug in glob()?
How can I open a file with a leading ">" or trailing blanks?
How can I reliably rename a file?
How can I lock a file?
Why can't I just open(FH, ">file.lock")?
I still don't get locking. I just want to increment the number in the file. How can I do this?
All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?
How do I randomly update a binary file?
How do I get a file's timestamp in perl?
How do I set a file's timestamp in perl?
How do I print to more than one file at once?
How can I read in an entire file all at once?
How can I read in a file by paragraphs?
How can I read a single character from a file? From the keyboard?
How can I tell whether there's a character waiting on a filehandle?
How do I do a "tail -f" in perl?
How do I dup() a filehandle in Perl?
How do I close a file descriptor by number?
Why can't I use "C:\temp\foo" in DOS paths? What doesn't `C:\temp\foo.exe` work?
Why doesn't glob("*.*") get all the files?
Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
How do I select a random line from a file?
Why do I get weird spaces when I print an array of lines?
perlfaq6: 正则表达式
我把一个正规表示式放入 $/但却没有用。错在哪里?
如何在 LHS端【译注:式子中运算元左端部份】作不区别大小写式的替换,但在 RHS端【右端】保留大小写区别?
如何使 "\w" 对应到附重音记号 (accented)的字元?
如何作一个适合不同 locale【译注:语言环境,国家地区在文字编码上各自的惯例】的 "/[a-zA-Z]/" 对应?
在一个正规表示式里如何引入 (quote) 变量?
"/o" 到底是干麽用的?
如何使用正规表示式将档案中 C语言样式的注解删掉?
我能用 Perl的正规表示式去对应成对的符号吗?
为何我用 "\b" 作字界搜寻时会失败呢?
为什麽每当我用 $&, $`,或 $'时程式的速度就慢下来了呢?
正规表示式中的 "\G" 能给我什麽好处?
Perl正规表示引擎是 DFA 或 NFA?它们是 POSIX 相容的吗?
在无返回值上下文中用 grep或 map有什麽不对?
perlfaq7: 综合的问题
综合的 Perl 语言问题,不适于在其他所有段落中讨论的问题
我能拿到 Perl的 BNF/yacc/RE吗?
什麽是一个扩充 (extension)?
为何 Perl运算子的优先顺序和 C的不一样?
如何传递/返回一个{函数 Function, 文件句柄 FileHandle, 数组 Array, 散列 Hash, 方法 Method, 正则表达式 Regex}?
动态和静态作用域有什么区别?local()my() 呢?
为什么 "my($foo) = <FILE>;" 不工作?
如何重定义一个内建函数,操作符 或者方法?
调用函数时 &foo 和 foo() 的形式有什么不同?
如何注释掉大块的 perl 代码?
"bad interpreter" 是什么意思?
perlfaq8: 系统交互
进程间通信 (IPC), 用户界面控制 (键盘,屏幕和指点设备)。
为什么 exec() 不返回?
在 Unix 系统中如何修改 shadow 文件?
如何 sleep()alarm() 少于一秒的时间?
如何做 atexit()或 setjmp()/longjmp()的动作?(异常处理)
为何我的 sockets程式在 System V (Solaris)系统下不能用?「不支持的协议」这个错误讯息又是什麽意思?
如何从 Perl里呼叫系统中独特的 C函数?
在哪里可以找引入档来做 ioctl()或 syscall()?
为何 setuid perl程式会抱怨关於系统核心的问题?
如何打开对某程式既输入又输出的管道 (pipe)?
为何用 system()却得不到一个指令的输出呢?
如何补捉外部指令的 STDERR?
为何当管道开启失败时 open()不会传回错误讯息?
如何不经过 shell处理来呼叫反向引号?
为何给了 EOF(Unix上是 ^D,MS-DOS上是 ^Z)後我的程式就不能从 STDIN读取东西了呢?
如何把 shell程式转成 perl?
perl能处理 telnet或 ftp 会话吗?
如何在 Perl里达到 Expect的功能?
有没有可能将 perl的指令列隐藏起来,以躲避像 "ps"之类的程式?
我在 perl script里 {更动目录,更改我的使用环境}。为何这些改变在程式执行完後就消失了呢?如何让我做的修改显露出来?
如何 fork 一个守护进程?
如何设置 CPU 限额?
如何避免在 Unix 系统中产生僵尸进程?
如何使用 SQL 数据库?
如何使 system() 在收到 control-C 时退出?
如何从 CPAN 安装模块?
require 和 use 的区别是什么?
如何在运行时将一个目录加入到我的 include 路径中?
perlfaq9: 网络
网络通信,互联网以及少量有关 web 的内容
一个 CGI 脚本的回应的正确格式是什么?
我的 CGI 脚本从命令行执行正常,但是在浏览器中不行 (500 Server Error)。
如何从 CGI 程序中得到好一点的错误提示?
如何将字符串中的 HTML 删除?
如何展开 URL?
如何在 HTML 添加一个弹出菜单?
如何获取 HTML 文件?
如何根据提交的内容自动生成一个 HTML ?
如何解码或创建 web 中的 %-encoding?
如何用 Perl 修改我的 .htpasswd 和 .htgroup 文件?
如何确保用户不会在表单中输入使我的 CGI 脚本作坏事的值?
如何解码一个 CGI 表单?
如何解码一个 MIME/BASE64 字符串?
如何使用 MIME 来为邮件消息增加附件?
如何找到我的主机名/域名/IP 地址?
如何获取/上传一个 FTP 文件?
如何进行远程过程调用 RPC ?


2003-11-25 perl v5.8.3