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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about using Date::Calc, and a general question about Perl 2

Status
Not open for further replies.

septemberlogic

Technical User
Feb 10, 2007
15
US
Hi,

I've had some past experience in C++ and Java but I'm pretty new to Perl. I was trying to use the Date::Calc module, after briefly looking through the Calc.pod page on CPAN, I realized that I didn't even know how to create a new Date object. In Java, it would be something like:

Date DateName = new Date(year, month, day);

But in Date::Calc, I was not able to find a "new()" method. After playing around for a while, I realized I can declare a new date simply with the following line (correct me if I'm wrong):

$DateName = ($year, $month, $day);

This puzzled me more than it delighted me. I mean, how can you declare an object by equating it to an array? Further more, how does Perl know that I'm trying to create a Date object?

I guess I just don't get OOP in Perl yet. It would be nice if someone can explain it to me. Much thanks in advance!
 
Kevin,

Thanks. Now that makes sense. So you are saying that by using the module, I'm just using its member subroutines to manipulate 3-element arrays?

Thanks again!
 
well, sort of. But this is not really an array:

($year, $month, $day);

it's a list, a list of arguments in this case. There are lots of functions in Date::Calc, some take more and some take less arguments.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
One quick question:

I have created a Date object with the following line:

$myDate = Date::Calc->today();

How do I find out which day of the week is it? One way to do so is:

print Day_of_Week($myDate->year(),$myDate->month(),$myDate->day());

but it feels like there's gotta be some easier way to do so.

Thanks!

P.S: By the way, how come I didn't need to add Date::Calc when I called the Day_of_Week() method? I only realized this after the code compiled successfully.
 
Date::Calc exported the subroutine Day_of_Week into the caller's namespace, "main::" in this case, so that you can call the function simply by calling its sub name (as if it was a sub you created yourself in your own code), as opposed to having to call "Date::Calc::Day_of_Week"

-------------
Cuvou.com | The NEW Kirsle.net
 
If that's the case, why did I have to use:

Date::Calc->today() instead of today()?

I tried the latter and it didn't work. Thanks Kirsle!!
 
Modules can export some functions by default, export others only when requested (eg. "use Digest::MD5 qw(md5_hex)" exports md5_hex() only when requested from the script in that way), and it can keep other ones as private-only and never export them.

Date::Calc might export some by default and others by request.

-------------
Cuvou.com | The NEW Kirsle.net
 
module documentation usually tells what is impoted by default and what needs to be imported "manually". Not always, but the date-calc modules seem pretty well documented so I assume they do.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin! I'll check it out. What about my previous question:

I have created a Date object with the following line:

$myDate = Date::Calc->today();

How do I find out which day of the week it is? One way to do so is:

print Day_of_Week($myDate->year(),$myDate->month(),$myDate->day());

but it feels like there's gotta be some easier way to do so.

Sorry if these questions seem silly. I'm quite a Perl n00b =)
 
I don't know the answer off the top of me head. I would have to read the module documentation to find out, same as you.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You're welcome

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

Part and Inventory Search

Sponsor

Back
Top