2009-11-06 61 views
1

我想修改一個Perl腳本來創建一個不同的/新的日誌文件,每次我運行腳本。我試圖創建日期的每個日誌文件,但我有將這一概念的麻煩......這是我到目前爲止有:如何編寫一個每次運行時都會生成一個新日誌文件的Perl腳本?

#!perl -w 

use WWW::Mechanize; 

# What URL shall we retrieve? 
$url = "http://www.mediabase.com/whatsong/whatsong.asp?var_s=075068071069045070077"; 

# Create a new instance of WWW::Mechanize 
# enabling autocheck checks each request to ensure it was successful, 
# producing an error if not. 
my $mechanize = WWW::Mechanize->new(autocheck => 1); 

# Retrieve the page 
$mechanize->get($url); 

# Assign the page content to $page 
my $page = $mechanize->content; 

# Output the page 
#print $page; 

# Let's also save the page locally 
open LOG, ">>", "102.1_content.txt"; 

#print LOG $page; #######get error here when run from c++.... 

close(LOG); 
######################################## 
######################################## 
# INPUT_FILE 
open INPUT_FILE, "<", "102.1_content.txt"; 
######################################## 
my $html = join '', <INPUT_FILE>; 

my @stuff = $html =~ />([^<]+)</g; 
######################################## 

use Time::localtime; 
$tm = localtime; 
print "*****", $tm->mon+1, "/", $tm->mday, "/", 
     $tm->year+1900, "--", $tm->hour, "::", 
     $tm->min, "::", $tm->sec, 
     "******************************"; 
##sec, min, hour, mday, mon, year, wday, yday, and isdst 
######################################## 
# OUTPUT_FILE 
open OUTPUT_FILE, ">>", ("PLAYLIST_TABLE_"$tm->mon+1, "/", $tm->mday, "/", tm->year+1900".txt") or die $!; 
######################################## 
print OUTPUT_FILE "******************************", 
      $tm->mon+1, "/", $tm->mday, "/", $tm->year+1900, 
      "--", $tm->hour, "::", $tm->min, "::", $tm->sec, 
      "******************************"); 

print join (" ", @stuff), "\n"; 

print OUTPUT_FILE join ("   ", @stuff), "\n"; 

print "thats all!\n"; 

close(INPUT_FILE); 
close(OUTPUT_FILE); 

我道歉,我知道我的代碼是亂了,感謝提前...

尼克

+8

我看到「使用嚴格」失敗,一個語法錯誤或兩個錯誤,並嘗試使用正則表達式解析HTML。您應該修復這些以避免以後發生麻煩。 – 2009-11-06 12:20:47

回答

2

您應該使用.(點)來連接字符串。這裏的文件名的字符串就變成:

open (OUTPUT_FILE, ">> PLAYLIST_TABLE_" . ($tm->mon+1) . "/" . $tm->mday . "/" . ($tm->year+1900) . ".txt") or die $!; 

,(逗號)只能與print AFAIK,並且僅因爲在串陣列的通常轉化爲print級聯。

+0

好的,謝了,我知道它必須是簡單的東西,哈哈,謝謝! :) – nick 2009-11-06 09:10:21

+2

然後馬克他的答案! – Geo 2009-11-06 10:57:01

+0

因爲代碼不起作用... – nick 2009-11-06 11:09:41

4

使用File::SpecPath::Class操縱文件名和路徑:(在Windows上)

#!/usr/bin/perl 

use strict; 
use warnings; 

use File::Spec::Functions qw(catfile); 

my ($mday, $mon, $year) = (localtime)[3 .. 5]; 

my $filename = catfile(
    sprintf('PLAYLIST_TABLE_%02d', $mon + 1), 
    sprintf('%02d', $mday), 
    sprintf('%04d.txt', $year + 1900) 
); 

print $filename, "\n"; 

輸出:

 
PLAYLIST_TABLE_11\06\2009.txt 

不過,我會建議你使用:

my $filename = catfile(
    sprintf('%04d.txt', $year + 1900) 
    sprintf('PLAYLIST_TABLE_%02d', $mon + 1), 
    sprintf('%02d', $mday), 
); 

因此運行時間接近的日誌文件在文件系統中保持「關閉」狀態。

請避免有很長的字符串連接行。它們使得很難看到混亂中隱藏的語法錯誤是怎麼回事。相反,你可以使用join

join('/', 
    "PLAYLIST_TABLE_" . ($tm->mon + 1), 
    $tm->mday, 
    ($tm->year + 1900) . '.txt' 
); 
0

你忘了做錯誤檢查,所以

use autodie; 

我們都應該喜歡ISO8601格式化的日期/時間戳記:)

#!/usr/bin/perl -- 

use strict; 
use warnings; 

use File::Spec; 
use POSIX(); 

my $thisf = File::Spec->rel2abs(__FILE__); 
my $thisl = sprintf '%s-log-%s.txt', $thisf, POSIX::strftime(q!%Y-%m-%d-%H.%M.%SZ!,gmtime); 

print " 
thisf $thisf 
thisl $thisl 
"; 

__END__ 

$ perl tmp.pl 

thisf /home/boy/tmp.pl 
thisl /home/boy/tmp.pl-log-2009-11-08-20.35.38Z.txt 

$ 
相關問題