Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why Time::Local->timelocal and timelocal gets different result? 1

Status
Not open for further replies.

julianLiuRsch

Programmer
Oct 4, 2008
11
My platform is WinXP, activePerl 5.10.0. Please see the following program
---
use strict;
use Time::Local;

sub main {
my $t1=Time::Local->timelocal(0,0,12,4,9,108);
my $t2=timelocal(0,0,12,4,9,108);

print "t1 is [$t1]\n";
print "t2 is [$t2]\n";
}

&main;
===
The question is : t1 and t2 is different, why?

thanks in advance.
 
Time::Local has no OO interface, so using the arrow operator "->" is altering the usage of the arguments passed to the localtime function. This example shows what I mean:

Code:
my $t1=Time::Local->timelocal(0,12,4,9,108);
my $t2=timelocal(0,0,12,4,9,108);

Now they both have the correct time.

I assume my explanation is the correct one. If not hopefully someone will correct me.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Supplementary,

When you do this:

Class->method(@args)

or in your case:

Time::Local->localtime(0,0,12,4,9,108)

perl turns that into:

Class::method('Class',@args);

Or in your case:

Time::Local->localtime('Time::Local',0,0,12,4,9,108)

Try this and you will see:

my $t1=Time::Local->timelocal(0,0,12,4,9,108);
my $t2=timelocal('Time::Local',0,0,12,4,9,108);

Now they both have the same incorrect time because of the extra argument, so the 108 argument on the end is not passed into the localtime function (which only expects 6 arguments) and all the other arguments are now shifted one position to the right in the list of arguments. Since a string evaluated in numeric context will be zero, the 'Time::Local' argument is treated as a zero. So you in essence are passing this to the function:

my $t2=timelocal(0,0,0,12,4,9);

I drank too much coffee this morning.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I can not find the way to edit my posted article. Is that possible, and how?

One more question : why timelocal does not have a class name going before it? If a subroutine does not has a class name going before it, then is it still a method to a particular (common) class or it is not a method at all?
 
You can't edit posts after they are posted.

In the case of Time::Local, there are no objects, so there is no class and there are no methods. Its written in a simpler function style coding. So timelocal is a subroutine/function and not a method of any class/object.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top