Perl获取Web内容,然后将其写为文本文件

时间:2014-12-29 21:31:33

标签: perl

我试图创建一个脚本,从网站上获取一个日志文件(内容),然后将其输入到文本文件中,但如果存在use strict,我会遇到错误:

不能使用字符串(" / home / User / Downloads / text")作为符号引用,而#34; strict refs"在./scriptname第92行使用。

同样通过删除use strict:我得到另一个错误:

文件名太长了./scriptname第91行。

我尝试了Perl: Read web text file and "open" it

但是,它对我不起作用。另外,我是Perl的新手并且混淆了Perl语法。

是否有任何建议或建议?

注意:代码会在RoomOutProcessTT存在的情况下覆盖整行,并将其与显示的次数一起显示。

这是代码。

my $FOutput = get "http://website/Logs/Log_number.ini";
my $FInput = "/home/User/Downloads/text";
open $FInput, '<', $FOutput or die "could not open $FInput: $!";
my $ctr;
my @results;
my @words = <$FInput>;
@results = grep /RoomOutProcessTT/, @words;
print "@results\n";
close $FInput;

open $FInput, '<', $FOutput or die "could not open $FInput: $!";
while(<$FInput>){
    $ctr = grep /RoomOutProcessTT/, split ' ' , $_;             
    $ctr += $ctr; 
}   
print "RoomOutProcessTT Count: $ctr\n";
close $FInput;

1 个答案:

答案 0 :(得分:0)

要打开的第一个参数是文件句柄名称,而不是文件的实际名称。那是后来的open函数。

将您的代码更改为:

my $FOutput = get "http://website/Logs/Log_number.ini"; # your content should be stored in this 
                                                        # variable, you need to write data to your output file.
my $FInput = "/home/User/Downloads/text";
open OUTPUT_FILEHANDLE, '>', $FInput or die "could not open $FInput: $!"; # give a name to the file 
                                                                          # handle, then supply the file name itself after the mode specifier. 
                                                                          # You want to WRITE data to this file, open it with '>'
my $ctr;
my @results;
my @words = split(/(\r|\n)/, $FOutput); # create an array of words from the content from the logfile
                                        # I'm not 100% sure this will work, but the intent is to show
                                        # an array of 'lines' corresponding to the data

# here, you want to print the results of your grep to the output file
@results = grep /RoomOutProcessTT/, @words;
print OUTPUT_FILEHANDLE "@results\n"; # print to your output file
# close the output file here, since you re-open it in the next few lines.
close OUTPUT_FILEHANDLE;

# not sure why you're re-opening the file here... but that's up to your design I suppose
open INPUT_FILEHANDLE, '<', $FInput or die "could not open $FInput: $!"; # open it for read
while(<INPUT_FILEHANDLE>){
    $ctr = grep /RoomOutProcessTT/, split ' ' , $_;             
    $ctr += $ctr; 
}   
print "RoomOutProcessTT Count: $ctr\n"; # print to stdout
close INPUT_FILEHANDLE;  # close your file handle

我可能会建议您更改用于识别&#34;输入和输出&#34;的条款,因为它有些令人困惑。在这种情况下,输入实际上是您从Web提取的文件,输出是您的文本文件。至少我是如何理解它的。您可能希望在最终设计中解决这个问题。