For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2005 > Logfile name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Logfile name
Jan Eden

2005-01-24, 3:58 pm

Hi,

I create monthly log files with names like statistics_01_2005.log. Now I ha=
ve a function which returns the logfile name for the current month:

sub statfile {
my @date =3D localtime(time);
my ($month, $year) =3D @date[4,5];
$year +=3D 1900;
$month =3D sprintf ("%02d", $month+1); =20
return my $statfile =3D qq{statistics_${month}_${year}.log};
}

In some cases I need the name of last month's logfile. To achieve this, I m=
odified the subroutine like this:

sub statfile {
my $mode =3D shift;
my @date =3D localtime(time);
my ($month, $year) =3D @date[4,5];
$year +=3D 1900;
if ($mode) {
$month =3D sprintf ("%02d", $month);
$month =3D 12 if $month =3D=3D 0;
$year =3D $year - 1;
}
else {
$month =3D sprintf ("%02d", $month+1); =20
}
return my $statfile =3D qq{statistics_${month}_${year}.log};
}

This looks quite clumsy to me. Is there a more elegant way?

Thanks,

Jan
--=20
How many Microsoft engineers does it take to screw in a lightbulb? None. Th=
ey just redefine "dark" as the new standard.
Owen

2005-01-24, 8:55 pm

On Mon, 24 Jan 2005 14:31:53 +0100
Jan Eden <lists@janeden.org> wrote:

> Hi,
>
> I create monthly log files with names like statistics_01_2005.log. Now I have a function which returns the logfile name for the current month:
>
> sub statfile {
> my @date = localtime(time);
> my ($month, $year) = @date[4,5];
> $year += 1900;
> $month = sprintf ("%02d", $month+1);
> return my $statfile = qq{statistics_${month}_${year}.log};
> }
>
> In some cases I need the name of last month's logfile. To achieve this, I modified the subroutine like this:
>
> sub statfile {
> my $mode = shift;
> my @date = localtime(time);
> my ($month, $year) = @date[4,5];
> $year += 1900;
> if ($mode) {
> $month = sprintf ("%02d", $month);
> $month = 12 if $month == 0;
> $year = $year - 1;
> }
> else {
> $month = sprintf ("%02d", $month+1);
> }
> return my $statfile = qq{statistics_${month}_${year}.log};
> }



I just get all the file names and sort them, last months is the second last (or first)



opendir(DIR,$wherever) or die "Can't open DIR $!\n";
while (defined($file = readdir(DIR))) {
next unless($file =~ /\.log/); #just take out the .log files
push (@filenames,$file);
}
closedir(DIR);

# Now I get a sorted list for inclusion in a drop down menu

@filenames = sort{$a cmp $b}@filenames;




Owen
Jan Eden

2005-01-25, 8:55 am

Owen wrote on 25.01.2005:

>On Mon, 24 Jan 2005 14:31:53 +0100 Jan Eden <lists@janeden.org>
>wrote:
>
[snip][color=darkred]
[snip]
[color=darkred]
>I just get all the file names and sort them, last months is the
>second last (or first)
>
>opendir(DIR,$wherever) or die "Can't open DIR $!\n"; while
>(defined($file =3D readdir(DIR))) { next unless($file =3D~ /\.log/);
>#just take out the .log files push (@filenames,$file);
>}
>closedir(DIR);
>
># Now I get a sorted list for inclusion in a drop down menu
>
>@filenames =3D sort{$a cmp $b}@filenames;
>

Thanks, Owen, this is a nice solution, but would generate too much overhead=
when dealing with hundreds of log files.

- Jan
--=20
Common sense is what tells you that the world is flat.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com