Tag Archive for 'perl'

Install DBD::mysql with CPAN

1
2
3
4
5
6
7
8
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

自动回答CPAN安装

1
cpan> o conf coderequisites_policy follow

Perl check LDAP password online

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!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 =~ /<td width="35%"><b>(.+)<\/b><\/td><\/tr>/;
        }
        else {
            print $res->status_line, "\n";
        }
    }

}

用CPAN安装perl module

  1. 首先配置CPAN Module,方法是在bash下面运行
    1
    $ perl -MCPAN -eshell

    配置过程会询问一些问题,其中CPAN的镜像可以选择http://cpan.linuxforum.net/

  2. 接下来要更新CPAN Module,而不是直接装LWP。否则会遇到undefined subroutine &Digest::base::new之类的错误。方法是运行
    1
    cpan> install Bundle::CPAN

    这一步是关键,切记切记。我开始不知道,还在网上搜索这个错误信息,发现有人说是Perl的问题,我就把整个Cygwin更新到1.5.19.4,结果啥用也没有,还把其它东西弄得一塌糊涂。

  3. 装好新的CPAN Module,先退出,然后再运行1里面的命令进入CPAN的shell,这时就可以装LWP了
    1
    cpan> install Bundle::LWP

    安装Bundle::LWP意味着不仅安装LWP,而且安装它需要的其它module。

使用Perl和MySQL找出书名中的中文词语

使用Perl找出书名中的中文词语,用于搜索引擎:

  1. 导入中文词库到MySQL数据库
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #!/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 (<INPUT>) {
        $dbh->do("INSERT INTO dict(name) VALUES(?)",undef, $_);
    }
    close(INPUT);

    $dbh->disconnect();
  2. 匹配中文词库
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    #!/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)

Hello, Catalyst!

If you wanna learn catalyst framework, follow links are very helpful ;)

Catalyst 介绍

  • 快速起步
  • 工作原理
  • 流程控制
  • 组件

  • 测试
  • 支持
  • 作者
  • 版权声明

  • 名字

    Catalyst 指南 – Catalyst 介绍


    描述

    本文简单的介绍了为何要用 Catalyst 以及如何使用它。文中解释了 Catalyst 的工作原理并通过一个简单应用的快速建立来加以验证。

    Catalyst 是什么?

    Catalyst 是一个优雅的 Web 应用框架,极为灵活又特别简单。它类似于 Ruby on Rails、Java 的 Spring 和 Maypole(原来就基于 Maypole 建立)。

    MVC

    Catalyst 遵循模型-视图-控制(MVC)设计模式,它擅长将内容处理、表示和流程控制方面的工作区分开来交给独立的模块来做。这种区分允许你为某一方面的问题修改代码而不影响解决其它问题的代码。这样 Catalyst 提升了原有的解决 Web 应用方面的问题的模块的重用程度。

    下面就是 M、V、C 分别解决的问题,每个方面都有著名的 Perl 模块的可用。

    如果你不熟悉 MVC 和设计模式,你得查看一下这方面的原始资料:Gamma、Helm、Johson、Vlissides 写的 Design Patterns,也叫 Gang of Four 或 GoF。你也可以 google 一下。有很多很多的 Web 应用框架都是基于 MVC 的,如前面提到的那些。

    灵活性

    Catalyst 比起其他的框架来说灵活很多。我们会慢慢的解释,很快就会看到那些你喜爱的 Perl 模块在 Catalyst 里面的应用。

    关于Perl模块的重新安装

    由于重新编译了Perl之后,发现以前安装的Perl模块出了不少问题~ 也不能用CPAN重新安装了,至少我不会,安装LWP也不行,不知道为什么。后来在IBM的站点上找到了这样的方式可以重新安装已经安装的PERL模块

    1
    $ /usr/local/bin/perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'

    安装 mod_perl

    在安装mod_perl的时候提示不能安装,原因是因为Perl没有击活线程,怎么办?重新编译一次Perl 5.8.8 就可以了。

    1
    2
    3
    $ Configure --codefix=/opt/perl -Dusethreads
    $ make && make test
    # make install

    DBD::mysql安装问题

    在安装perl的DBD::mysql模块的时候遇到一些问题

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ 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的代码安装目录

    1
    2
    3
    4
    5
    $ cd $HOME/.cpan/build/DBD-mysql-3.0008
    $ perl --mysql_config=$MYSQL_PREFIX_DIR/bin/mysql_config
    $ make
    $ make test
    $ make install

    这样就安装成功了