Perldoc, Day 1


Using my in combination with a use strict; at the top of your Perl scripts means that the interpreter will pick up certain common programming errors. For instance, in the example above, the final print $b would cause a compile-time error and prevent you from running the program. Using strict is highly recommended.

類似 gcc -Wall

unless ( condition )
if (!condition)

Note that the braces are required in Perl, even if you've only got one line in the block.

Too bad

print "LA LA LA\n" while 1;          # loops forever


A list of them is given at the start of perlfunc and you can easily read about any given function by using perldoc -f functionname .

You can read from an open filehandle using the <> operator. In scalar context it reads a single line from the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list:

Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog.

寫的真傳神! hog *grin* :P

When you're done with your filehandles, you should close() them (though to be honest, Perl will clean up after you if you forget):

哈! 好笑! though to e honest. :P

if ($a =~ /foo/) { ... }  # true if $a contains "foo"

The // matching operator is documented in perlop. It operates on $_ by default, or can be bound to another variable using the =~ binding operator (also documented in perlop).

In [perlop.html],
Binary "=~" binds a scalar expression to a pattern match. Certain operations search or modify the string $_ by default. This operator makes that kind of operation work on some other string. The right argument is a search pattern, substitution, or transliteration. The left argument is what is supposed to be searched, substituted, or transliterated instead of the default $_. When used in scalar context, the return value generally indicates the success of the operation. Behavior in list context depends on the particular operator. See "Regexp Quote-Like Operators" for details and perlretut for examples using these operators.

If the right argument is an expression rather than a search pattern, substitution, or transliteration, it is interpreted as a search pattern at run time.

Binary "!~" is just like "=~" except the return value is negated in the logical sense.

These are documented at great length in perlre, but for the meantime, here's a quick cheat sheet:
XDDD 快速騙人小抄 (quick cheat sheet)

while (<>) {
next if /^$/;


while ((c = get()) != NULL) {
printf ("%s\n", c);


第一次嘗試 CPAN install Bundle::CPAN 跑出一大堆鬼東西,看起來這個系統寫的很完整,也很大!

2 則留言:

Cornelius 提到...

常去 Ptt 的 Perl 版可以搜到一些寶,像是好用的模組之類的,特別要搜 Audreyt ( Autrijus ) 的文章,都很有用處。

這個 blog 也有分享一些 Perl

a2n 提到...

Thx for sharing. :-)