读取自定义配置文件
# more test.conf? ### Database Configration #### host = 10.8.9.20; dbname = webdb ; user = admin ; pwd = admin; 2.测试程序内容 #!/usr/bin/perl use strict; our($host,$dbname,$user,$password); my $prog_root="/usr/test_prog"; my $prog_conf = $prog_root."/test.conf"; open my $conf_file,"<",$prog_conf; my @conf_items = <$conf_file>; close($conf_file); foreach my $line(@conf_items){ ? ?#### read each line from configration file ? ?$host = $1 if ($line =~/hosts*=s*(S+)s*;/i); ? ?$dbname=$1 if ($line =~/dbnames*=s*(S+)s*;/i); ? ?$user=$1 if ($line =~/users*=s*(S+)s*;/i); ? ?$password=$1 if ($line =~/pwds*=s*(S+)s*;/i); } print "$hostn"; print "$dbnamen"; print "$usern"; print "$passwordn"; 3.运行输出结果 # perl conf.pl? 10.8.9.20 webdb admin admin (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |