全部博文(85)
分类:
2010-01-19 14:46:57
#!/usr/bin/perl -w
use strict;
use LWP::Simple qw(get);
my $url = shift || "";
my $content = get($url);
print $content;
exit 0;
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(gettimeofday usleep);
my ($start_sec, $start_usec, $end_sec, $end_usec, $time_used);
my $micro_sec = 100000;
($start_sec, $start_usec) = gettimeofday;
foreach(1..20)
{
print `date +\%H:\%M:\%S`;
usleep($micro_sec);
}
($end_sec, $end_usec) = gettimeofday;
$time_used = ($end_sec - $start_sec) + ($end_usec - $start_usec)/1000000;
printf("time used : %.3fsec\n", $time_used);
exit 0;
#!/usr/bin/perl
use strict;
use Net::FTP;
my $user = "anonymous";
my $passwd = "chinaunix@";
my $host = "ftp.freebsd.org";
my $ftp = Net::FTP->;new("$host", Debug =>; 0)
or die "Can't connect to $host: $@\n";
$ftp->;login("$user","$passwd")
or die "Can't login\n", $ftp->;message;
$ftp->;cwd("/pub/FreeBSD/doc/")
or die "Can't change dir\n", $ftp->;message;
$ftp->;get("README")
or die "get failed\n", $ftp->;message;
$ftp->;quit;
exit 0;
#!/usr/bin/perl
use strict;
use Expect;
my $timeout = 2;
my $delay = 1;
my $cmd = "ssh";
my @params = qw/202.108.xx.xx -lusername -p22/;
my $pass = "passwd";
my $exp = Expect->;spawn($cmd, @params) or die "Can't spawn $cmd\n";
$exp->;expect($timeout, -re=>;'[Pp]assword:');
$exp->;send_slow($delay, "$pass\r\n");
$exp->;interact();
$exp->;hard_close();
exit 0;
#!/usr/bin/perl -w
use strict;
use XML::Simple;
my $text = <
;; ;
; ;
;php ;
;net.php.servlet ;
; ;
;php ;
;*.php ;
xml
my $x = XMLin($text);
foreach my $tag(keys %$x)
{
my %h = %{$$x{$tag}};
foreach(keys %h)
{
print "$tag =>; ";
print "$_ =>; $h{$_}\n";
}
}
exit 0;
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
print Dumper(\@INC);
print Dumper(\%ENV);
exit 0;
#!/usr/bin/perl -w
use strict;
use IO::Socket;
my $host = "";
my $port = "80";
my $http_head = "GET / HTTP/1.0\nHost: $host:$port\n\n";
my $sock = IO::Socket::INET->;new("$host:$port")
or die "Socket() error, Reason : $! \n";
print $sock $http_head;
print <$sock>;;
exit 0;
#!/usr/bin/perl
use strict;
use Date::Manip;
my $date=&DateCalc("today","-1 days", 0);#yesterday
my $date=&UnixDate($date, "%Y-%m-%d %T");
print "$date\n";
exit 0;
#!/usr/bin/perl
use strict;
use Date::Manip;
my $date1 = "Fri Jun 6 18:31:42 GMT 2003";
my $date2 = "2003/05/06";
my $flag=&Date_Cmp($date1,$date2);
if($flag<0)
{
print "date1 is earlier!\n";
}
elsif($flag==0)
{
print "the two dates are identical!\n";
}
else
{
print "date2 is earlier!\n";
}
exit 0;
#!/usr/bin/perl -w
use strict;
use File::Find;
my $file = "access.log";
my $path = "/";
find(\&process, $path);
sub process{ print $File::Find::dir, "$_\n" if(/$file/); }
exit 0;
#!/usr/bin/perl
use strict;
use ExtUtils::Installed;
my $inst= ExtUtils::Installed->;new();
my @modules = $inst->;modules();
foreach(@modules)
{
my $ver = $inst->;version($_) || "???";
printf("%-12s -- %s\n", $_, $ver);
}
exit 0;
#!/usr/bin/perl
use strict;
use DBI;
my $dbh = DBI->;connect("dbi:mysql:dbname", 'user','passwd', '')
or die "can't connect!\n";
my $sql = qq/show variables/;
my $sth = $dbh->;prepare($sql);
$sth->;execute();
while(my @array=$sth->;fetchrow_array())
{
printf("%-35s", $_) foreach(@array);
print "\n";
}
$dbh ->; disconnect();
exit 0;
#!/usr/bin/perl
use strict;
use Getopt::Std;
my %opts;
getopts("c:hv", \%opts);
foreach(keys %opts)
{
/c/ && print "welcome to ", $opts{$_} || "ChinaUnix", "!\n";
/h/ && print "Usage : $0 -[hv] -[c msg] \n";
/v/ && print "This is demo, version 0.001.001 built for $^O\n";
}
exit 0;
#!/usr/bin/perl
use strict;
use Proc::ProcessTable;
my $pt = new Proc::ProcessTable;
foreach(reverse sort @{$pt->;table})
{
print $_->;pid, " =>; ";
print $_->;cmndline, "\n";
}
exit 0;
#!/usr/bin/perl
use strict;
use Shell;
print "now is : ", date();
print "current time is : ", date("+%T");
my @dirs = ls("-laF");
foreach(@dirs)
{
print if(/\/$/);#print directory
}
exit 0;
#!/usr/bin/perl
use strict;
use Time::HiRes qw(sleep time);
$| = 1;
my $before = time;
for my $i (1..100)
{
print "$i\n";
sleep(0.01);
}
printf("time used : %.5f seconds\n", time - $before);
exit 0;
#!/usr/bin/perl
use strict;
use HTML::LinkExtor;
my $p = new HTML::LinkExtor;
$p->;parse_file(*DATA);
foreach my $links ($p->;links())
{
map {print "$_ "} @{$links};
print "\n";
}
exit 0;
__DATA__
"">;
;
;
;;CPAN ;
;
;
;;
;
;
;
; ; ;alt="[CPAN Logo]" height="121" width="250"/>; | ;; ; | ;
; ; ;2003-06-10 online since 1995-10-26 ;1662 MB 246 mirrors ;2903 authors 4767 modules | ;|
; ; | ;; | ;
;;Browsing;
| ;;;Searching;
;FAQ etc;; ;; | ;
; ; | ;; ; | ;
#!/usr/bin/perl
use strict;
use Net::Telnet;
my $p = Net::Telnet->;new();
my $h = shift || "";
$p->;open(Host =>; $h, Port =>; 80);
$p->;print("GET /\n");
while(my $line = $p->;getline())
{
print $line;
}
exit 0;
#!/usr/bin/perl
use strict;
use Compress::Zlib;
my $gz = gzopen("a.gz", "rb");
while( $gz->;gzreadline(my $line) >; 0 )
{
chomp $line;
print "$line\n";
}
$gz->;gzclose();
exit 0;
#!/usr/bin/perl
use strict;
use Net::POP3;
use Data::Dumper;
my $user = "user";
my $pass = shift or die "Usage : $0 passwd\n";
my $host = "pop3.web.com";#pop3 address
my $p = Net::POP3->;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;
#!/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;
#!/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;
#!/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;
#!/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;
#!/usr/bin/perl
use strict;
use HTTPD::Log::Filter;
my $filter = HTTPD::Log::Filter->;new(format =>; "CLF",
capture =>; ['request', 'host']);
foreach(`cat access_log`)
{
chomp;
unless( $filter->;filter($_) )
{
print "[$_]\n";
next;
}
print $filter->;request, "\n";
}
exit 0;
use HTTPD::Log::Filter;
print HTTPD::Log::Filter->;new(format=>;"CLF",capture=>;['request'])->;re;
#!/usr/bin/perl
use Net::LDAP;
## get a object of ldap
$ldap = Net::LDAP->;new("1.1.1.1", port =>;"389", version =>; 3) or die "$@";
# object of Net::LDAP::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"}
#!/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;
#!/usr/bin/perl -w
use strict;
use MIME::Base64;
foreach(;)
{
print decode_base64($_);
}
exit 0;
__DATA__
xOO6w6Osu7bTrcC0tb1jaGluYXVuaXguY29tIFtwZXJsXbDmIQo=
1eLKx2Jhc2U2NLHgwuu1xMD919OjrNPJTUlNRTo6QmFzZTY0xKO/6cC0veLC66GjCg==
cGVybGRvYyBNSU1FOjpCYXNlNjQgZm9yIGRldGFpbHMsIGVuam95IGl0IDopCg==
#!/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;
#!/usr/bin/perl -w
use Bio::DB::GenBank;
use Bio::SeqIO;
my $gb = new Bio::DB::GenBank;
my $seqout = new Bio::SeqIO(-fh =>; \*STDOUT, -format =>; 'fasta');
# if you want to get a bunch of sequences use the batch method
my $seqio = $gb->;get_Stream_by_id([ qw(27501445 2981014)]);
while( defined ($seq = $seqio->;next_seq )) {
$seqout->;write_seq($seq);
}
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode; #gb support
my $oExcel = new Spreadsheet::ParseExcel;
die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
my $code = $ARGV[1] || "CP936"; #gb support
my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->;new(Unicode_Map =>; $code); #gb support
my $oBook = $oExcel->;Parse($ARGV[0], $oFmtJ);
my($iR, $iC, $oWkS, $oWkC);
print "FILE :", $oBook->;{File} , "\n";
print "COUNT :", $oBook->;{SheetCount} , "\n";
print "AUTHOR:", $oBook->;{Author} , "\n"
if defined $oBook->;{Author};
for(my $iSheet=0; $iSheet < $oBook->;{SheetCount} ; $iSheet++)
{
$oWkS = $oBook->;{Worksheet}[$iSheet];
print "--------- SHEET:", $oWkS->;{Name}, "\n";
for(my $iR = $oWkS->;{MinRow} ;
defined $oWkS->;{MaxRow} && $iR <= $oWkS->;{MaxRow} ;
$iR++)
{
for(my $iC = $oWkS->;{MinCol} ;
defined $oWkS->;{MaxCol} && $iC <= $oWkS->;{MaxCol} ;
$iC++)
{
$oWkC = $oWkS->;{Cells}[$iR][$iC];
print "( $iR , $iC ) =>;", $oWkC->;Value, "\n" if($oWkC);
}
}
}
#!/usr/bin/perl
use strict;
use Text::CSV_XS;
my @columns;
my $csv = Text::CSV_XS->;new({
'binary' =>; 1,
'quote_char' =>; '"',
'sep_char' =>; ','
});
foreach my $line(;)
{
chomp $line;
if($csv->;parse($line))
{
@columns = $csv->;fields();
}
else
{
print "[error line : ", $csv->;error_input, "]\n";
}
map {printf("%-14s\t", $_)} @columns;
print "\n";
}
exit 0;
__DATA__
id,compact_sn,name,type,count,price
37,"ITO-2003-011","台式机,compaq","128M","290","1,2900"
35,I-BJ-2003-010,"显示器,硬盘,内存",'三星',480,"1,4800"
55,"C2003-104",笔记本,"Dell,Latitude,X200",13900,"1,13900"
#!/usr/bin/perl
use Benchmark;
timethese(100,
{
'local'=>;q
{
for(1..10000)
{
local $a=$_;
$a *= 2;
}
},
'my'=>;q
{
for(1..10000)
{
my $a=$_;
$a *= 2;
}
}
});
#!/usr/bin/perl
use HTTP::Daemon;
$| = 1;
my $wwwroot = "/home/doc/";
my $d = HTTP::Daemon->;new || die;
print "Perl Web-Server is running at: ", $d->;url, " ...\n";
while (my $c = $d->;accept)
{
print $c "Welcome to Perl Web-Server
;";
if(my $r = $c->;get_request)
{
print "Received : ", $r->;url->;path, "\n";
$c->;send_file_response($wwwroot.$r->;url->;path);
}
$c->;close;
}
#!/usr/bin/perl
use Array::Compare;
$comp = Array::Compare->;new(WhiteSpace =>; 1);
$cmd = "top -n1 | head -4";
@a1 = `$cmd`;
@a2 = `$cmd`;
@result = $comp->;full_compare(\@a1, \@a2);
foreach(@result)
{
print $_ + 1, "th line:\n";
print ">; $a1[$_]>; $a2[$_]";
print "-----\n";
}
exit 0;
#!/usr/bin/perl
use Algorithm::Diff qw(diff);
die("Usage: $0 file1 file2\n") if @ARGV != 2;
my ($file1, $file2) = @ARGV;
-T $file1 or die("$file1: binary\n");
-T $file2 or die("$file2: binary\n");
@f1 = `cat $file1 `;
@f2 = `cat $file2 `;
$diffs = diff(\@f1, \@f2);
foreach $chunk (@$diffs)
{
foreach $line (@$chunk)
{
my ($sign, $lineno, $text) = @$line;
printf "$sign%d %s", $lineno+1, $text;
}
print "--------\n";
}
(37) HTML::Parser
#!/usr/bin/perl
use List::Util qw/max min sum maxstr minstr shuffle/;
@s = ('hello', 'ok', 'china', 'unix');
print max 1..10; #10
print min 1..10; #1
print sum 1..10; #55
print maxstr @s; #unix
print minstr @s; #china
print shuffle 1..10; #radom order
#!/usr/bin/perl
use LWP::Simple;
use HTML::Parser;
my $url = shift || "";
my $content = LWP::Simple::get($url) or die("unknown url\n");
my $parser = HTML::Parser->;new(
start_h =>; [\&start, "tagname, attr"],
);
$parser->;parse($content);
exit 0;
sub start
{
my ($tag, $attr, $dtext, $origtext) = @_;
if($tag =~ /^img$/)
{
if (defined $attr->;{'src'} )
{
print "$attr->;{'src'}\n";
}
}
}
(2)发送html内容
#!/usr/bin/perl
use Mail::Sender;
$sender = new Mail::Sender{
smtp =>; 'localhost',
from =>; 'xxx@localhost'
};
$sender->;MailFile({
to =>; 'xxx@xxx.com',
subject =>; 'hello',
file =>; 'Attach.txt'
});
$sender->;Close();
print $Mail::Sender::Error eq "" ? "send ok!\n" : $Mail::Sender::Error;
#!/usr/bin/perl
use Mail::Sender;
open(IN, "< ./index.html") or die("");
$sender = new Mail::Sender{
smtp =>; 'localhost',
from =>; 'xxx@localhost'
};
$sender->;Open({
to =>; 'xxx@xxx.com',
subject =>; 'xxx',
msg =>; "hello!",
ctype =>; "text/html",
encoding =>; "7bit",
});
while(;)
{
$sender->;SendEx($_);
}
close IN;
$sender->;Close();
print $Mail::Sender::Error eq "" ? "send ok!\n" : $Mail::Sender::Error;