读取文件内容的脚本file_input3.pl(见http://blog.youkuaiyun.com/zhyuh_perl/archive/2010/03/05/5348799.aspx)可以写成一个通用的函数,供其它脚本调用,并将读取的结果返回。
==function0.pl==
#!/usr/bin/perl -w
#
# functon0.pl
# To be called by other scripts,
# to get parameters from configuration files
while (<FILE>){
chomp($_);
$line = $_;
$line =~ s/^/s*|/s*$//g;
@array1=split(/=/,$line);
$array1[0] =~ s/^/s*|/s*$//g;
$array1[1] =~ s/^/s*|/s*$//g;
$hash1{$array1[0]} = $array1[1];
}
1;
==调用function0.pl的主程序call_function0.pl==
#!/usr/bin/perl -w
#
# call_function0.pl
# Try to call another perl script "function0.pl",
# and get the values from "function0.pl"
use Data::Dumper;
$arg = shift(@ARGV);
while ($arg)
{
if ($arg eq "-f" or $arg eq "-F")
{
$file_name = shift(@ARGV);
}
$arg = shift(@ARGV);
}
open FILE,"$file_name" or die "Cannot open $file_name: $!";
require "function0.pl"; #调用function0.pl脚本
close(FILE);
$bin_dir=$hash1{'BIN_DIR'}; #从返回的结果%hash1里面取值
print Dumper(/%hash1); #输出%hash1内容
print Dumper($bin_dir);
==执行结果==
C:/temp1/temp1_prj>perl call_function0.pl -f Env_Var.txt
$VAR1 = {
'DATA_FILE_LOCATION' => 'I://INTEL_DATA',
'AUDIT_ARCHIVE_DIR' => 'H://INTEL_DB_DUMPS//GDCPW1169//AUDIT_ARCHIVE',
'BIN_DIR' => 'G://mssql2005//MSSQL.1//MSSQL//Binn',
'TRAN_DIR' => 'H://INTEL_TRAN_DUMPS//GDCPW1169',
'DUMP_DIR' => 'H://INTEL_DB_DUMPS//GDCPW1169',
'INSTANCE' => 'GDCPW1169',
'LOG_FILE_LOCATION' => 'J://INTEL_LOG',
'SERVER' => 'GDCPW1169',
'SCRIPT_LOG' => 'G://mssql2005//log',
'SQL_VER' => '2005'
};
$VAR1 = 'G://mssql2005//MSSQL.1//MSSQL//Binn';