Perl自动格式输出select 结果

#!/usr/bin/perl   
use strict;
use DBI;
my $dbName = 'orcl';  
my $dbUser = 'test';  
my $dbUserPass = 'test';  
my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database ";
if ($#ARGV <0){
   print "请输入一个参数
";
  exit(-1);
}
my $var=$ARGV[0];
my $sql = $var;

if ($var =~ /selects+(.+)s+froms+.*/i){ $a = $1};
my @arr = (split /,/,"$a");
foreach (@arr){
print "$_"." ";
}
my $sth = $dbh->prepare($sql); 
$sth->execute();  
while (my  @arr = $sth->fetchrow_array()){
print "
@arr
";
}
sh-3.2$ perl a5.pl "SELECT sid,serial# ,module from v$session" | more
sid serial#  module 
1626 521 

1627 1421 perl@jhoa (TNS V1-V3)

1630 3 

1634 2 

1635 1442 SQL*Plus

1640 9 

1645 1 

1646 1 

1647 1 

1648 1 

1649 1 

1650 1 

1651 1 

1652 1 

1653 1 

1654 1 

1655 1 

原文地址:https://www.cnblogs.com/hzcya1995/p/13351936.html