Perl脚本间传递参数

读取文件内容的脚本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';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值