perl -MCPAN -e shell
#Open a subshell in the DBD::mysql directory (will download the module if not already downloaded):
look DBD::mysql
#Execute any command as you would installing the module "manually":
perl Makefile.PL --testdb=test --testuser=root --testpassword=supersecret --testhost=localhost
make
make test
make install
Tuesday, September 15, 2009
Install DBD::mysql with CPAN
Monday, August 10, 2009
Perl check LDAP password online
#!C:\Perl\bin\perl.exe -w
use strict;
use warnings;
use IO::File;
use MIME::Base64;
use LWP::UserAgent;
my $file = 'ldap.passwd';
my $input = IO::File->new("< $file")
or die "Could't open $file for reading: $!\n";
while ( defined( my $line = $input->getline() ) ) {
chomp($line);
if ( $line =~ /\@perl/ ) {
# print $line . "\n";
my @user = split( /,/, $line );
# find username and password
my $username = $user[0];
my $password = $user[1];
my $realname = $user[2];
$password =~ s/{md5}//;
my $base64_password = decode_base64($password);
my $md5_password = unpack( "H*", $base64_password );
# get md5 reverse
my $ua = LWP::UserAgent->new;
$ua->agent("JapanApp/0.1");
my $req =
HTTP::Request->new(
GET => 'http://gdataonline.com/qkhash.php?mode=txt&hash='
. $md5_password );
my $res = $ua->request($req);
if ( $res->is_success ) {
my $content = $res->content();
print $username . "\t\t" . $1 . "\t\t" . $realname . "\n"
if $content =~ /(.+)<\/b><\/td><\/tr>/;
}
else {
print $res->status_line, "\n";
}
}
}
Sunday, July 20, 2008
用CPAN安装perl module
- 首先配置CPAN Module,方法是在bash下面运行
$ perl -MCPAN -eshell
配置过程会询问一些问题,其中CPAN的镜像可以选择http://cpan.linuxforum.net/。
- 接下来要更新CPAN Module,而不是直接装LWP。否则会遇到undefined subroutine &Digest::base::new之类的错误。方法是运行
cpan> install Bundle::CPAN
这一步是关键,切记切记。我开始不知道,还在网上搜索这个错误信息,发现有人说是Perl的问题,我就把整个Cygwin更新到1.5.19.4,结果啥用也没有,还把其它东西弄得一塌糊涂。
- 装好新的CPAN Module,先退出,然后再运行1里面的命令进入CPAN的shell,这时就可以装LWP了
cpan> install Bundle::LWP
安装Bundle::LWP意味着不仅安装LWP,而且安装它需要的其它module。
Monday, July 14, 2008
使用Perl和MySQL找出书名中的中文词语
使用Perl找出书名中的中文词语,用于搜索引擎:
- 导入中文词库到MySQL数据库
#!/usr/bin/perl
# $Id$
# Add booknames to database
use DBI;
my $database = 'zhu_tags_filter';
my $hostname = '192.168.1.249';
my $port = '3306';
my $user = 'root';
my $password = '123456';
my $input_file = '/mnt/books/dict.txt';
$dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
$dbh = DBI->connect($dsn, $user, $password);
$sth = $dbh->codepare("set NAMES 'utf8'");
$sth->execute;
$sth->finish;
open(INPUT, "< $input_file")
or die "Couldn't open $input_file";
while () {
$dbh->do("INSERT INTO dict(name) VALUES(?)",undef, $_);
}
close(INPUT);
$dbh->disconnect();
- 匹配中文词库
#!/usr/bin/perl
# $Id$
# Generat keywords form Chinese dict and booknams
use strict;
use warnings;
use DBI;
my $db = 'zhu_tags_filter';
my $host = '192.168.1.249';
my $user = 'root';
my $password = '123456';
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
$user, $password);
$dbh->do("SET NAMES 'utf8'");
my $sth = $dbh->codepare("SELECT name FROM dict");
$sth->execute();
my $n = 0;
do {
while (my @row = $sth->fetchrow_array()) {
$n++;
my $keyword = $row[0];
my $hn = $dbh->codepare("SELECT id FROM all_booknames WHERE name LIKE '%$keyword%' LIMIT 1");
$hn->execute;
if ($hn->rows > 0){
$dbh->do("INSERT INTO keywords (name) VALUES('$row[0]')");
}
$hn->finish;
}
} until(!$sth->more_results)
Sunday, April 13, 2008
Hello, Catalyst!
If you wanna learn catalyst framework, follow links are very helpful ;)
- Wiki http://dev.catalyst.perl.org/
- Catalyst Cookbook http://search.cpan.org/~mstrout/Catalyst-Manual-5.701004/lib/Catalyst/Manual/Cookbook.pod
- Template Toolkit http://template-toolkit.org/
- DBix Class http://search.cpan.org/~ash/DBIx-Class-0.08010/
Wednesday, July 11, 2007
Catalyst 介绍
名字
Catalyst 指南 - Catalyst 介绍
描述
本文简单的介绍了为何要用 Catalyst 以及如何使用它。文中解释了 Catalyst 的工作原理并通过一个简单应用的快速建立来加以验证。
Catalyst 是什么?
Catalyst 是一个优雅的 Web 应用框架,极为灵活又特别简单。它类似于 Ruby on Rails、Java 的 Spring 和 Maypole(原来就基于 Maypole 建立)。
MVC
Catalyst 遵循模型-视图-控制(MVC)设计模式,它擅长将内容处理、表示和流程控制方面的工作区分开来交给独立的模块来做。这种区分允许你为某一方面的问题修改代码而不影响解决其它问题的代码。这样 Catalyst 提升了原有的解决 Web 应用方面的问题的模块的重用程度。
下面就是 M、V、C 分别解决的问题,每个方面都有著名的 Perl 模块的可用。
- Model
模型
存取和修改数据内容。Class::DBI、Plucene、Net::LDAP 等
- View
视图
向用户展示内容。Template Toolkit、Mason、HTML::Template 等
- Controller
控制器
控制整个请求阶段、检查参数、派发动作、流程控制。也就是 Catalyst!
如果你不熟悉 MVC 和设计模式,你得查看一下这方面的原始资料:Gamma、Helm、Johson、Vlissides 写的 Design Patterns,也叫 Gang of Four 或 GoF。你也可以 google 一下。有很多很多的 Web 应用框架都是基于 MVC 的,如前面提到的那些。
灵活性
Catalyst 比起其他的框架来说灵活很多。我们会慢慢的解释,很快就会看到那些你喜爱的 Perl 模块在 Catalyst 里面的应用。
- 多模型、视图和控制
为了要建立一个 Catalyst 应用,你得用名为 组件(Components) 的模块来处理各种问题。一般这样的代码会非常简单,只是调用 MVC 下面列出的 Perl 模块。Catalyst 用很灵活的方式来使用这些组件。在一个应用里面可以使用任意数量的模板、视图和控制模块。想要操作多个数据库并读写 LDAP 数据么?没问题。想要用 Template Toolkit 和 PDF::Template 来展现同样的模型么?很简单。
Thursday, January 4, 2007
关于Perl模块的重新安装
由于重新编译了Perl之后,发现以前安装的Perl模块出了不少问题~ 也不能用CPAN重新安装了,至少我不会,安装LWP也不行,不知道为什么。后来在IBM的站点上找到了这样的方式可以重新安装已经安装的PERL模块
$ /usr/local/bin/perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
Tuesday, January 2, 2007
安装 mod_perl
在安装mod_perl的时候提示不能安装,原因是因为Perl没有击活线程,怎么办?重新编译一次Perl 5.8.8 就可以了。
$ Configure --codefix=/opt/perl -Dusethreads
$ make && make test
# make install
Tuesday, December 12, 2006
DBD::mysql安装问题
在安装perl的DBD::mysql模块的时候遇到一些问题
$ perl -MCPAN -e shell
[....]
Can't exec "mysql_config": No such file or directory at Makefile.PL line 76.
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 454.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 454.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 454.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 454.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 454.
Can't exec "mysql_config": No such file or directory at Makefile.PL line 454.
Failed to determine directory of mysql.h. Use
[....]
进入DBD::mysql的代码安装目录
$ cd $HOME/.cpan/build/DBD-mysql-3.0008
$ perl --mysql_config=$MYSQL_PREFIX_DIR/bin/mysql_config
$ make
$ make test
$ make install
这样就安装成功了
Perl安装问题
在测试服务器上安装perl5.8.8
$./configure.gnu --codefix=$HOME/local
make
结果出错啦~
make[1]: Leaving directory `/home/.salk/ndw/src/perl-5.8.6/x2p'
Making B (dynamic)
opendir(./../../../../..): Permission denied at ../../lib/File/Find.pm line 597
Use of chdir('') or chdir(undef) as chdir() is decodecated at ../../lib/File/Find.pm line 741.
opendir(./..): Permission denied at ../../lib/ExtUtils/MakeMaker.pm line 165
Couldn't change to directory /home/.salk/ndw/C: No such file or directory at Makefile.PL line 18
Writing Makefile for B
Warning: No Makefile!
make[1]: Entering directory `/home/.salk/ndw/src/perl-5.8.6/ext/B'
make[1]: *** No rule to make target `config'. Stop.
make[1]: Leaving directory `/home/.salk/ndw/src/perl-5.8.6/ext/B'
make config failed, continuing anyway...
make[1]: Entering directory `/home/.salk/ndw/src/perl-5.8.6/ext/B'
make[1]: *** No rule to make target `all'. Stop.
make[1]: Leaving directory `/home/.salk/ndw/src/perl-5.8.6/ext/B'
make: *** [lib/auto/B/B.so] Error 2
用
$make minitest
来测试,结果在98.5%的地方出错了,没有办法,GOOGLE上搜索了一下,结果是编译的地方出错了,在 /tmp 下面编译一点问题都没有,看来是不是以后都要改变一下编译的习惯了? ;)
/tmp$ ./configure.gnu --codefix=$HOME/local && make && make test && make install
Wednesday, October 25, 2006
HOT PERL ONLINERS
Just enough perl to do most everything! Tom Christianson (spelling?)
once posted a canonical list of one line perl programs to do many common
command-line tasks.
It included:
# run contents of "my_file" as a program
perl my_file
# run debugger "stand-alone"
perl -d -e 42
# run program, but with warnings
perl -w my_file
# run program under debugger
perl -d my_file
# just check syntax, with warnings
perl -wc my_file
# useful at end of "find foo -print"
perl -nle unlink
# simplest one-liner program
perl -e 'print "hello world!\n"'
# add first and penultimate columns
perl -lane 'print $F[0] + $F[-2]'
# just lines 15 to 17
perl -ne 'print if 15 .. 17' *.pod
# in-place edit of *.c files changing all foo to bar
perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c
# command-line that prints the first 50 lines (cheaply)
perl -pe 'exit if $. > 50' f1 f2 f3 ...
# delete first 10 lines
perl -i.old -ne 'print unless 1 .. 10' foo.txt
# change all the isolated oldvar occurrences to newvar
perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy]
# command-line that reverses the whole file by lines
perl -e 'print reverse <>' file1 file2 file3 ....
# find palindromes
perl -lne 'print if $_ eq reverse' /usr/dict/words
# command-line that reverse all the bytes in a file
perl -0777e 'print scalar reverse <>' f1 f2 f3 ...
# command-line that reverses the whole file by paragraphs
perl -00 -e 'print reverse <>' file1 file2 file3 ....
# increment all numbers found in these files
perl i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 ....
# command-line that shows each line with its characters backwards
perl -nle 'print scalar reverse $_' file1 file2 file3 ....
# delete all but lines beween START and END
perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt
# binary edit (careful!)
perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape
# look for dup words
perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi'
# command-line that prints the last 50 lines (expensively)
perl -e 'lines = <>; print @@lines[ $#lines .. $#lines-50' f1 f2 f3 ...
Thursday, October 19, 2006
Perl Package Manager
这两天挺累的,有好消息,也有坏消息,我尽量保持冷静,毕竟,这是应该的。来成都第三个月,好好坏坏都经历过了,仿佛生活就是在用不同的方式去考验每一个,直到自己能够接受,或是重新再领悟过去,憧憬未来,好好过者现在。我试着去帮助每个人,而不是为了自己,我想这样生活会更有意义的,不是吗?
在Windows上使用Perl的模块怎么办?用CPAN很麻烦的,使用ActivePerl的PPM来管理吧,安装、升级、删除都可以方便的进行,可以使用GUI也可以使用命令行。不过在Cygwin我使用PPM失败了,不知道为什么,一会检查一下,哈哈,休息一下,整理一下心情的碎片,一天接一天,幸福就会一天天接近,不是吗?
Thursday, August 17, 2006
Perl script on cgiwrap
If I use some script like this
#!/usr/bin/perl
print "Hello World!";
It will give me error :
[Wed Aug 16 08:57:50 2006] [error] [client 222.209.97.197] malformed header from script. Bad header=Hello World...: /www/cgiwrap/cgi/cgiwrap
And I must do like this:
#!/usr/bin/perl
use CGI qw(:standard);
print header('Welcome to my home page!');
print start_html(), "Linux and PHP", end_html();
Friday, June 23, 2006
查看所安装的 Perl 模块
今天在perlchian的mail-list上有人提出这样的问题
如何查看已经安装的perl模块?
问答是这样的
use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
print join "\n", $inst->modules();
在我的 ubuntu dapper 上则看到这些模块,很显然,我在公司的个人工作电脑上还不是很喜欢用 perl 。
$ perl
use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
print join "\n", $inst->modules();
Archive::Tar
Archive::Zip
Comcodess::Zlib
Cwd
Digest::SHA
File::HomeDir
Module::Build
Module::Signature
Net::Telnet
Perl
Spiffy
Term::ReadKey
Term::ReadLine
Test::Base
Test::Simple
Text::Glob
YAML
Labels:
Linux,
Operating system,
perl,
system,
Tech,
ubuntulinux
Subscribe to:
Posts (Atom)