Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Getting Today's Date - Strange Problem

by svsingh (Priest)
on May 28, 2003 at 03:23 UTC ( [id://261196]=perlquestion: print w/replies, xml ) Need Help??

svsingh has asked for the wisdom of the Perl Monks concerning the following question:

I needed to stick today's date into the output for a program and couldn't get it to work. I followed the Cookbook's example too. Here's the defective code:
use strict; use Time::localtime; my ($day, $month, $year) = (localtime)[3,4,5]; print "$month/$day/$year\n"; my $s = "$month/$day/$year"; print "s: $s\n";
And it returns:
Use of uninitialized value at test.pl line 5. Use of uninitialized value at test.pl line 5. Use of uninitialized value at test.pl line 5. //

Now, here's the strange part. If I remove the use Time::localtime; line, then the script does exactly what it's supposed to. I couldn't find any mention of this being a bug and all of the examples I'm seeing include the use ... line.

By leaving out the line, am I going to run into problems down the road? Thanks for your advice.

I'm also aware this will print 103 as the year. I'm using the straight value for simplicity here.

Replies are listed 'Best First'.
Re: Getting Today's Date - Strange Problem
by Zaxo (Archbishop) on May 28, 2003 at 03:31 UTC

    Your code is fine without Time::localtime, which is just a hashed(?) modification of the builtin localtime. You are using the builtin correctly.

    You may be interested in POSIX::strftime, which formats time strings from the list output of localtime.

    After Compline,
    Zaxo

Re: Getting Today's Date - Strange Problem
by JamesNC (Chaplain) on May 28, 2003 at 05:10 UTC
    Looks like a namespace problem when you use Time::localtime, the pod for Time::localtime; shows localtime namespace being an Time::tm=ARRAY(0x224f8c) and the localtime3,4,5 assign ARRAY refs as well, if you use the module looks like you need to do this... it kinda looks cleaner than trying to remember what array element maps to what... ++ for interesting question
    use Time::localtime; my $year = 1900 + localtime->year; my $month = 1 + localtime->mon; my $day = localtime->mday; print "$year-$month-$day\n"; #OUTPUT: 2003-5-28
Re: Getting Today's Date - Strange Problem
by NetWallah (Canon) on May 28, 2003 at 05:15 UTC
    According to the documentation,
    Time::localtime overrides the core localtime() function, replacing it with a version that returns "Time::tm" objects.

    Your "use" statement caused localtime() to return OBJECTS, instead of an array, which caused the errors you posted.

    The previous monks answer is right on - you don't need the "use" statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://261196]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found