Perl的新手和我的局限令人沮丧。非常感谢任何帮助。
创建将:
的脚本在input.txt文件中回显“这是一台机器:ford.com测试”
我在打印输出时出错了,因为我一直收到以下内容
在test21.pl第38行的void上下文中无用的字符串。
非权威性答案:在打印时使用未初始化的值$ _ test21.pl第39行。
0
#!/bin/perl
use strict;
use warnings;
use Net::Nslookup;
use File::Copy;
sub host {
my $result;
my $node = shift;
print system("nslookup $node.com | findstr ^Name: >> POOH");
open( my $stuff, "<", "POOH" ) || die "Can't open input.txt: $!";
while (<$stuff>) {
if (/(Name:)(\s+)(\w+)/) {
$result = $3;
}
}
return $result;
}
my $captured;
my $captured2;
my $jeff;
my $in = 'input.txt';
my $out = 'output.txt';
my $test = 'test.txt';
copy( $in, $out ) || die "File cannot be copied.";
open OUTPUT, "< $out" || die "Can't open input.txt: $!";
open TEST, "> $test" || die "Can't open input.txt: $!";
while (<OUTPUT>) { # assigns each line in turn to $_
if (/(Machine:)(\s)(\w+)/) {
$captured = $3;
$jeff = host($captured);
}
"s/$captured/$jeff/g";
print TEST;
}
答案 0 :(得分:0)
“s / $ captured / $ jeff / g”周围不需要引号。对于第二个问题,我会更清楚我打印到文件处理程序的内容。例如打印TEST $ _
答案 1 :(得分:0)
感谢大家的建议。在使用while循环输入一行代码后,将一些代码指向保存默认标量变量的代码放入变量中。这样做可以节省很多杂乱的编码。这是正常运行的代码。
#!E:/Perl/bin/perl
use strict;
use warnings;
sub host {
my $result;
my $node = shift;
print system("nslookup $node.com | findstr ^Name: >> LOOKUP");
open(my $stuff, "<", "LOOKUP") || die "Can't open input.txt: $!";
while (<$stuff>) {
if ( /(Name:)(\s+)(\w+)/ ) {
$result = $3;
}
}
return $result;
}
my $captured;
my $jeff;
open INPUT, '<', 'input.txt' || die "Can't open input.txt: $!";
open OUTPUT, '>', 'output.txt' || die "Can't open input.txt: $!";
while (<INPUT>) { # while loop inputs each line of input.txt file
my($line) = $_;
if ( /(Machine:)(\s)(\w+)/ ){
$captured = $3;
$jeff = host($captured);
$_ = $line;
$line =~ s/$captured/$jeff/g;
print OUTPUT $line;
}
}
close OUTPUT;
close INPUT;