免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: deathcult
打印 上一主题 下一主题

Perl模块使用 => 简短例子代码集合!  关闭 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2003-06-13 15:33 |只看该作者

Perl模块使用 => 简短例子代码集合!

(20) Net:OP3, login(), list(), get()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Net::POP3;
  4. use Data::Dumper;

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

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

  11. foreach my $h(keys %$title)
  12. {
  13.         my $msg = $p->;get($h);
  14.         print @$msg;       
  15. }
  16. $p->;quit;
  17. exit 0;
复制代码


telnet pop3.web.com 110 也可以直接连到pop3 server上,然后通过
pop3命令与邮件服务器交互,
简单的命令有:
USER name
PASS string

STAT
LIST [n]
RETR msg
DELE msg
NOOP
RSET
QUIT
有兴趣的朋友可以试一试。
这样,也就可以利用Net::Telnet来做一个收信件的简单程序。

论坛徽章:
0
22 [报告]
发表于 2003-06-16 14:39 |只看该作者

Perl模块使用 => 简短例子代码集合!

(21) Term::ANSIColor 例子一

  1. #!/usr/bin/perl
  2. use strict;
  3. use Term::ANSIColor qw(:constants);

  4. $Term::ANSIColor::AUTORESET = 1;

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

  7. for my $i(0..length($str)-1)
  8. {
  9.         print BOLD RED substr($str, $i, 1);
  10.         select(undef, undef, undef, 0.3);
  11. }
  12. exit 0;
复制代码
   

查看ANSIColor.pm可以得知作者是利用ANSI转义序列,改变终端字符颜色的。
print "\e[34m\n";
即是改变前景色为blue;

shell命令为echo -e "\033[31m";#改变前景色为红色。
(freeBSD,Solaris下此命令测试OK)

论坛徽章:
0
23 [报告]
发表于 2003-06-16 14:57 |只看该作者

Perl模块使用 => 简短例子代码集合!

(21) Term::ANSIColor 例子二

  1. #!/usr/bin/perl
  2. use strict;
  3. use Term::ANSIColor qw(:constants);

  4. $Term::ANSIColor::AUTORESET = 1;

  5. $| = 1;

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

  8. print BOLD BLINK $str;
  9. exit 0;
复制代码


转义序列echo -e "\033[20;40H";可以改变光标位置。
perl中就可以:print "\e[20;40H";

详细请搜索精华。还有perldoc Term::ANSIColor 。

论坛徽章:
0
24 [报告]
发表于 2003-06-16 17:13 |只看该作者

Perl模块使用 => 简短例子代码集合!

(22) Date::Calc Calendar(), Today()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Date::Calc qw(Calendar Today);

  4. my $year = "2003";
  5. my $month = "6";
  6. my $day;


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

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

  10. print $cal;
  11. exit 0;
复制代码
   

本例子打印出一个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里) :)

论坛徽章:
0
25 [报告]
发表于 2003-06-20 13:45 |只看该作者

Perl模块使用 => 简短例子代码集合!

(23) Term::Cap, Tgetend(), Tgoto, Tputs()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Term::Cap;

  4. $| = 1;
  5. my $i = 1;
  6. my $flag = 0;

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

  9. while($i)
  10. {
  11.         if($i >; 50 || $flag == 1)
  12.         {
  13.                 $i --;
  14.                 $flag = 1;
  15.                 $flag = 0 if($i == 1);
  16.         }
  17.         else
  18.         {
  19.                 $i ++;       
  20.                 $flag = 0;
  21.         }

  22.         $tcap->;Tgoto('cm', $i, 15, *STDOUT);#move cursor
  23.         print " welcome to chinaunix! ";
  24.         select(undef, undef, undef, 0.02);
  25. }
  26. exit 0;
复制代码


Term::Cap 终端控制模块。     
代码效果:一个左右移动的字串 "welcome to chinaunix! " :)

论坛徽章:
0
26 [报告]
发表于 2003-06-20 13:46 |只看该作者

Perl模块使用 => 简短例子代码集合!

(24) HTTPD::Log::Filter

  1. #!/usr/bin/perl
  2. use strict;
  3. use HTTPD::Log::Filter;

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

  6. foreach(`cat access_log`)
  7. {
  8.         chomp;
  9.         unless( $filter->;filter($_) )
  10.         {
  11.                 print "[$_]\n";
  12.                 next;
  13.         }
  14.         print $filter->;request, "\n";
  15. }
  16. exit 0;
复制代码
   

如果我们工作中经常需要分析Apache日志,这个模块可以提供一些方便。
创建对象实例以后,用filter方法来过滤,没有正确匹配的行将返回false,
然后用相应的方法print出我们需要的数据。(host,request,date...等等方法,
由capture选项以参数引入)
可以用re方法打印出作者所使用的匹配模式:


  1. use HTTPD::Log::Filter;
  2. print HTTPD::Log::Filter->;new(format=>;"CLF",capture=>;['request'])->;re;
复制代码



详见perldoc HTTPD::Log::Filter. enjoy it

论坛徽章:
0
27 [报告]
发表于 2003-06-23 10:35 |只看该作者

Perl模块使用 => 简短例子代码集合!

提供者:Apile


(25) Net::LDAP

  1. #!/usr/bin/perl
  2. use Net::LDAP;

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

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

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

  20. #--作密碼認證
  21. $mesg = $ldap->;bind($entry->;dn, password =>; "abc", version =>; 3)
  22. ||die "can't connect to ldap";
  23. if($mesg->;code) { print "verification is failed"}
  24. else{ print "success"}
复制代码


LDAP version 3..可以用於查詢基本資料、驗證密碼之用..

论坛徽章:
0
28 [报告]
发表于 2003-06-26 17:37 |只看该作者

Perl模块使用 => 简短例子代码集合!

(26) Net::SMTP mail(), to(), data(), datasend(), auth()

  1. #!/usr/bin/perl

  2. use strict;
  3. use Net::SMTP;

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

  11. exit 0;

复制代码


有的SMPT Server需要Authentication,那么就使用auth()方法进行验证。
Debug模式打开,可以看到详细的SMTP命令代码。也有助于我们排错。

论坛徽章:
0
29 [报告]
发表于 2003-06-26 17:43 |只看该作者

Perl模块使用 => 简短例子代码集合!

(27) MIME::Base64, encode_base64(), decode_base64()

  1. #!/usr/bin/perl -w

  2. use strict;
  3. use MIME::Base64;

  4. foreach(<DATA>;)
  5. {
  6.         print decode_base64($_);
  7. }
  8. exit 0;

  9. __DATA__
  10. xOO6w6Osu7bTrcC0tb1jaGluYXVuaXguY29tIFtwZXJsXbDmIQo=
  11. 1eLKx2Jhc2U2NLHgwuu1xMD919OjrNPJTUlNRTo6QmFzZTY0xKO/6cC0veLC66GjCg==
  12. cGVybGRvYyBNSU1FOjpCYXNlNjQgZm9yIGRldGFpbHMsIGVuam95IGl0IDopCg==


复制代码
   

用来处理MIME/BASE64编码。

论坛徽章:
0
30 [报告]
发表于 2003-07-07 18:54 |只看该作者

Perl模块使用 => 简短例子代码集合!

(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()...

  1. #!/usr/bin/perl

  2. use strict;
  3. use Net::IMAP::Simple;

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

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

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

  17. exit 0;
复制代码
   

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


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

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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP