Welcome to jaever.com/diary

Perl常见特殊变量

1. $_
$ARG,常常是一个默认变量

2. @_
@ARG,子例程参数表

3. {row.content}
$PROGRAM_NAME,本程序的名字

4. @ARGV
本程序的命令行参数表

5. $"
$LIST_SEPARATOR,数组内插到双引号字符串中时所用的分隔符,
默认为空格

6. $,
$OFS,或$OUTPUT_FIELD_SEPARATOR,用于print的输出字段分隔符,
即一个print语句中用逗号分隔的部分之间用什么分隔,默认为无

7. $
$ORS,或$OUTPUT_RECORD_SEPARATOR,用于print的输出记录分隔符,
即一个print语句结束时末尾添加什么,默认为无

8. $/
$RS,或$INPUT_RECORD_SEPARATOR,输入记录分隔符,改变了readline,
<FH>和chomp对于“行”的看法,默认为换行符

9. $.
$NR,或$INPUT_LINE_NUMBER,最后读取的“行”号(注意行的概念可能
被8所改变)

10. $|
$AUTOFLUSH,或$OUTPUT_AUTOFLUSH,输出缓冲区开关,默认为0,即关

11. $$
$PID,或$PROCESS_ID,本脚本的进程号(PID)

12. $!
$ERRNO,或$OS_ERROR,上一次系统调用错误值

13. $@
$EVAL_ERROR,上一次eval操作错误值

14. $^I
$INPLACE_EDIT,现场编辑的备份文件扩展名

15. %ENV
当前环境变量

