這個網站對於perl的dbi有詳細的介紹,有需要的人可以參考一下。
http://perl.hcchien.org/ch15.html
http://www.felixgers.de/teaching/perl/perl_DBI.html
星期一, 6月 15, 2009
星期二, 4月 21, 2009
星期三, 11月 05, 2008
[Perl] 設定$ENV
http://www.devdaily.com/perl/edu/qanda/plqa00014/
可以抓取系統環境變數,在linux下打env有出現的都抓得到
也可以參考CGI的程式,也有抓取環境變數。
可以抓取系統環境變數,在linux下打env有出現的都抓得到
也可以參考CGI的程式,也有抓取環境變數。
[Perl] function & 的意思
只要function的定義比使用先,就可以不用使用&
例如:
sub open_file(){
xxxxx
xxxxx
}
open_file;
如果把function的定義放在後面,就得加&
例如:
&open_file;
sub open_file(){
xxxxxxxx
xxxxxxxx
}
但如果function的名稱與build-in function名稱一樣的時候,就一定要加&來指定為自己定義的function
例如:
sub chomp{
xxxxxxxxx
}
&chomp;
這樣才能正確地使用subroutine。
結論:有加&比沒加好
例如:
sub open_file(){
xxxxx
xxxxx
}
open_file;
如果把function的定義放在後面,就得加&
例如:
&open_file;
sub open_file(){
xxxxxxxx
xxxxxxxx
}
但如果function的名稱與build-in function名稱一樣的時候,就一定要加&來指定為自己定義的function
例如:
sub chomp{
xxxxxxxxx
}
&chomp;
這樣才能正確地使用subroutine。
結論:有加&比沒加好
星期二, 11月 04, 2008
[Perl] perl使用POSIX的sys_wait_h
http://manpages.ubuntu.com/manpages/feisty/zh_TW/man1/perlfunc.html
關於POSIX的sys_wait_h的用法,原文如下:
waitpid PID,FLAGS
Waits for a particular child process to terminate and returns
the pid of the deceased process, or "-1" if there is no such
child process. On some systems, a value of 0 indicates that
there are processes still running. The status is returned in
$?. If you say
use POSIX ":sys_wait_h";
#...
do {
$kid = waitpid(-1, WNOHANG);
} until $kid > 0;
then you can do a non-blocking wait for all pending zombie pro-
cesses. Non-blocking wait is available on machines supporting
either the waitpid(2) or wait4(2) system calls. However, wait-
ing for a particular pid with FLAGS of 0 is implemented every-
where. (Perl emulates the system call by remembering the sta-
tus values of processes that have exited but have not been har-
vested by the Perl script yet.)
Note that on some systems, a return value of "-1" could mean
that child processes are being automatically reaped. See per-
lipc for details, and for other examples.
意思就是說如果要用waitpid來等待child process terminal or child process的pid
就得這樣用嚕
與fork child process等timeout或是等child process結束一樣的道理
use POSIX qw(sys_wait_h);
my $pid = fork;
unless ($pid) {
exec $command;
die "Could not exec $command";
}
my $timeout;
my $wait_pid = -1;
eval
{
local $SIG{ALRM} = sub { die "time out" };
alarm $timeout;
$wait_pid = waitpid($pid, 0);
alarm 0;
};
if ($@ and $@ =~ /time out/)
{
kill_pid($pid);
}
關於POSIX的sys_wait_h的用法,原文如下:
waitpid PID,FLAGS
Waits for a particular child process to terminate and returns
the pid of the deceased process, or "-1" if there is no such
child process. On some systems, a value of 0 indicates that
there are processes still running. The status is returned in
$?. If you say
use POSIX ":sys_wait_h";
#...
do {
$kid = waitpid(-1, WNOHANG);
} until $kid > 0;
then you can do a non-blocking wait for all pending zombie pro-
cesses. Non-blocking wait is available on machines supporting
either the waitpid(2) or wait4(2) system calls. However, wait-
ing for a particular pid with FLAGS of 0 is implemented every-
where. (Perl emulates the system call by remembering the sta-
tus values of processes that have exited but have not been har-
vested by the Perl script yet.)
Note that on some systems, a return value of "-1" could mean
that child processes are being automatically reaped. See per-
lipc for details, and for other examples.
意思就是說如果要用waitpid來等待child process terminal or child process的pid
就得這樣用嚕
與fork child process等timeout或是等child process結束一樣的道理
use POSIX qw(sys_wait_h);
my $pid = fork;
unless ($pid) {
exec $command;
die "Could not exec $command";
}
my $timeout;
my $wait_pid = -1;
eval
{
local $SIG{ALRM} = sub { die "time out" };
alarm $timeout;
$wait_pid = waitpid($pid, 0);
alarm 0;
};
if ($@ and $@ =~ /time out/)
{
kill_pid($pid);
}
星期三, 7月 09, 2008
用Perl做URL Encode & Decode
Encode
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
例如
testpq1-1-abc.com/80/test/test.htm?redirect.asp%uhttp://abc.com
Encode後就是
testpq1%2D1%2Dabc%2Ecom%2F80%2Ftest%2Ftest%2Ehtm%3Fredirect%2Easp%25uhttp%3A%2F%2Fabc%2Ecom
Decode
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
參考一下吧~~~^^
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
例如
testpq1-1-abc.com/80/test/test.htm?redirect.asp%uhttp://abc.com
Encode後就是
testpq1%2D1%2Dabc%2Ecom%2F80%2Ftest%2Ftest%2Ehtm%3Fredirect%2Easp%25uhttp%3A%2F%2Fabc%2Ecom
Decode
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
參考一下吧~~~^^
訂閱:
文章 (Atom)