【发布时间】:2015-09-10 11:58:19
【问题描述】:
我知道如何使用 perl 脚本打开文件并显示全部内容,但是如何将文件中的字符串读入变量中
以下是打开文件并显示全部内容
#!/usr/bin/perl
use strict;
use warnings;
my $filename = '/home/abc/data.txt';
if (open(my $fh, '<', $filename)) {
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}
} else {
warn "Could not open file '$filename' $!";
}
我的要求是
#!/usr/bin/perl
use strict;
use warnings;
my $filename = '/home/abc/data.txt';
my $foo = ""
if (open(my $fh, '<', $filename)) {
---- Read test_reg_ip string from data.txt into variable
$foo=test_reg_ip;
} else {
warn "Could not open file '$filename' $!";
}
print "$foo\n";
下面是输入文件data.txt
#############################################################################################
# mon_server_ip
# This parameter value should point to the IP address where the mon Server
# is installed
#############################################################################################
mon_server_ip = 127.0.0.1
#############################################################################################
# test_reg_ip
# This parameter value should point to the IP address where reg server is
# installed
#############################################################################################
test_reg_ip = 127.0.0.1
#############################################################################################
# mon_port
# This parameter value should point to the mon port
#############################################################################################
【问题讨论】:
标签: perl