简体   繁体   English

如何使用perl从命令行读取配置文件(自动化)

[英]How to read a config file from a command line using perl (automation)

I am new to Perl and need help with this. 我是Perl的新手,需要帮助。

I want to read a config file and see if the first 10 lines of the file contents starts with # and have or not have some particular string to do some automation in Perl. 我想读取一个配置文件,并查看文件内容的前10行是否以#开头,并且是否具有某些特定的字符串来在Perl中进行一些自动化。

I was able to open the file using: 我可以使用以下方式打开文件:

 $obj->cmd("path");
 $a = ->obj(cat "/path/.config");

I don't know if we can save this to variable or is there any way I can copy it to the text file and search it. 我不知道是否可以将其保存到变量中,或者有什么方法可以将其复制到文本文件中并进行搜索。

Also how do I edit this file like adding or removing file content. 另外,我如何编辑此文件,例如添加或删除文件内容。

Thank you 谢谢

To open a file for reading use: 要打开文件以供阅读,请执行以下操作:

open(my $fh, "<", "/path/.config") or die "Can't open < .config: $!";

To open a file for writing 打开要写入的文件

open(my $fh, ">", "/path/.config") or die "Can't open > .config: $!";

the content of the file is then assigned to $fh 然后将文件内容分配给$fh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM