如何从 Perl 访问 INI 文件?
在 Perl 中解析 INI 文件并将其转换为哈希的最佳方法是什么?
What is the best way to parse INI file in Perl and convert it to hash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 Perl 中解析 INI 文件并将其转换为哈希的最佳方法是什么?
What is the best way to parse INI file in Perl and convert it to hash?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我更喜欢使用 Config::IniFiles 模块。
I prefer to use Config::IniFiles module.
如果您喜欢更perlish风格,请尝试
Tie::Cfg
。样本:If you like more perlish style then try
Tie::Cfg
. Sample:最好的方法是按照其他人的建议使用 CPAN 中的可用模块。下面仅供您自己理解,假设您有这样的 ini 文件:
您可以仅使用 Perl 的正则表达式(或字符串方法)+ 哈希等数据结构来进行自己的解析,无需模块。
示例代码:
输出
The best way is to make use of available modules in CPAN as what others have suggested. Below is just for your own understanding, let's say you have ini file like this:
you can do your own parsing w/o modules by using just Perl's regex (or string methods) + data structures like hashes.
Sample Code:
output
Config::Tiny 非常简单易用。
Config::Tiny is very easy and straightforward to use.
尝试来自 CPAN 的此模块: Config::INI::Reader
Try this module from CPAN: Config::INI::Reader
对上面的挑剔:
从 CPAN 下载的 Tie::Cfg 不处理可能包含空格的部分和键。当设置该部分和部分内的键的哈希条目时,需要通过在“键”周围添加引号(“)来更改它。我试图读取的文件是由 MS Windows 的人员生成的,因此 Config::Tiny、Config::IniFiles 对格式非常
挑剔,如果任何行不是 [section] 或 key=val 的形式,它们就会抛出错误并且无法访问。至少在 Config::Files 中,哈希值被正确填充,如果我尝试读取的文件中有一些虚假的 M4 行,我可以运行它。通过 m4 来摆脱,但这在我尝试使用这个特定脚本执行的操作中是不必要的。
Nitpicking on the above:
Tie::Cfg, as downloaded from CPAN, does not handle sections and keys that may have spaces in them. It needs to be changed by adding quotes (") around the "keys" when setting the hash entries for both the section and the keys within the sections. The files I'm trying to read were generated by MS Windows' folks, and thus have plenty of spaces to go around.
Config::Tiny, Config::IniFiles are fussy about the format. If any line is not of the form [section] or key=val, they throw an error and one can't get to the hashes, which, at least in Config::Files, are filled in properly anyway. It would be nice to have an ignore error option. The files I'm trying to read have some spurious M4 lines in it, that I could run through m4 to get rid of, but that is not necessary in what I'm trying to do with this particular script.
ini 文件的读写功能 编辑:
示例用法:
读取conf 文件并保存到散列
更改您的配置属性或添加新的配置属性
编辑
添加的新配置
将您的新配置保存到文件
reading and write function for ini file edit:
sample usage:
read conf file and saved to hash
change the your config attribute or added new config attributes
edit
added new config
saved your new configuration to file