LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: devel

perl 常用模块使用例子------欢迎大家补充。

[复制链接]
 楼主| 发表于 2003-12-6 23:39:52 | 显示全部楼层
(20) Net:OP3, login(), list(), get()[php]
#!/usr/bin/perl
use strict;
use Net:OP3;
use Data:umper;

my $user = "user";
my $pass = shift or die "Usage : $0 passwd\n";
my $host = "pop3.web.com";#pop3 address

my $p = Net:OP3->new($host) or die "Can't connect $host!\n";
$p->login($user, $pass) or die "user or passwd error!\n";
my $title = $p->list or die "No mail for $user\n";

foreach my $h(keys %$title)
{
   my $msg = $p->get($h);
   print @$msg;   
}
$p->quit;
exit 0;[/php]
telnet pop3.web.com 110 也可以直接连到pop3 server上,然后通过
pop3命令与邮件服务器交互,
简单的命令有:
USER name
PASS string

STAT
LIST [n]
RETR msg
DELE msg
NOOP
RSET
QUIT
有兴趣的朋友可以试一试。
这样,也就可以利用Net::Telnet来做一个收信件的简单程序。
 楼主| 发表于 2003-12-6 23:40:19 | 显示全部楼层
(21) Term::ANSIColor 例子一[php]
#!/usr/bin/perl
use strict;
use Term::ANSIColor qw(:constants);

$Term::ANSIColor::AUTORESET = 1;

$| = 1;
my $str = "Welcome to chinaunix ^_^!\n";

for my $i(0..length($str)-1)
{
   print BOLD RED substr($str, $i, 1);
   select(undef, undef, undef, 0.3);
}
exit 0;[/php]
查看ANSIColor.pm可以得知作者是利用ANSI转义序列,改变终端字符颜色的。
print "\e[34m\n";
即是改变前景色为blue;

shell命令为echo -e "\033[31m";#改变前景色为红色。
(freeBSD,Solaris下此命令测试OK)
 楼主| 发表于 2003-12-6 23:41:32 | 显示全部楼层
(21) Term::ANSIColor 例子二[php]
#!/usr/bin/perl
use strict;
use Term::ANSIColor qw(:constants);

$Term::ANSIColor::AUTORESET = 1;

$| = 1;

print "\e[20;40H";
my $str = "Welcome to chinaunix ^_^!\n";

print BOLD BLINK $str;
exit 0;[/php]


转义序列echo -e "\033[20;40H";可以改变光标位置。
perl中就可以:print "\e[20;40H";
 楼主| 发表于 2003-12-6 23:42:02 | 显示全部楼层
(22) Date::Calc Calendar(), Today()[php]
#!/usr/bin/perl
use strict;
use Date::Calc qw(Calendar Today);

my $year = "2003";
my $month = "6";
my $day;


my $cal = Calendar($year, $month);
(undef, undef, $day) = Today();