16. 与正则表达式相关的几个量
|<-($`)->|<-($&)->|<-($')->|
/ ------- mmmmmmmm ++++++++/ => 正则表达式(注意:空格不代表实际空格)
|       |匹配部分|
0  ($-[0])      ($+[0])

17. $^O
操作系统名称。
或者
use English;
print $OSNAME;
或者
use Config;
print $Config{'osname'};

 

CONTINUE
INFO: 2008-07-21 14:28:00 | purpen | digg | link

perl模块指南

LY from http://fanqiang.chinaunix.net/program/perl/2005-06-28/3349.shtml

 

是否觉得perl中关于模块的文档有些难懂?好的,这里有一个世界上最简单的模块,它将用于展示(demonstrate)Exporter模块所 有的特性,另外还有一段使用这个模块的脚本。同时,我们也会给出一个有关于@INC的简短说明,最后,还要讲一下有些关于using warnings和use模块的使用。

下面是这个模块的内容:

MyModule.pm

package MyModule;

use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);

$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(func1 func2);
%EXPORT_TAGS = ( DEFAULT => [qw(&func1)],
Both => [qw(&func1 &func2)]);

sub func1 { return reverse @_ }
sub func2 { return map{ uc }@_ }

1;

首先,我们将通过声明 "package" 名字来获得一个名字空间。这将确保模块中的方法与变量,和调用他们的代码所分隔开来。 use strict在模块中是一个非常好的做法,这将使Perl对使用全局变量做出一定的约束。详细介绍参看 "use strict warnings and diagnostics or die"

我们需要用Exporter模块来将我们的函数从MyModule::namespace输出到main::namespace,让使用 MyModule的程序可以使用这些函数。 为了use strict,我们必须使用use vars来声明一些变量。当然,在5.6版本以上我们还可以使用our来声明变量。 我们现在设置一个$VERSION数值,然后通过使用@ISA来使得Exporter成为MyModule的一部本。想要了解@ISA是什么以及如何使用 等细节,请参考"perlboot"

@EXPORT包含了我们需要默认输出的函数列表。在这里,它是空的。一般来说,你通过默认的使用@EXPROT输出的越少越好。因为调用该模块的 程序中,有可能存在与其中函数相之冲突的函数或者代码。如果程序需要调用某个指定的函数,那么,就请就让它主动请求。 @EXPORT_OK包含了我们在调用时需要输出的函数列表,我们只输出了&func1和&func2,这种方法要优先于盲目地使用 @EXPORT来输出函数。你也可以输出像$CONFIG这样全局的、不是用my定义的字义范畴的变量。(可参考用 "our" 或者 use vars来声明全局变量) %EXPORT_TAGS.为了方便起见,我们定义了两套输出标签。‘:DEFAULT’标签只输出&func1;‘:Both’标签则输出 &func1和&func2。这个哈希表存储指向数组引用的标签。注意:在这里的数组是匿名的。 最后,我们需要在模块结尾加上一个“1;”。因为当perl装载一个模块时,它会实现查看这个模块是否能在最后返回一个真值,并且据此判断该模块是否已装 载成功。当然,你可以在最后面添加任何真值(参看 "Code::Police" ),但其中1是最方便的。

MySciprt.pl(使用MyModule的一个例子)

#!/usr/bin/perl -w

use strict;

# you may need to set @INC here (see below)

my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !);

# case 1
# use MyModule;
# print func1(@list),"\n";
# print func2(@list),"\n";

# case 2
# use MyModule qw(&func1);
# print func1(@list),"\n";
# print MyModule::func2(@list),"\n";

# case 3
# use MyModule qw(:DEFAULT);
# print func1(@list),"\n";
# print func2(@list),"\n";

# case 4
# use MyModule qw(:Both);
# print func1(@list),"\n";
# print func2(@list),"\n";

正如上面所见,我们在MyScript.pl中使用了MyModule。把中间的注释符号都去掉来看看会发生什么。一次都去掉即可。 Case1:因为我们的模块默认什么都没有输出(没有输出&func1和&func2),所以我们会得到一个他们在main:: namespace中不存在的错误。 Case2:这个运行正常。我们让模块输出了&func1,于是我们可以正常使用它。尽管我们没有输出&func2,但是我们使用的是 &func2完整的包路径,所以它也可以正常工作。 Case3:‘:DEFAULT’标签应该输出&func1,所以你应该希望返回一个缺少&func2函数的错误。但事实上perl却偏 偏找上了&func1的麻烦(错误信息提示未定义&func1函数)。恩,这里怎么了呢?原来,DEFAULT这个标签名字是特殊的,在 我们的模块中,%EXPORT_TAGS哈希表它会被自动设置成这样DEFAULT=>\@EXPROT.也就是说,DEFAULT默认导出的是来 自@EXPROT数组的函数。 Case4:我们指定通过‘:Both’标签实现两个函数都输出,他实现了。 *关于@INC的注意事项* 当你提交一个use MyModule的时候,就会指示perl去搜索@INC数组中是否有此模块名。@INC通常包含:

/perl/lib 
/perl/site/lib
.

“.”这个目录表示当前的工作目录。核心模块是安装在perl/lib目录中,非核心模块安装在perl/site/lib目录中。你可以向@INC中添加自定义目录。像下面这样:

BEGIN { push @INC, '/my/dir' }
# or
BEGIN { unshift @INC, '/my/dir' }
# or
use lib '/my/dir';

我们需要使用BEGIN块在编译时向@INC中添加值,此时是perl检查模块的时刻。

如果你等到程序被编译的时候就太晚了,perl会抛出一个异常,说“在@INC中无法找到MyModule”.使用push还是unshift方法 添加值的区别是,perl搜索@INC的顺序是从@INC中的第一个目录开始的。如果你在/perl/lib/、/perl/site/lib/和./中 都有一个MyModule模块的话,那么/perl/lib中的模块将首先被找到并使用。use lib用法可以起到和BEGIN{unshift @INC,$dir}一样的效果-请参看"perlman:lib:lib":http://www.perlmonks.org/?node= perlman%3Alib%3Alib . *use Foo::Bar意味着什么* use Foo::Bar并不意味着在@INC的目录中寻找一个叫做Foo::Bar.pm的模块文件。它的意思是在@INC的目录中寻找一个叫做‘Foo’的 “子目录”,然后在其中找一个叫做“Bar.pm”的“模块”。 现在,如果我们成功"use"了一个模块,那么我们就可以通过完整的包路径语法&PACKAGE::FUNCTION使用这个模块中的所有函数。 当我们说&Foo::Bar::some_func的时候,我们指的是“包的名字”而不是那个在use中曾使用的包含路径的文件名。这会允许你可 以在一个use过的文件中包含很多包名字。实际使用中这些名字通常是相同的。

use warnings;

你应该打开warnings来检测你的模块,因为它可以检测出很多细微的错误。你可以通过在测试模块代码中添加-w参数来打开警告选项。如果你在模 块中添加了use warnings,那么你的模块必须要求运行在perl5.6以上,否则不支持。如果你在模块的顶端添加了$^W++,那么你将会在全局范围内打开警告选 项-这将影响到其他模块,你最好只在你自己的程序中这么使用,因为这略显霸道了一些。这有一个专家写的叫做"tye":http: //www.perlmonks.org/?node=tye 的代码来测试警告选项,但没有直接将它包含进他/她自己的模块中。 希望这些会讲清楚它是怎样工作的。

CONTINUE
INFO: 2008-07-21 11:19:04 | purpen | digg | link

Perl安装DBD::MySQL (Mac OS X 10.4.10)的烦恼

今天想尝试Perl链接Mysql数据库的操作,可DBD::mysql安装了N次就是不行,遇到的错误:

Cannot find the file 'mysql_config'! Your execution PATH doesn't seem
not contain the path to mysql_config. Resorting to guessed values!
Can't exec "mysql_config": No such file or directory at Makefile.PL 
line 466.

可我添加了--mysql_config的路径,仍然不能成功,最后还是找到把mysql_config的路径直接添加到$PATH中,才安装OK!

<perl Makefile.Pl --testuser=root --testdb=test --testhost=localhost>

make

sudo make install

安装成功,下面进行测试:

#!/usr/bin/perl -w

use DBI;
use strict;
use warnings;

my $dsn = 'DBI:mysql:test:localhost';
my $db_user_name = 'root';
my $db_password = '';

my $dbh = DBI->connect($dsn, $db_user_name, $db_password);

print "$dbh" if "$dbh";

输出<DBI::db=HASH(0x504888)>

应该是DBI对象了,ok继续前进!

 

 

CONTINUE
INFO: 2008-07-19 10:02:11 | purpen | digg | link

Ping 命令完全講解

對於Windows下ping指令相信大家已經再熟悉不過了,但是能把ping的功能發揮到最大的人卻並不是很多,當然我也並不是說我可以讓ping發揮最大的功能,我也只不過經常用ping這個工具,也總結了一些小經驗,現在和大家分享一下。 


現在我就參照ping指令的輔助說明來給大家講我使用ping時會用到的技巧,ping只有在安裝了TCP/IP通訊協定以後才可以使用: 

ping [-t] [-a] [-n count] [-l length] [-f] [-i ttl] [-v tos] [-r 
count] [-s count] [[-j computer-list] | [-k computer-list]] [-w 
timeout] destination-list 

Options: 

-t Ping the specified host until stopped.To see statistics and 
continue - type Control-Break;To stop - type Control-C. 

不停的ping地方主机,直到你按下Control-C。 

此功能?有什麼特別的技巧,不過可以配合其他參數使用,將在下面提到。 

-a Resolve addresses to hostnames. 

解析電腦NetBios名。 

例:C:\>ping -a 192.168.1.21 

Pinging iceblood.yofor.com [192.168.1.21] with 32 bytes of data: 

Reply from 192.168.1.21: bytes=32 time<10ms TTL=254 

Reply from 192.168.1.21: bytes=32 time<10ms TTL=254 

Reply from 192.168.1.21: bytes=32 time<10ms TTL=254 

Reply from 192.168.1.21: bytes=32 time<10ms TTL=254 

Ping statistics for 192.168.1.21: 

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate 
round trip times in milli-seconds: 

Minimum = 0ms, Maximum = 0ms, Average = 0ms 

從上面就可以知道IP為192.168.1.21的電腦NetBios名為iceblood.yofor.com。 

-n count Number of echo requests to send. 

發送count指定的Echo數據包數。 

在默認情況下,一般都只發送四個數據包,通過這個命令可以自己定義發送的個數,對衡量網路速度很有幫助,比如我想測試發送50個數據包的返回的平均時間為多少,最快時間為多少,最慢時間為多少就可以通過以下?知: 


C:\>ping -n 50 202.103.96.68 

Pinging 202.103.96.68 with 32 bytes of data: 

Reply from 202.103.96.68: bytes=32 time=50ms TTL=241 

Reply from 202.103.96.68: bytes=32 time=50ms TTL=241 

Reply from 202.103.96.68: bytes=32 time=50ms TTL=241 

Request timed out. 

……………… 

Reply from 202.103.96.68: bytes=32 time=50ms TTL=241 

Reply from 202.103.96.68: bytes=32 time=50ms TTL=241 

Ping statistics for 202.103.96.68: 

Packets: Sent = 50, Received = 48, Lost = 2 (4% loss),Approximate 
round trip times in milli-seconds: 

Minimum = 40ms, Maximum = 51ms, Average = 46ms 

從以上我就可以知道在給202.103.96.68發送50個數據包的過程當中,返回了48個,其中有兩個由於未知原因丟失,這48個數據包當中返回速度最快為40ms,最慢為51ms,平均速度為46ms。 


-l size Send buffer size. 

定義echo數據包大小。 

在默認的情?下windows的ping發送的數據包大小為32byt,我們也可以自己定義它的大小,但有一個大小的限制,就是最大只能發送 65500byt,也許有人會問為什麼要限制到65500byt,因為Windows系列的系統都有一個安全漏洞(也許還包括其他系統)就是當向對方一次 發送的數據包大于或等於65532時,對方就很有可能當机,所以微軟公司為了解決這一安全漏洞於是限制了ping的數據包大小。雖然微軟公司已經做了此限 制,但這個參數配合其他參數以後危害依然非常強大,比如我們就可以通過配合-t參數來實現一個帶有攻擊性的指令:(以下介紹帶有危險性,只用於試驗,請勿 輕易施於別人電腦上,否?後果自負) 


C:\>ping -l 65500 -t 192.168.1.21 

Pinging 192.168.1.21 with 65500 bytes of data: 

Reply from 192.168.1.21: bytes=65500 time<10ms TTL=254 

Reply from 192.168.1.21: bytes=65500 time<10ms TTL=254 

……………… 

這樣它就會不停的向192.168.1.21電腦發送大小為65500byt的數據包,如果你只有一台電腦也許?有什麼效果,但如果有很多台電腦那麼就可 以使對方完全癱瘓,我曾經就做過這樣的試驗,當我同時使用10台以上電腦ping一台Win2000Pro系統的電腦時,不到5分鐘對方的網路就已經完全 癱瘓,網路嚴重堵塞,HTTP和FTP服務完全停止,由此可見威力非同小可。 


-f Set Don't Fragment flag in packet. 

在數據包中發送“不要分段”標誌。 

在一般你所發送的數據包都會通過路由分段再發送給對方,加上此參數以後路由就不會再分段處理。 

-i TTL Time To Live. 

指定TTL值在對方的系統里停留的時間。 

此參數同樣是幫助你檢查網路運轉情況的。 

-v TOS Type Of Service. 

將“服務類型”字段設置為tos指定的值。 

-r count Record route for count hops. 

在“記錄路由”字段中記錄伝出和返回數據包的路由。 

在一般情況下你發送的數據包是通過一個個路由才到達對方的,但到底是經過了哪些路由呢?通過此參數就可以設定你想探測經過的路由的個數,不過限制在了9 個,也就是說你只能跟蹤到9個路由,如果想探測更多,可以通過其他命令實現,我將在以後的文章中給大家講解。以下為範例: 


C:\>ping -n 1 -r 9 202.96.105.101(發送一個數據包,最多記錄9個路由) 

Pinging 202.96.105.101 with 32 bytes of data: 

Reply from 202.96.105.101: bytes=32 time=10ms TTL=249 

Route: 202.107.208.187 -> 

202.107.210.214 -> 

61.153.112.70 -> 

61.153.112.89 -> 

202.96.105.149 -> 

202.96.105.97 -> 

202.96.105.101 -> 

202.96.105.150 -> 

61.153.112.90 

Ping statistics for 202.96.105.101: 

Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), 

Approximate round trip times in milli-seconds: 

Minimum = 10ms, Maximum = 10ms, Average = 10ms 

從上面我就可以知道從我的電腦到202.96.105.101一共通過了202.107.208.187,202.107.210.214 , 
61.153.112.70 , 61.153.112.89 , 202.96.105.149 , 202.96.105.97這幾個路由。 


-s count Timestamp for count hops. 

指定count指定的?點數的時間戳。 

此參數和-r差不多,只是這個參數不記錄數據包返回所經過的路由,最多也只記錄4個。 

-j host-list Loose source route along host-list. 

利用computer-list指定的電腦列表路由數據包。連續電腦可以被中間關網?分隔(路由稀疏源)IP允許的最大?量為9。 

-k host-list Strict source route along host-list. 

利用computer-list指定的電腦列表路由數據包。連續電腦不能被中間網?分隔(路由?格源)IP允許的最大數量為9。 

-w timeout Timeout in milliseconds to wait for each reply. 

指定超時間隔,單位為毫秒。 

此參數?有什麼其他技巧。 

ping指令的其他技巧:在一般情況下還可以通過ping對方讓對方返回給你的TTL值大小,粗略的判斷目標主机的系統類型是Windows系列還是 UNIX/Linux系列,一般情況下Windows系列的系?返回的TTL值在100-130之間,而UNIX/Linux系列的系統返回的TTL值在 240-255之間,當然TTL的值在對方的主机裡是可以修改的,Windows系列的系?可以通過修改注?表以下鍵值實現: 


[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] 


"DefaultTTL"=dword:000000ff 

255---FF 

128---80 

64----40 

32----20 

好了,ping命令也基本上完全講解完了,其中還有-j,-k參數我還沒有詳細說明,由於某些原因也包括我自己所收集的資料過少這里也?有向大家詳細介 紹,請大家見諒,如果在看了這篇文章的朋友當中有知道得比我更多的,以及其他使用技巧的也希望您能告知我,並在此先謝過。

CONTINUE
INFO: 2008-07-18 01:37:24 | purpen | digg | link

Memcache 协议(中英对照)

协议
Protocol
memcached 的客户端使用TCP链接 与 服务器通讯。(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的memcached服务器监视一些(可设置)端口。客户端连接这些端口,发送命令到服务器,读取回应,最后关闭连接。 Clients of memcached communicate with server through TCP connections. (A UDP interface is also available; details are below under "UDP protocol.") A given running memcached server listens on some (configurable) port; clients connect to that port, send commands to the server, read responses, and eventually close the connection.
结 束会话不需要发送任何命令。当不再需memcached服务时,要客户端可以在任何时候关闭连接。需要注意的是,鼓励客户端缓存这些连接,而不是每次需要 存取数据时都重新打开连接。这是因为memcached 被特意设计成及时开启很多连接也能够高效的工作(数百个,上千个如果需要的话)。缓存这些连接,可以消除建立连接所带来的开销(/*/相对而言,在服务器 端建立一个新连接的准备工作所带来的开销,可以忽略不计。)。 There is no need to send any command to end the session. A client may just close the connection at any moment it no longer needs it. Note, however, that clients are encouraged to cache their connections rather than reopen them every time they need to store or retrieve data. This is because memcached is especially designed to work very efficiently with a very large number (many hundreds, more than a thousand if necessary) of open connections. Caching connections will eliminate the overhead associated with establishing a TCP connection (the overhead of preparing for a new connection on the server side is insignificant compared to this).
在memcache 协议中发送的数据分两种:文本行 和 自由数据。 文本行被用于来自客户端的命令和服务器的回应。自由数据用于客户端从服务器端存取数据时。同样服务器会以字节流的方式传回自由数据。/*/服务器不用关心 自由数据的字节顺序。自由数据的特征没有任何限制;但是通过前文提到的文本行,这项数据的接受者(服务器或客户端),便能够精确地获知所发送的数据库的长 度。
There are two kinds of data sent in the memcache protocol: text lines
and unstructured data. Text lines are used for commands from clients
and responses from servers. Unstructured data is sent when a client
wants to store or retrieve data. The server will transmit back
unstructured data in exactly the same way it received it, as a byte
stream. The server doesn't care about byte order issues in
unstructured data and isn't aware of them. There are no limitations on
characters that may appear in unstructured data; however, the reader
of such data (either a client or a server) will always know, from a
preceding text line, the exact length of the data block being
transmitted.
文 本行固定以“\r\n”(回车符紧跟一个换行符)结束。 自由数据也是同样会以“\r\n”结束,但是 \r(回车符)、\n(换行符),以及任何其他8位字符,均可出现在数据中。因此,当客户端从服务器取回数据时,必须使用数据区块的长度来确定数据区块的 结束位置,而不要依据数据区块末尾的“\r\n”,即使它们固定存在于此。
Text lines are always terminated by \r\n. Unstructured data is _also_
terminated by \r\n, even though \r, \n or any other 8-bit characters
may also appear inside the data. Therefore, when a client retrieves
data from a server, it must use the length of the data block (which it
will be provided with) to determine where the data block ends, and not
the fact that \r\n follows the end of the data block, even though it
does.
   
键值
Keys
存储在memcached中的数据通过键值来标识。键值是一个文本字符串,对于需要存取这项数据的客户端而言,它必须是唯一的。键值当前的长度限制设定为250字符(当然,客户端通常不会用到这么长的键);键值中不能使用制表符和其他空白字符(例如空格,换行等)。
Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn't need to use such long keys); the key must not include
control characters or whitespace.
   
命令
Commands
所有命令分为3种类型 There are three types of commands.
存储命令(有3项:’set’、’add’、’repalce’)指示服务器储存一些由键值标识的数据。客户端发送一行命令,后面跟着数据区块;然后,客户端等待接收服务器回传的命令行,指示成功与否。
Storage commands (there are three: "set", "add" and "replace") ask the
server to store some data identified by a key. The client sends a
command line, and then a data block; after that the client expects one
line of response, which will indicate success or faulure.
取 回命令(只有一项:’get’)指示服务器返回与所给键值相符合的数据(一个请求中右一个或多个键值)。客户端发送一行命令,包括所有请求的键值;服务器 每找到一项内容,都会发送回客户端一行关于这项内容的信息,紧跟着是对应的数据区块;直到服务器以一行“END”回应命令结束。
Retrieval commands (there is only one: "get") ask the server to
retrieve data corresponding to a set of keys (one or more keys in one
request). The client sends a command line, which includes all the
requested keys; after that for each item the server finds it sends to
the client one response line with information about the item, and one
data block with the item's data; this continues until the server
finished with the "END" response line.
/*?*/其他的命令都不能携带自由数据。在这些命令中,客户端发送一行命令,然后等待(由命令所决定)一行回应,或最终以一行“END”结束的多行命令。
All other commands don't involve unstructured data. In all of them,
the client sends one command line, and expects (depending on the
command) either one line of response, or several lines of response
ending with "END" on the last line.
一行命令固定以命令名称开始,接着是以空格隔开的参数(如果有参数的话)。命令名称大小写敏感,并且必须小写。
A command line always starts with the name of the command, followed by
parameters (if any) delimited by whitespace. Command names are
lower-case and are case-sensitive.
   
一 些客户端发送给服务器的命令会包含一些时限(针对内容或客户端请求的操作)。这时,时限的具体内容既可以是Unix时间戳(从1970年1月1日开始的秒 钟数),或当前时间开始的秒钟数。对后者而言,不能超过 60*60*24*30(30天);如果超出,服务器将会理解为Unix时间戳,而不是从当前时间起的秒偏移。
Some commands involve a client sending some kind of expiration time
(relative to an item or to an operation requested by the client) to
the server. In all such cases, the actual value sent may either be
Unix time (number of seconds since January 1, 1970, as a 32-bit
value), or a number of seconds starting from current time. In the
latter case, this number of seconds may not exceed 60*60*24*30 (number
of seconds in 30 days); if the number sent by a client is larger than
that, the server will consider it to be real Unix time value rather
than an offset from current time.
   
错误字串
Error strings
每一个由客户端发送的命令,都可能收到来自服务器的错误字串回复。这些错误字串会以三种形式出现: Each command sent by a client may be answered with an error string
from the server. These error strings come in three types:
- "ERROR\r\n"
意味着客户端发送了不存在的命令名称。 means the client sent a nonexistent command name.
- "CLIENT_ERROR <error>\r\n"
意味着输入的命令行里存在一些客户端错误,例如输入未遵循协议。<error>部分是人类易于理解的错误解说…… means some sort of client error in the input line, i.e. the input
doesn't conform to the protocol in some way. <error> is a
human-readable error string.
- "SERVER_ERROR <error>\r\n"
意味着一些服务器错误,导致命令无法执行。<error>部分是人类易于理解的错误解说。在一些严重的情形下(通常应该不会遇到),服务器将在发送这行错误后关闭连接。这是服务器主动关闭连接的唯一情况。
means some sort of server error prevents the server from carrying
out the command. <error> is a human-readable error string. In cases
of severe server errors, which make it impossible to continue
serving the client (this shouldn't normally happen), the server will
close the connection after sending the error line. This is the only
case in which the server closes a connection to a client.
在后面每项命令的描述中,这些错误行不会再特别提到,但是客户端必须考虑到这些它们存在的可能性。 In the descriptions of individual commands below, these error lines
are not again specifically mentioned, but clients must allow for their
possibility.
   
存储命令
Storage commands
首先,客户端会发送一行像这样的命令: First, the client sends a command line which looks like this:
<command name> <key> <flags> <exptime> <bytes>\r\n
- <command name> 是 set, add, 或者 repalce - <command name> is "set", "add" or "replace"
  • set 意思是 “储存此数据”
  • add 意思是 “储存此数据,只在服务器*未*保留此键值的数据时”
  • replace意思是 “储存此数据,只在服务器*曾*保留此键值的数据时”
  • "set" means "store this data".
  • "add" means "store this data, but only if the server *doesn't* already
    hold data for this key".
  • "replace" means "store this data, but only if the server *does*
    already hold data for this key".
- <key> 是接下来的客户端所要求储存的数据的键值 - <key> is the key under which the client asks to store the data
- <flags> 是在取回内容时,与数据和发送块一同保存服务器上的任意16位无符号整形(用十进制来书写)。客户端可以用它作为“位域”来存储一些特定的信息;它对服务器是不透明的。
- <flags> is an arbitrary 16-bit unsigned integer (written out in
decimal) that the server stores along with the data and sends back
when the item is retrieved. Clients may use this as a bit field to
store data-specific information; this field is opaque to the server.
- <exptime> 是终止时间。如果为0,该项永不过期(虽然它可能被删除,以便为其他缓存项目腾出位置)。如果非0(Unix时间戳或当前时刻的秒偏移),到达终止时间后,客户端无法再获得这项内容。
- <exptime> is expiration time. If it's 0, the item never expires
(although it may be deleted from the cache to make place for other
items). If it's non-zero (either Unix time or offset in seconds from
current time), it is guaranteed that clients will not be able to
retrieve this item after the expiration time arrives (measured by
server time).
- <bytes> 是随后的数据区块的字节长度,不包括用于分野的“\r\n”。它可以是0(这时后面跟随一个空的数据区块)。 - <bytes> is the number of bytes in the data block to follow, *not*
including the delimiting \r\n. <bytes> may be zero (in which case
it's followed by an empty data block).
   
在这一行以后,客户端发送数据区块。 After this line, the client sends the data block:
<data block>\r\n
- <data block> 是大段的8位数据,其长度由前面的命令行中的<bytes>指定。 - <data block> is a chunk of arbitrary 8-bit data of length <bytes>
from the previous line.
发送命令行和数据区块以后,客户端等待回复,可能的回复如下: After sending the command line and the data blockm the client awaits
the reply, which may be:
- "STORED\r\n"
表明成功. to indicate success.
- "NOT_STORED\r\n"
表明数据没有被存储,但不是因为发生错误。这通常意味着add 或 replace命令的条件不成立,或者,项目已经位列删除队列(参考后文的“delete”命令)。
to indicate the data was not stored, but not
because of an error. This normally means that either that the
condition for an "add" or a "replace" command wasn't met, or that the
item is in a delete queue (see the "delete" command below).
   
取回命令
Retrieval command
一行取回命令如下: The retrieval command looks like this:
get <key>*\r\n
- <key>* 表示一个或多个键值,由空格隔开的字串 - <key>* means one or more key strings separated by whitespace.
这行命令以后,客户端的等待0个或多个项目,每项都会收到一行文本,然后跟着数据区块。所有项目传送完毕后,服务器发送以下字串:
After this command, the client expects zero or more items, each of
which is received as a text line followed by a data block. After all
the items have been transmitted, the server sends the string
"END\r\n"
来指示回应完毕。 to indicate the end of response.
服务器用以下形式发送每项内容: Each item sent by the server looks like this:
VALUE <key> <flags> <bytes>\r\n
<data block>\r\n
- <key> 是所发送的键名 - <key> is the key for the item being sent
- <flags> 是存储命令所设置的记号

- <flags> is the flags value set by the storage command

- <bytes> 是随后数据块的长度,*不包括* 它的界定符“\r\n” - <bytes> is the length of the data block to follow, *not* including
its delimiting \r\n
- <data block> 是发送的数据 - <data block> is the data for this item.
如果在取回请求中发送了一些键名,而服务器没有送回项目列表,这意味着服务器没这些键名(可能因为它们从未被存储,或者为给其他内容腾出空间而被删除,或者到期,或者被已客户端删除)。
If some of the keys appearing in a retrieval request are not sent back
by the server in the item list this means that the server does not
hold items with such keys (because they were never stored, or stored
but deleted to make space for more items, or expired, or explicitly
deleted by a client).
   
删除
Deletion
命令“delete”允许从外部删除内容: The command "delete" allows for explicit deletion of items:
delete <key> <time>\r\n
- <key> 是客户端希望服务器删除的内容的键名 - <key> is the key of the item the client wishes the server to delete
- <time> 是一个单位为秒的时间(或代表直到某一刻的Unix时间),在该时间内服务器会拒绝对于此键名的“add”和“replace”命令。此时内容被放入 delete队列,无法再通过“get”得到该内容,也无法是用“add”和“replace”命令(但是“set”命令可用)。直到指定时间,这些内容 被最终从服务器的内存中彻底清除。
- <time> is the amount of time in seconds (or Unix time until which)
the client wishes the server to refuse "add" and "replace" commands
with this key. For this amount of item, the item is put into a
delete queue, which means that it won't possible to retrieve it by
the "get" command, but "add" and "replace" command with this key
will also fail (the "set" command will succeed, however). After the
time passes, the item is finally deleted from server memory.
<time>参数 是可选的,缺省为0(表示内容会立刻清除,并且随后的存储命令均可用)。
The parameter <time> is optional, and, if absent, defaults to 0
(which means that the item will be deleted immediately and further
storage commands with this key will succeed).
此命令有一行回应: The response line to this command can be one of:
- "DELETED\r\n"
表示执行成功 to indicate success
- "NOT_FOUND\r\n"
表示没有找到这项内容 to indicate that the item with this key was not found.
参考随后的“flush_all”命令使所有内容无效
See the "flush_all" command below for immediate invalidation
of all existing items.
   
增加/减少
Increment/Decrement
命 令 “incr” 和 “decr”被用来修改数据,当一些内容需要 替换、增加 或减少时。这些数据必须是十进制的32位无符号整新。如果不是,则当作0来处理。修改的内容必须存在,当使用“incr”/“decr”命令修改不存在的 内容时,不会被当作0处理,而是操作失败。
Commands "incr" and "decr" are used to change data for some item
in-place, incrementing or decrementing it. The data for the item is
treated as decimal representation of a 32-bit unsigned integer. If the
current data value does not conform to such a representation, the
commands behave as if the value were 0. Also, the item must already
exist for incr/decr to work; these commands won't pretend that a
non-existent key exists with value 0; instead, they will fail.
客户端发送命令行: The client sends the command line:
incr <key> <value>\r\n

decr <key> <value>\r\n
- <key> 是客户端希望修改的内容的建名

- <key> is the key of the item the client wishes to change

- <value> 是客户端要增加/减少的总数。 - <value> is the amount by which the client wants to increase/decrease
the item. It is a decimal representation of a 32-bit unsigned integer.
回复为以下集中情形:

The response will be one of:

- "NOT_FOUND\r\n"
指示该项内容的值,不存在。 to indicate the item with this value was not found
- <value>\r\n ,<value>是 增加/减少 。 - <value>\r\n , where <value> is the new value of the item's data,
after the increment/decrement operation was carried out.
注意"decr"命令发生下溢:如果客户端尝试减少的结果小于0时,结果会是0。"incr" 命令不会发生溢出。 Note that underflow in the "decr" command is caught: if a client tries
to decrease the value below 0, the new value will be 0. Overflow in
the "incr" command is not checked.

……

Note also that decrementing a number such that it loses length isn't
guaranteed to decrement its returned length. The number MAY be
space-padded at the end, but this is purely an implementation
optimization, so you also shouldn't rely on that.
   
状态
Statistics
命令"stats" 被用于查询服务器的运行状态和其他内部数据。有两种格式。不带参数的:
The command "stats" is used to query the server about statistics it
maintains and other internal data. It has two forms. Without
arguments:
 stats\r\n
这会在随后输出各项状态、设定值和文档。另一种格式带有一些参数:

it causes the server to output general-purpose statistics and
settings, documented below. In the other form it has some arguments:


stats <args>\r\n
通过<args>,服务器传回各种内部数据。因为随时可能发生变动,本文不提供参数的种类及其传回数据。 Depending on <args>, various internal data is sent by the server. The
kinds of arguments and the data sent are not documented in this vesion
of the protocol, and are subject to change for the convenience of
memcache developers.
   
各种状态
General-purpose statistics
受到无参数的"stats"命令后,服务器发送多行内容,如下: Upon receiving the "stats" command without arguments, the server sents
a number of lines which look like this:
STAT <name> <value>\r\n
服务器用以下一行来终止这个清单: The server terminates this list with the line
END\r\n
在每行状态中,<name> 是状态的名字,<value> 使状态的数据。 以下清单,是所有的状态名称,数据类型,和数据代表的含义。
In each line of statistics, <name> is the name of this statistic, and
<value> is the data. The following is the list of all names sent in
response to the "stats" command, together with the type of the value
sent for this name, and the meaning of the value.
在“类型”一列中,"32u"表示32位无符号整型,"64u"表示64位无符号整型,"32u:32u"表示用冒号隔开的两个32位无符号整型。

In the type column below, "32u" means a 32-bit unsigned integer, "64u"
means a 64-bit unsigner integer. '32u:32u' means two 32-but unsigned
integers separated by a colon.

名称/Name 类型/Type 含义/Meaning
pid 32u 服务器进程ID Process id of this server process
uptime 32u 服务器运行时间,单位秒 Number of seconds this server has been running
time 32u 服务器当前的UNIX时间 current UNIX time according to the server
version string 服务器的版本号 Version string of this server
rusage_user 32u:32u 该进程累计的用户时间
(秒:微妙)
Accumulated user time for this process
(seconds:microseconds)
rusage_system 32u:32u 该进程累计的系统时间
(秒:微妙)
Accumulated system time for this process
(seconds:microseconds)
curr_items 32u 服务器当前存储的内容数量 Current number of items stored by the server
total_items 32u 服务器启动以来存储过的内容总数 Total number of items stored by this server
ever since it started
bytes 64u 服务器当前存储内容所占用的字节数 Current number of bytes used by this server
to store items
curr_connections 32u 连接数量 Number of open connections
total_connections 32u 服务器运行以来接受的连接总数 Total number of connections opened since
the server started running
connection_structures 32u 服务器分配的连接结构的数量 Number of connection structures allocated
by the server
cmd_get 32u 取回请求总数 Cumulative number of retrieval requests
cmd_set 32u 存储请求总数 Cumulative number of storage requests
get_hits 32u 请求成功的总次数 Number of keys that have been requested and
found present
get_misses 32u 请求失败的总次数 Number of items that have been requested
and not found
bytes_read 64u 服务器从网络读取到的总字节数 Total number of bytes read by this server
from network
bytes_written 64u 服务器向网络发送的总字节数 Total number of bytes sent by this server to
network
limit_maxbytes 32u 服务器在存储时被允许使用的字节总数 Number of bytes this server is allowed to
use for storage.
   
其它命令
Other commands

“flush_all” 命令有一个可选的数字参数。它总是执行成功,服务器会发送“OK\r\n”回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执 行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行。flush_all 效果具体如下:它导致所有更新时间早于flush_all所设定时间的项目,在被执行取回命令时命令被忽略。

"flush_all" is a command with an optional numeric argument. It always succeeds, and the server sends "OK\r\n" in response. Its effect is to invalidate all existing items immediately (by default) or after the expiration specified. After invalidation none of the items will be returned in response to a retrieval command (unless it's stored again under the same key *after* flush_all has invalidated the items). flush_all doesn't actually free all the memory taken up by existing items; that will happen gradually as new items are stored. The most precise definition of what flush_all does is the following: it causes all items whose update time is earlier than the time at which flush_all was set to be executed to be ignored for retrieval purposes.
“version”命令没有参数: "version" is a command with no arguments:
version\r\n
在回应中,服务器发送: In response, the server sends
"VERSION <version>\r\n"
<version> 是服务器的版本字串。 where <version> is the version string for the server.
“quit”命令没有参数: "quit" is a command with no arguments:
quit\r\n
接收此命令后,服务器关闭连接。不过,客户端可以在不再需要时,简单地关闭连接就行,并不一定需要发送这个命令。 Upon receiving this command, the server closes the connection. However, the client may also simply close the connection when it no longer needs it, without issuing this command.
   
UDP 协议
UDP protocol
当来自客户端的连接数远大于TCP连接的上限时,可以使用基于UDP的接口。UDP接口不能保证传输到位,所以只有在不要求成功的操作中使用;比如被用于一个“get”请求时,会因不当的缓存处理而发生错误或回应有遗失。 For very large installations where the number of clients is high enough that the number of TCP connections causes scaling difficulties, there is also a UDP-based interface. The UDP interface does not provide guaranteed delivery, so should only be used for operations that aren't required to succeed; typically it is used for "get" requests where a missing or incomplete response can simply be treated as a cache miss.
每个UDP数据包都包含一个简单的帧头,数据之后的内容与TCP协议的描述类似。在执行所产生的数据流中,请求必须被包含在单独的一个UDP数据包中,但是回应可能跨越多个数据包。(只有“get”和“set”请求例外,跨越了多个数据包) Each UDP datagram contains a simple frame header, followed by data in the same format as the TCP protocol described above. In the current implementation, requests must be contained in a single UDP datagram, but responses may span several datagrams. (The only common requests that would span multiple datagrams are huge multi-key "get" requests and "set" requests, both of which are more suitable to TCP transport for reliability reasons anyway.)
帧头有8字节长,如下(均由16位整数组成,网络字节顺序,高位在前): The frame header is 8 bytes long, as follows (all values are 16-bit integers in network byte order, high byte first):
  • 0-1 请求ID
  • 2-3 序号
  • 4-5 该信息的数据包总数
  • 6-7 保留位,必须为0
  • 0-1 Request ID
  • 2-3 Sequence number
  • 4-5 Total number of datagrams in this message
  • 6-7 Reserved for future use; must be 0
请求ID有客户端提供。一般它会是一个从随机基数开始的递增值,不过客户端想用什么样的请求ID都可以。服务器的回应会包含一个和请求中的同样的ID。客户端使用请求ID来区分每一个回应。任何一个没有请求ID的数据包,可能是之前的请求遭到延迟而造成的,应该被丢弃。 The request ID is supplied by the client. Typically it will be a monotonically increasing value starting from a random seed, but the client is free to use whatever request IDs it likes. The server's response will contain the same ID as the incoming request. The client uses the request ID to differentiate between responses to outstanding requests if there are several pending from the same server; any datagrams with an unknown request ID are probably delayed responses to an earlier request and should be discarded.
序号的返回是从0到n-1,n是该条信息的数据包数量。 The sequence number ranges from 0 to n-1, where n is the total number of datagrams in the message. The client should concatenate the payloads of the datagrams for a given response in sequence number order; the resulting byte stream will contain a complete response in the same format as the TCP protocol (including terminating \r\n sequences).
CONTINUE
INFO: 2008-07-15 14:50:19 | purpen | digg | link

ImageMagick FAQ 摘要

  1. 如何让animate在显示图片的过程保持窗口的标题不变
    animate -title "My Image Sequence" images.
  2. import抓得的图片与X服务器上显示的不一致怎么办?
    import -descend image.miff
  3. 如何动画显示YUV 图片序列?
    animate -geometry 352x240 -scene 0-71 yuv3:frame%d
  4. 如果只看一个RGB图片的红色通道(red channel)?
    display -gamma 1.0,0.0,0.0 image.miff
  5. 如何改变PostScript默认纸张大小?
    要改变默认的612x792大小,修改magick/image.h中的PSPageGeometry的值并重编译.
  6. 如何自左至右无边无框无文字半三幅画连接在一起?
    convert +append image1.ppm image2.ppm image3.ppm side_by_side.miff
    # 或
    convert -size 350x500 xc:black composite.miff
    composite -geometry +0+0 composite.miff image1.gif composite.miff
    composite -geometry +100+0 composite.miff image2.gif composite.miff
    composite -geometry +0+300 composite.miff image3.gif composite.miff
    composite -geometry +0+375 composite.miff image4.gif composite.miff
  7. 如何生成gif动画?
    convert -delay 20 frame*.gif animation.gif
    convert -delay 20 frame1.gif -delay 10 frame2.gif -delay 5 frame3.gif animation.gif
    convert frame1.gif -page +50+100 frame2.gif -page +0+100 frame3.gif animation.gif
    convert -loop 50 frame*.gif animation.gif
    convert +adjoin images.* frames%d.gif
  8. 如果完整的显示PostScript图片?
    display -page letter image.ps
  9. 什么是图片目录?如何使用它们?
    #图片目录(visual image directory ,VID)的创建:
    montage *.jpg directory.vid
    convert 'vid:*.jpg' directory.vid
    #显示图片目录
    display directory.vid
    display vid:movie.mpg
  10. 如果带框架保存一个窗口?
    xwininfo -frame
    import -frame -window ID window.miff
  11. 图片以单一颜色(a solid color)显示,是怎么回事? 图片被破坏,或含有值为0的matte层,试着:
     display +matte image.miff
  12. 出现 "???? library is not available ..."是怎么回事? 没安装相关的函数库。参见安装说明。
  13. 如何查看图片不光滑层(matte channel)的值?
    convert image.tiff image.matte
    display -size 640x480 gray:image.matte
  14. 运行"Convert in.gif out.png" 时显示 "Invalid Parameter - out. 你运行的convert是windows的分区类型转换程序。
  15. 如何在图片增加斜角?
    #对图片边缘的处理共分四类。
    1)增加有色边
    convert -bordercolor red -border 25x25 image.jpg image.gif
    2)加亮或变暗图片边缘,以增强3D效果
    convert -raise 25 image.jpg image.gif
    3)在图片周围增加装饰性框架。
    convert -mattecolor gray -frame 25x25 image.jpg image.gif
    4)在图片边缘增加升、降斜角
    convert -mattecolor gray -frame 25x25+0+25 image.jpg image.gif
    convert -mattecolor gray -frame 25x25+25+0 image.jpg image.gif
  16. 用窗口管理器运行display失败了,怎么办? 用下列命令之一来确认display运行在一个交互环境中:
    display logo:Untitled
    display < /dev/console
  17. 将PostScript文件转换为其它格式时,如何改善文字的外观?
    convert -density 288 -geometry 25% image.ps image.gif
  18. 如何用2-3英寸高的文字注释一个图片? 先检查有无可缩放字体: xlsfonts -fn '*-0-0-0-0-*' 有则:
    convert -font '-*-helvetica-*-*-*--300-300-*-*-*-*-iso8859-1' \
    -fill green -draw 'text 50,300 Magick' image.gif annotated.gif
    # If you have the FreeType support built into ImageMagick,
    # just increase your pointsize and/or density:
    convert -font Helvetica -pointsize 100 -density 300 ...
  19. 如何将gif文件分析为一系列的单个图片?
    convert animation.gif frame%02d.gif
  20. 用NetScape显示的图片,如何去掉周围的背景色? convert +page alpha.gif beta.gif
  21. 如何用web安全的颜色(Web safe colors)创建GIF或PNG图片?
    convert -map netscape: alpha.gif beta.gif
  22. 为何Adobe Acrobat 2.1不能读ImageMagick's制作的PDF? 用 Acrobat 3.0或以上版本,来阅读这种经zip压缩的pdf,或制作PDF时不进行压缩。
    convert +compress images.tiff image.pdf
  23. 如何在图片上增加一个不光滑层(matte channel)?
    convert image.gif -matte temp.miff
    composite -compose CopyOpacity mask.xbm temp.miff transparent.gif
  24. 在NT环境下如何用convert在图片上写字?
    convert -font Arial -fill blue -draw "text 10,10 'your text here'" d:\test.tif png:d:\test.png
    convert.exe -pointsize 18 -draw "text 0,0 "This is my text!"" C:\blank.gif c:\text.gif
  25. 为何GIF文件比期望的大? 需要在编译源码时加 --enable-lzw 参数,来打开LZW压缩功能。
  26. 为何JPEG文件比期望的大? 可能嵌入Exif或IPTC等"profiles", 或者含有没压缩的thumbnails. 可以用命令行参数 +profile ""或相关的API来将它们去掉。
  27. 如何从图片文件中抽取第一张图片?
    convert "Image.gif[0]" first.gif
  28. 如何创建一张空白图片?
    convert -size 800x600 xc:"#ddddff" ltblue.ppm
    convert -size 800x600 null:white white.ppm
    convert in.png -threshold 100% black.ppm #<--与in.png同大小
  29. 为何GIF文件比期望的大? 需要在编译源码时加 --enable-lzw 参数,来打开LZW压缩功能。
  30. 为何JPEG文件比期望的大? 可能嵌入Exif或IPTC等"profiles", 或者含有没压缩的thumbnails. 可以用命令行参数 +profile ""或相关的API来将它们去掉。
CONTINUE
INFO: 2008-07-14 14:22:34 | purpen | digg | link

Image MagicK 速查

(原文:http://dsec.pku.edu.cn/~yuhj/wiki/ImageMagick.html)

Image MagicK 是一个强大的图象处理工具包。它提供了几个非常方 便的命令行命令: display, animate,import, montage,mogrify,identify等,可以进行图象的显示,缩放,旋转, 调色,加框,加注释等,还能制作GIF动画,图象索引,能自动生成 图象.

  1. 制作索引图和动画
    !/bin/bash
    montage -bordercolor red -borderwidth 3 -label "%f" -tile 5x3 *.JPG montage.jpg
    mogrify -format gif *.JPG
    display montage.jpg
    animate *.JPG
  2. 缩放 convert -sample 80x40 input.jpg output.jpg #注意:缩放后图像保持原来的长宽比例 convert -sample 25%x25% input.jpg output.jpg
  3. 为当前目录的所有图像生成缩略图
    for img in `ls *.jpg`
    do
    convert -sample 25%x25% $img thumb-$img
    done
  4. 获取文件信息 libtiff
    tiffinfo filename.tiff
    pnginfo filename.png
  5. 可以使用 ImageMagick 的 identify
     identify -verbose sample.png 
    identify -format "%wx%h" sample.png
  6. 旋转图像
     convert -rotate 90 input.jpg output.jpg
  7. 更改文件类型
     convert input.jpg output.png
  8. 为图像增加注释文字
    convert -font helvetica -fill white -pointsize 36 \
    -draw 'text 10,50 "Floriade 2002, Canberra, Australia"' \
    floriade.jpg comment.jpg
    convert -font fonts/1900805.ttf -fill white -pointsize 36 \
    -draw 'text 10,475 "stillhq.com"' \
    floriade.jpg stillhq.jpg
  9. 特殊效果
    convert -charcoal 2 input.jpg output.jpg #炭笔
    convert -colorize 255 input.jpg output.jpg #着色 可以指定三种颜色 red/green/blue
    convert -implode 4 input.jpg output.jpg #内爆效果
    convert -solarize 42 input.jpg output.jpg #曝光,模拟胶片曝光
    convert -spread 5 input.jpg output.jpg #随机移动,参数是位移大小
  10. 一次执行多个操作
     convert -sample 25%x25% -spread 4  -charcoal 4 input.jpg output.jpg
CONTINUE
INFO: 2008-07-14 14:20:59 | purpen | digg | link

Perl 内嵌的警告(warnings)

当程序中包含可能的错误时,可以要求 Perl 警告你。

运行程序时,可以在命令行中使用 –w 这个参数把警告打开:


$ perl –w my_program


或者,如果一直都需要警告(warning),可以在 # ! 这一行加上-w,如:


#! /usr/bin/perl –w


这条命令甚至在 non-Unix 系统中也有效。

CONTINUE
INFO: 2008-07-11 02:57:15 | purpen | digg | link

Memcahe 原理简介

一、memcached 简介

在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东。这里简单介绍一下,memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速 WEB 动态应用程序。

二、memcached 安装

首先是下载 memcached 了,目前最新版本是 1.2.2,直接从官方网站 (http://www.danga.com/memcached/) 即可下载到 memcached-1.2.2.tar.gz。除此之外,memcached 用到了 libevent,我下载的是 libevent-1.3e (目前最新的版本)。

接下来是分别将 libevent-1.3e.tar.gz 和 memcached-1.2.2.tar.gz 解开包、编译、安装:

# tar -zxvf libevent-1.3e.tar.gz
# cd libevent-1.3e
# ./configure --prefix=/usr
# make
# make install clean
# cd ..
# tar -zxvf memcached-1.2.2.tar.gz
# cd memcached-1.2.2
# ./configure --prefix=/usr
# make
# make install clean

安 装完成之后,memcached 应该在 /usr/bin/memcached。石头提醒一下大家,这里安装的是 /usr 目录下,所以不会出现问题,但是若 libevent 不是安装在默认位置必须在 /usr/lib 下建立一个软连接,否则 memcached 会无法运行,比如:

# ln -s /usr/local/lib/libevent-1.3e.so.1 /usr/lib
# ln -s /usr/local/lib/libevent.so /usr/lib

三、运行 memcached 守护程序

运行 memcached 守护程序很简单,只需一个命令行即可,不需要修改任何配置文件(也没有配置文件给你修改 ):

/usr/bin/memcached -d -m 128 -l 192.168.10.1 -p 10101 -u httpd
参数解释:

-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户,如果当前为 root 的话,需要使用此参数指定用户。

当然,还有其它参数可以用,man memcached 一下就可以看到了。

四、memcached 的工作原理

首 先 memcached 是以守护程序方式运行于一个或多个服务器中,随时接受客户端的连接操作,客户端可以由各种语言编写,目前已知的客户端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。客户端在与 memcached 服务建立连接之后,接下来的事情就是存取对象了,每个被存取的对象都有一个唯一的标识符 key,存取操作均通过这个 key 进行,保存到 memcached 中的对象实际上是放置内存中的,并不是保存在 cache 文件中的,这也是为什么 memcached 能够如此高效快速的原因。注意,这些对象并不是持久的,服务停止之后,里边的数据就会丢失。

与许多 cache 工具类似,Memcached 的原理并不复杂。它采用了C/S的模式,在 server 端启动服务进程,在启动时可以指定监听的 ip,自己的端口号,所使用的内存大小等几个关键参数。一旦启动,服务就一直处于可用状态。Memcached 的目前版本是通过C实现,采用了单进程,单线程,异步I/O,基于事件 (event_based) 的服务方式.使用 libevent 作为事件通知实现。多个 Server 可以协同工作,但这些 Server 之间是没有任何通讯联系的,每个 Server 只是对自己的数据进行管理。Client 端通过指定 Server 端的 ip 地址(通过域名应该也可以)。需要缓存的对象或数据是以 key->value 对的形式保存在Server端。key 的值通过 hash 进行转换,根据 hash 值把 value 传递到对应的具体的某个 Server 上。当需要获取对象数据时,也根据 key 进行。首先对 key 进行 hash,通过获得的值可以确定它被保存在了哪台 Server 上,然后再向该 Server 发出请求。Client 端只需要知道保存 hash(key) 的值在哪台服务器上就可以了。

其实说到底,memcache 的工作就是在专门的机器的内存里维护一张巨大的 hash 表,来存储经常被读写的一些数组与文件,从而极大的提高网站的运行效率(当然花钱就是必不可免的事了,听说 MySpace 的每台 Cache 服务器都配备了至少 64G 的内存)。但是也要注意 memcache 不是万能的,他的速度还是要比本地的文件读取慢很多,如果不是很大的应用,我建议大家使用一些轻量级的 Cache 库,比如 PHP 的 Cache_Lite 就不错,最后希望大家读完这篇能有所收获 :)

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1845625

CONTINUE
INFO: 2008-07-10 02:56:22 | purpen | digg | link

跳绳减肥要领和跳法

昨天,在中关村买了跟跳绳,晚上,我尝试了一次,发现跳绳运动果然是很耗体能的健身方式,决定坚持不懈,来减掉我的“将军肚”。:)  当然,运动也要讲究方法要领,如果你也想参与跳绳,就look下面的诀窍吧!

跳绳减肥要领:

每日跳5分钟为一节,每天可跳5到6节,每周跳6天,待适应后可逐步加量。长期坚持,一定可以有效地减轻体重。

跳法:

1.双脚齐跳,有弹回动作:每跳过一次绳子,双脚再一齐在地上垫一下;

2.双脚齐跳,无弹回动作,即连续不断的跳过绳子;

3.单脚跳:就是两只脚轮流跳,很像跑步的动作。

跳跃的速度:

慢速:平均每分钟跳60-70次。较快速:平均每分钟跳140-160次。

小贴士:

1.跳绳者应穿质地软、重量轻的高帮鞋,避免脚踝受伤。

2.选择软硬适中的草坪、木质地板和泥土地的场地较好,以免损伤关节,并易引起头昏。

3.跳绳时需放松肌肉和关节,脚尖和脚跟需用力协调,防止扭伤。

4.身体肥胖者宜采用双脚同时起落的跳法,以免关节负重而受伤。

CONTINUE
INFO: 2008-07-08 05:52:03 | purpen | digg | link

Copyright © 2008 Jaever. All rights reserved.

This Site looks and works best when viewed using browsers enabled with JavaScript 1.5 and CSS, such as Firefox 1+ or Safari 3+.