码迷,mamicode.com
首页 > 其他好文 > 详细

perl package

时间:2014-11-10 18:16:58      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ar   os   sp   for   on   art   log   bs   as   

#!/usr/bin/perl -w
use strict;
use File::Basename;
use XML::Simple;

package CITIC;

my $TPMS_EOD_ETC=$ENV{TPMS_EOD_ETC};

#return script name
sub getscript_name{
         my($path)=@_;
         my $script_name=File::Basename::basename($path);
         #( my $pgm = $0 ) =~ s!.*\\(.*)!$1!g ;
         $script_name =~ s/.pl$|.sql$//;
         return $script_name;
}

#return dbh;
sub connect_db{
         my ($ip,$port,$sid,$user,$pwd)=@_;
         #CITIC::showtime();print "$ip,$port,$sid,$user,$pwd"."\n";
         my $dbh;
         eval{
                  $dbh = DBI->connect("DBI:Oracle:host=$ip;port=$port;sid=$sid",
                  $user,$pwd,{AutoCommit=>1,PrintError=>1,RaiseError=>0});
                  };
         #or die "Can‘t connect db,DBI->errstr";
         unless ($dbh){
                           CITIC::showtime();print DBI->errstr."\n";
                  }else{
                           CITIC::showtime();print "Connect db success!"."\n";
                  }
         return 
$dbh;
}

#return dbc info
sub get_dbc_info{
         my ($subdbc)=@_;
         my $dbc_file="dbc.xml";
         my $xmlfile = "${TPMS_EOD_ETC}${dbc_file}";
         my $xml = XML::Simple->new();
         my $info  = eval{$xml->XMLin($xmlfile,ForceArray => 1)};
         my $CONFIGINFO=$info;         
         my %dbc_info;
         foreach my $get_info(keys(%{$CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}})){
                  
                  $dbc_info{"user"} = $CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}->{"$get_info"}->{"user"};
                  $dbc_info{"pwd"} = $CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}->{"$get_info"}->{"pwd"};
                  $dbc_info{"ip"} = $CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}->{"$get_info"}->{"ip"};
                  $dbc_info{"port"} = $CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}->{"$get_info"}->{"port"};
                  $dbc_info{"sid"} = $CONFIGINFO->{"config"}->{"$subdbc"}->{"info"}->{"$get_info"}->{"sid"};
                  
         }
         return %dbc_info;
}

#去除空格
sub strimstr{
         my $str=shift;
         s/^\s+//;
         s/\s+$//;
         return $str;
}

#return hh:mm:ss yyyymmdd
sub showtime{
         my($sec,$min,$hour,$day,$mon,$year)=localtime(time());
         my 
$current="";
         $sec=sprintf("%02d",$sec);
         $min=sprintf("%02d",$min);
         $hour=sprintf("%02d",$hour);
         $day=sprintf("%02d",$day);
         $mon=sprintf("%02d",$mon+1);
         $year+=1900;
         $current="$hour".":$min".":$sec"." $year"."$mon"."$day";
         print "${current} INFO :    ";

}
#return yyyymmdd 
sub getdate{
         my($sec,$min,$hour,$day,$mon,$year)=localtime(time());
         my $current="";
         $sec=sprintf("%02d",$sec);
         $min=sprintf("%02d",$min);
         $hour=sprintf("%02d",$hour);
         $day=sprintf("%02d",$day);
         $mon=sprintf("%02d",$mon+1);
         $year+=1900;
         $current="$year"."$mon"."$day";
         return "${current}";

}
#return openfile
sub create_logfile($$){
         my ($filename,$path)=@_;
         my $get_date=getdate();
         my $get_time=localtime(time());
         open(FILE,">","${path}${filename}_${get_date}.log") or die "Can`t Create file :$!";
         print FILE "-" x 8 ."[$get_time] Start"."-" x 8 . "\n";
         return *FILE;
}
#close open;
sub close_logfile($){
         my ($log_file)=@_;
         my $get_time=localtime(time());
         print $log_file "-" x 8 ."[$get_time] End"."-" x 8 . "\n";
         close($log_file);
}

#return true;
1;


perl package

标签:ar   os   sp   for   on   art   log   bs   as   

原文地址:http://my.oschina.net/suzm/blog/342739

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!