$cal =~ s/$day/\e[5m\e[31m$day\e[0m/;

print $cal;
exit 0;[/php]


本例子打印出一个2003年6月份的日历,当天日期用红色的闪烁数字表示。

Date::Calc提供了时间日期计算的另一种方式(一种是Date::Manip),
大量简单方便的方法(函数)供使用者调用。

在例子中的年和月我是自己指定的,也可以
($year, $month, $day) = Today();

颜色和闪烁是用ANSI escape sequences。
详细说明尽在ANSIColor.pm source和perldoc Term::ANSIColor里。
(perldoc Term::ANSIColor其实也在ANSIColor.pm source里) :)
 楼主| 发表于 2003-12-6 23:42:29 | 显示全部楼层
(23) Term::Cap, Tgetend(), Tgoto, Tputs()[php]
#!/usr/bin/perl
use strict;
use Term::Cap;

$| = 1;
my $i = 1;
my $flag = 0;

my $tcap = Term::Cap->Tgetent({TERM => undef, OSPEED => 1});
$tcap->Tputs('cl', 1, *STDOUT);#clear screen

while($i)
{
   if($i > 50 || $flag == 1)
   {
      $i --;
      $flag = 1;
      $flag = 0 if($i == 1);
   }
   else
   {
      $i ++;   
      $flag = 0;
   }

   $tcap->Tgoto('cm', $i, 15, *STDOUT);#move cursor
   print " welcome to chinaunix! ";
   select(undef, undef, undef, 0.02);
}
exit 0;[/php]
Term::Cap 终端控制模块。
代码效果:一个左右移动的字串 "welcome to chinaunix! " :)
 楼主| 发表于 2003-12-6 23:43:09 | 显示全部楼层
(24) HTTPD:og::Filter[php]
#!/usr/bin/perl
use strict;
use HTTPD:og::Filter;

my $filter = HTTPD:og::Filter->new(format => "CLF",
                           capture => ['request', 'host']);

foreach(`cat access_log`)
{
   chomp;
   unless( $filter->filter($_) )
   {
      print "[$_]\n";
      next;
   }
   print $filter->request, "\n";
}
exit 0;[/php]
如果我们工作中经常需要分析Apache日志,这个模块可以提供一些方便。
创建对象实例以后,用filter方法来过滤,没有正确匹配的行将返回false,
然后用相应的方法print出我们需要的数据。(host,request,date...等等方法,
由capture选项以参数引入)
可以用re方法打印出作者所使用的匹配模式:

代码:

use HTTPD:og::Filter;
print HTTPD:og::Filter->new(format=>"CLF",capture=>['request'])->re;



详见perldoc HTTPD:og::Filter. enjoy it
 楼主| 发表于 2003-12-6 23:54:22 | 显示全部楼层
提供者:Apile


(25) Net:DAP[php]
#!/usr/bin/perl
use Net:DAP;

## get a object of ldap
$ldap = Net:DAP->new("1.1.1.1", port =>"389", version => 3) or die "$@";
# object of Net:DAP::Message
$mesg = $ldap->bind($_cer_id, password => $_cer_pw); # 查詢用的ID/PASSWD
if($mesg->is_error) {die $mesg->error;}
$mesg = $ldap->search(
         base => "o=abc,c=tt", # 起始點
         scope => "sub", # 範圍
         filter => "(uid=apile)", # 條件
         attrs => ["cn"], # 要取得的attribute
         typesonly => 0   );

my $max_len = $mesg->count; ## get number of entry

#--取得中文姓名,可能不只一筆
for($i=0;$i<$max_len;$i++){
   $entry = $mesg->entry($i);
   $cname = $entry->get_value("cn"); # get chinese name
}

#--作密碼認證
$mesg = $ldap->bind($entry->dn, password => "abc", version => 3)
||die "can't connect to ldap";
if($mesg->code) { print "verification is failed"}
else{ print "success"}
[/php]

LDAP version 3..可以用於查詢基本資料、驗證密碼之用..
 楼主| 发表于 2003-12-6 23:55:03 | 显示全部楼层
(26) Net::SMTP mail(), to(), data(), datasend(), auth()[php]
#!/usr/bin/perl

use strict;
use Net::SMTP;

my $smtp = Net::SMTP->new('smtp.sohu.com', Timeout => 10, Debug => 0)
   or die "new error\n";
#$smtp->auth("user", "passwd") or die "auth error\n";
$smtp->mail('some');
$smtp->to('some@some.com');
$smtp->data("chinaunix,哈楼你好啊!\n:)");
$smtp->quit;

exit 0;[/php]
有的SMPT Server需要Authentication,那么就使用auth()方法进行验证。
Debug模式打开,可以看到详细的SMTP命令代码。也有助于我们排错。
 楼主| 发表于 2003-12-6 23:55:32 | 显示全部楼层
(27) MIME::Base64, encode_base64(), decode_base64()[php]
#!/usr/bin/perl -w

use strict;
use MIME::Base64;

foreach(<DATA>)
{
   print decode_base64($_);
}
exit 0;[/php]
__DATA__
xOO6w6Osu7bTrcC0tb1jaGluYXVuaXguY29tIFtwZXJsXbDmIQo=
1eLKx2Jhc2U2NLHgwuu1xMD919OjrNPJTUlNRTo6QmFzZTY0xKO/6cC0veLC66GjCg==
cGVybGRvYyBNSU1FOjpCYXNlNjQgZm9yIGRldGFpbHMsIGVuam95IGl0IDopCg==


用来处理MIME/BASE64编码。
 楼主| 发表于 2003-12-6 23:56:09 | 显示全部楼层
(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()...[php]
#!/usr/bin/perl

use strict;
use Net::IMAP::Simple;

my $server = new Net::IMAP::Simple( 'imap.0451.com' );
$server->login( 'user_name', 'passwd');

#show the mailboxs
#map {print "$_\n";} $server->mailboxes();

#show mail's content
my $n = $server->select( 'inbox' ) or die "no this folder\n";
foreach my $msg ( 1..$n )
{
    my $lines = $server->get( $msg );
    print @$lines;
   print "_________________ Press enter key to view another! ...... __________________\n";
   read STDIN, my $key, 1;
}

exit 0;[/php]


在取得中文的Folder时,会出现乱码的情况,
这个问题现在没有解决。英文的Folder则没问题。


IMAP协议,默认端口为143,可以用telnet登录。

telnet imap.xxx.com 143
2 login user pass
2 list "" *
2 select inbox
......
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表