您好,登錄后才能下訂單哦!
本篇內容主要講解“Perl怎么連接數據庫mysql”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Perl怎么連接數據庫mysql”吧!
一、命令行參數
1.1 代碼
#!/usr/bin/env perl use Getopt::Long; use Data::Dumper; use Pod::Usage; my $Debug = 1; sub dprint (@) { return unless $Debug; chomp(my @m = @_); print STDERR 'DEBUG: ', join(" ", @m),"\n"; } MAIN: { my $db_user; my $db_pass; GetOptions( \my %opt, 'debug|d+' => \$Debug, 'help|h|?' => sub{ pod2usage(-verbose=>1) }, 'db_user|u=s' => \$db_user, 'db_pass|x=s' => \$db_pass, ) or pod2usage(); pod2usage(-message=>"must requires db_user, db_pass!") unless $db_user && $db_pass; dprint "db_user : $db_user"; dprint "db_pass : $db_pass"; }
1.2 使用
命令:“--” 或 “-”
./test.pl --db_user testuser --db_pass testpass
./test.pl --u testuser --x testpass
二、連接數據庫mysql
2.1 代碼
#!/usr/bin/env perl use DBI; MAIN: { my $dbh = DBI->connect("DBI:mysql:clouxns:202.85.222.121:3306", "root", "zxc1758!&%*"); $query_sql = "SELECT zone_id, zone_name FROM xns_zone"; $query_sth = $dbh->prepare($query_sql); $query_sth->execute() or die "can't run query sql:$dbh->errstr"; while(my @data = $query_sth->fetchrow_array()) { my $zone_id = $data[0]; my $zone_name = $data[1]; print "$zone_id\t$zone_name\n"; } $dbh->disconnect(); }
三、DNS查詢
dns查詢A記錄、NS、SOA。
#!/usr/bin/env perl use Net::DNS::Resolver; use Net::DNS::Packet; MAIN: { my $resolver = Net::DNS::Resolver->new; #Find a host's address my $query = $resolver->search("www.guowenyan.cn"); if($query) { foreach my $rr ( grep { $_->type eq "A" } $query->answer ) { print $rr->address,"\n"; } } else { warn "query failed:",$resolver->errorstring,"\n"; } #Find the nameservers for a domain. my $query = $resolver->search("baidu.com", "NS"); if($query) { foreach my $rr ( grep { $_->type eq "NS" } $query->answer ) { print $rr->nsdname,"\n"; } } else { warn "query failed:",$resolver->errorstring,"\n"; } #Find a domain's SOA record in zone file format. my $query = $resolver->search("baidu.com", "SOA"); if($query) { ($query->answer)[0]->print; } else { warn "query failed:",$resolver->errorstring,"\n"; } }
四、發送郵件
4.1 代碼
#!/usr/bin/env perl use Net::SMTP; sub send_mail() { my @mail_to = ('480160531@qq.com', '2135361204@qq.com'); my $mail_from = 'kan_haha001@163.com'; my $mail_pass = "123zxc"; my $mail_message = "aanbb"; my $smtp = Net::SMTP->new("smtp.163.com"); $smtp->auth($mail_from, $mail_pass) || die "Auth Error! $!"; foreach my $mail_to (@mail_to) { $smtp->mail($mail_from); $smtp->to($mail_to); #Start the mail $smtp->data(); #Send the header $smtp->datasend("From: $mail_from\n"); $smtp->datasend("To: $mail_to\n"); $smtp->datasend("Subject: the mail is sended by perl.\n"); $smtp->datasend("\n"); #send the message $smtp->datasend("$mail_message\n"); #send the termination string $smtp->dataend(); } $smtp->quit(); }
4.2 注意事項
1. 需要安裝Authen::SASL:perl -MCPAN -e shell cpan>install Authen::SASL。
2. 發件人、收件人必須是單引號('kan_haha001.163.com')。
3. 郵件頭中的發件人、收件人、主題、正文的前后都必須有\n。(否則會系統退信)
到此,相信大家對“Perl怎么連接數據庫mysql”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。