Reputation: 11
I am trying to modify a Perl script to create a different/new log file each time I run the script. I was trying to create each log file by date, but I am having trouble incorporating this concept... This is what I have so far:
#!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);
I apologize, I know my code is messy, and thanks in advance...
Nick
Upvotes: 1
Views: 6401
Reputation: 57
I use a BEGIN block:
BEGIN
{
open (STDERR,"> $0.txt");
print STDERR scalar localtime, "\n";
}
The $0 is the name of the current script. So I add '.txt' so it's always easy to find. And the single redirect '>' ensures it doesn't grow if you forget about it. Just use '>>' if you want to append. Apache (or whatever server you run) saves log files as well, but they are all in the same file.
Upvotes: 0
Reputation: 11
You're forgetting to do error checking, so
use autodie;
We should all prefer a ISO8601 formatted date/time stamp :)
#!/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
$
Upvotes: 0
Reputation: 4680
You should use .
(dot) to concatenate strings. Here for the filename string it becomes :
open (OUTPUT_FILE, ">> PLAYLIST_TABLE_" . ($tm->mon+1) . "/" . $tm->mday . "/" . ($tm->year+1900) . ".txt") or die $!;
The ,
(comma) works only with print
AFAIK, and only because the usual transformation of an array in string is concatenation for print
.
Upvotes: 2
Reputation: 118128
Use File::Spec or Path::Class to manipulate file names and paths:
#!/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";
Output (on Windows):
PLAYLIST_TABLE_11\06\2009.txt
However, I would recommend you use:
my $filename = catfile(
sprintf('%04d.txt', $year + 1900)
sprintf('PLAYLIST_TABLE_%02d', $mon + 1),
sprintf('%02d', $mday),
);
so log files from runs close in time remain 'close' in the file system.
Please avoid having really long lines of string concatenations. They make it really hard to see what is going on an hide syntax errors in clutter. Instead, you could use join
:
join('/',
"PLAYLIST_TABLE_" . ($tm->mon + 1),
$tm->mday,
($tm->year + 1900) . '.txt'
);
Upvotes: 4