tags:

views:

297

answers:

3

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 advanced...

Nick

+3  A: 

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.

Steve Schnepp
Alright thanks man, I knew it had to be something simple haha, thanks! :)
nick
Mark his answer then!
Geo
because the code doesnt work...
nick
Please avoid long string concatenations. They hide errors in both logic and syntax in the clutter (as in this case).
Sinan Ünür
@nick: I edited my answer since I forgot the `$` on the last `$tm->year`. Sorry I couldn't test it before.
Steve Schnepp
+4  A: 

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'
);
Sinan Ünür
A: 

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

$
yo