[perl]: file -- read_file.pl
一、代码
1 #!/usr/bin/env perl 2 3 4 # file_name: read_file.pl 5 6 7 sub read_file 8 { 9 open(DATA, "$_[0]") or die "[ db.pl ]: that file dose not exist, $!" ; 10 11 12 $i = 0 ; 13 while (<DATA>) 14 { 15 print "[". ++$i . "] " . $_ ; 16 } 17 $i = 0 ; 18 } 19 20 21 read_file("<db.pl");
二、运行结果
1 [wit@fedora perl]$ ./read_file.pl 2 [1] #!/usr/bin/perl -w 3 [2] 4 [3] 5 [4] # file_name: db.pl 6 [5] 7 [6] 8 [7] use strict; 9 [8] use DBI; 10 [9] 11 [10] sub database 12 [11] { 13 [12] my $host = "10.10.10.30"; 14 [13] my $driver = "MariaDB"; 15 [14] my $db = "dbtest"; 16 [15] my $dsn = "DBI:$driver:database=$db:$host"; 17 [16] my $user = "admin"; 18 [17] my $passwd = "211224ln"; 19 [18] 20 [19] 21 [20] # connect to db 22 [21] my $dbh = DBI->connect($dsn, $user, $passwd ) or die $DBI::errstr; 23 [22] my $sth = $dbh->prepare("SELECT * FROM test"); 24 [23] $sth->execute(); # sql 25 [24] 26 [25] 27 [26] # get data 28 [27] while ( my @row = $sth->fetchrow_array() ) 29 [28] { 30 [29] print join("\t", @row)."\n"; 31 [30] } 32 [31] 33 [32] 34 [33] $sth->finish(); 35 [34] $dbh->disconnect(); 36 [35] } 37 [36] 38 [37] 39 [38] # run part 40 [39] database(); 41 [40] 42 [41] 43 [wit@fedora perl]$ 44 [wit@fedora perl]$
三、参考文档
1、 Perl 文件操作 -- https://www.runoob.com/perl/perl-files.html
标签:--,db,perl,read,file,my,pl From: https://www.cnblogs.com/lnlidawei/p/17991325