免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: afeiguo
打印 上一主题 下一主题

如何利用Perl来解析config文件 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-06-10 11:19 |只看该作者
谢谢楼上各位大侠!

我的愿意是想写一个自动化脚本,帮助用户设置安装软件的环境变量,安装路径,并提示用户如何安装;
以前没有接触过perl,以为perl应该很快能搞定。
现在发现perl需要掌握的东西也很多啊,而且要写自动化的东西,除了perl,还需要正则表达式等

谢谢楼上各位, 刚明白了一些概念, 刚刚买了 “小骆驼”书,希望多多指教!

论坛徽章:
0
12 [报告]
发表于 2008-06-10 14:26 |只看该作者
@zheng兄,

你的code可以分析config文件,谢谢。

但是, 我想将config文件中所有的变量都以“健-值”的形式存到Hash表中,如何做?
你的code好像是只存储一行(即一个变量,):如下:


#!/usr/bin/perl



use strict;
use warnings;

open (CONFIG_FILE, "config") || die ("Could not open file");

while(<CONFIG_FILE>){
     chomp;                  # no newline

       s/#.*//;                # no comments

       s/^\s+//;               # no leading white

       s/\s+$//;               # no trailing white

       next unless length;     # anything left?

     my %result = my ($keys,$values) = split(/\s*=\s*/, $_, 2);
     print "\$result{$keys} = $result{$keys}\n";

}

论坛徽章:
0
13 [报告]
发表于 2008-06-10 14:48 |只看该作者
那是我在前面的一位兄弟的代码上改的,只是作为一个参考!
这个问题其实很简单的,只是 'my' 引起的:
#!/usr/bin/perl


use strict;
use warnings;

open (CONFIG_FILE, "config") || die ("Could not open file");

my %result=();
while(<CONFIG_FILE>){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chomp;                  # no newline

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s/#.*//;                # no comments

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s/^\s+//;               # no leading white

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s/\s+$//;               # no trailing white

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next unless length;     # anything left?


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#my %result = my ($keys,$values) = split(/\s*=\s*/, $_, 2);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my ($keys,$values) = split(/\s*=\s*/, $_, 2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result{$keys} = $values;
}

close(CONFIG_FILE);


[ 本帖最后由 @zheng 于 2008-6-10 14:53 编辑 ]

论坛徽章:
0
14 [报告]
发表于 2008-06-10 15:09 |只看该作者

回复 #12 afeiguo 的帖子

呵呵,小弟愚笨,刚刚想到了修改@zheng兄的法子,如下:

#!/usr/bin/perl



use strict;
use warnings;

open (CONFIG_FILE, "config") || die ("Could not open file");

my %result; #static global Hash variable

while(<CONFIG_FILE>){
     chomp;                  # no newline

       s/#.*//;                # no comments

       s/^\s+//;               # no leading white

       s/\s+$//;               # no trailing white

       next unless length;     # anything left?

     my ($keys,$values) = split(/\s*=\s*/, $_, 2);
     $result{$keys} = $values;
     print "\$result{$keys} = $result{$keys}\n";

}

论坛徽章:
0
15 [报告]
发表于 2008-06-10 15:10 |只看该作者

回复 #12 afeiguo 的帖子

谢谢@zheng兄!

论坛徽章:
0
16 [报告]
发表于 2008-06-10 15:48 |只看该作者
原帖由 @zheng 于 2008-6-10 14:48 发表
那是我在前面的一位兄弟的代码上改的,只是作为一个参考!
这个问题其实很简单的,只是 'my' 引起的:
#!/usr/bin/perl


use strict;
use warnings;

open (CONFIG_FILE, "config") || die ("Could no ...

这段代码是可以分析config文件,呵呵,比较完善一些。

论坛徽章:
0
17 [报告]
发表于 2008-06-10 17:35 |只看该作者
有现成的还是别自己写了吧,perl本来就是给人用来偷懒的...

论坛徽章:
0
18 [报告]
发表于 2008-06-10 17:39 |只看该作者

回复 #17 converse 的帖子

converse兄指的是楼上大侠提到的 "config:simple" 或者"config:general"模块吗?

小弟刚用perl,模块(及其机制)还不会使,惭愧。

论坛徽章:
0
19 [报告]
发表于 2008-06-10 17:46 |只看该作者

回复 #18 afeiguo 的帖子

我不知道,没用过,但我知道一定有这样的模块,没必要自己写,不就是那点东西嘛,写来写去也没什么价值,把时间用在其它地方吧.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP