on Tue, 09 Jul 2002 22:12:52 GMT, [EMAIL PROTECTED] wrote:
> I'm trying to get my script to recognize the date and time.
> I thought I had it but I keep getting errors all over.
>
> Please take a look and tell me whats wrong
> [...]
> sub get_date {
> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
> localtime(time);
> @months = ("01","02","03","04","05","06",
> "07","08","09","10","11","12");
> @digits = split(//,$sec);
> $size = @digits;
> if ($size == 1) {
> $sec = "0$sec";
> }
> @digits = split(//,$min);
> $size = @digits;
> if ($size == 1) {
> $min = "0$min";
> }
> $year=$year+1900;
> $date = "@months[$mon]/$mday/$year";
> }
Some general comments:
- always turn on warnings and strictures
- always use my-variables, especially in subroutines
- learn sprintf (see perldoc -f sprintf)
- return the result of a subroutine call instead of storing it in a
global variable
With this, you get:
#! perl -w
use strict;
print get_date();
sub get_date {
my ($sec,$min,$hour,$mday,$mon,$year) = localtime;
return sprintf("%02d/%02d/%04d", $mon+1, $mday, $year+1900);
}
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]