十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“Perl怎么连接数据库mysql”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Perl怎么连接数据库mysql”吧!
创新互联建站是专业的新兴网站建设公司,新兴接单;提供成都做网站、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行新兴网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!一、命令行参数
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”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!