如何编写每次运行时生成新日志文件的Perl脚本?

时间:2009-11-06 08:58:41

标签: 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);

我道歉,我知道我的代码很乱,并提前感谢...

尼克

3 个答案:

答案 0 :(得分:4)

使用File::SpecPath::Class来操作文件名和路径:

#!/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";

输出(在Windows上):

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'
);

答案 1 :(得分:2)

您应该使用.(点)来连接字符串。这里的文件名字符串变为:

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

,(逗号)仅适用于print AFAIK,并且只是因为字符串中数组的常规转换是print的连接。

答案 2 :(得分: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

$