I'm not sure how to call another subroutine from a sub declared in a perl module. The subroutine is simply getting date information.
I have a file handle open for a log file and I want to print out some date information to this logfile, but its not printing out. I can see that it touches the file, I made the log file -rwx too.
Here is the sample subroutine in perl (Test.pm) module that I am calling:
sub spGetCurDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my $currentDateTime = sprintf "%4d-%02d-%02d %02d:%02d:%02d",
$year+1900, $mon+1, $mday, $hour, $min, $sec;
return $curDateTime;
}
Here's part of my test code where I am trying to call the subroutine from (Test.pm)
use Test.pm;
#Create log
open (LOG, ">/home/dev/test.log") || die "cannot append";
sub get_alert {
undef $/;
open (my $QFH, "< /home/dev/test.sql") or die "error can't open this file $!";
print LOG "Checking on status time =>", &spGetCurDateTime, "\n";
my $sth= $dbh->prepare(<$QFH>) ||
die ("Cannot connect to the database: ".$DBI::errstr."\n");
$sth->execute;
close $QFH;
my $row = $sth->fetchrow_hashref;
$sth->finish;
return $row->{RESULT};
print $row;
}
POSIX::strftime
, it is half the speed ofsprintf
. - Chas. Owensstrftime
does a lot more thansprintf
does. For instance, you can saymy @time = localtime; $time[2] += 24; print strftime "%F %T\n", @time;
. If you were to try that withsprintf
you would get an invalid date. Basically, if all you want to do is format localtime as it comes back, thensprintf
may be a better choice, especially if it is in a function that gets called a lot. If you need to normalize a time or provide a nice interface to someone else,strftime
is your friend. - Chas. Owens