本篇内容主要讲解“Perl怎么连接数据库MySQL”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Perl怎么连接数据库mysql”吧!
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站建设、吉州网络推广、成都微信小程序、吉州网络营销、吉州企业策划、吉州品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供吉州建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
一、命令行参数
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”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!