Re: The day of today
by moritz (Cardinal) on Mar 07, 2008 at 12:19 UTC
|
My favourite module for the job is Date::Simple, as long as you only have to deal with date, not time. It has a very simple interface, you'll like it.
If you don't want a module, can get rid of the line noise with this trick:
my ($second, $minute, $hour, $dayOfMonth, $month,
$yearOffset, $dayOfWeek, $dayOfYear, $isDST) = localtime();
$year += 1900; # get absolute year
$month++; # you'd expect the January to be the first
# month, not the 0th ;-)
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by haoess (Curate) on Mar 07, 2008 at 12:19 UTC
|
perl -le 'print +(localtime)[3]'
Runs under strict too.
-- Frank | [reply] [Watch: Dir/Any] [d/l] |
|
my $dayOfMonth = (localtime)[3];
print "Today is day $dayOfMonth.";
Note to gtrain: this is technically known as an array slice. You are taking the array of values returned by localtime and slicing out only the fourth (offset 3) element).
• another intruder with the mooring in the heart of the Perl
| [reply] [Watch: Dir/Any] [d/l] |
|
my $dayOfMonth = (localtime)[3];
In Perl terminology this is not an array slice, a slice
would contrain contain more than one element . An array slice expresses
a list, not a scalar. An array slice expresses a list, not an array.
Be well,
rir | [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by Erez (Priest) on Mar 07, 2008 at 12:25 UTC
|
I know if I got rid of "use strict" it could be much shorter, but at work all scripts must have this.
and for a very good reason. As for your code:
#!/usr/local/bin/perl
use strict;
use warnings;
my @localtime = localtime();
print "Today is day $localtime[3].";
UPDATE: changed @localtime to $localtime, ++haoess
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
|
If you are happy to dispense with the array you can do this.
use strict;
use warnings;
print "today is day @{ [ ( localtime )[3] ] }.";
Cheers, JohnGG | [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by Corion (Patriarch) on Mar 07, 2008 at 12:30 UTC
|
use strict;
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfW
+eek
, $dayOfYear, $daylightSavings) = localtime();
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by olus (Curate) on Mar 07, 2008 at 12:41 UTC
|
use strict;
my ($day, $month) = ( localtime() )[3..4];
$month++;
print $month.'-'.$day;
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by Herkum (Parson) on Mar 07, 2008 at 18:55 UTC
|
Use modules! In this case I recommend DateTime,
use DateTime;
my $dt = DateTime->now();
print "Month is " . $dt->month_name . "\n";
print "The day is " . $dt->day . "\n";
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by dwm042 (Priest) on Mar 07, 2008 at 19:08 UTC
|
There are a lot of good ways to handle dates.
Example with Date::Calc
use Date::Calc;
my ( $year, $month, $day ) = Today();
Example with Date::Manip
use Date::Manip;
print UnixDate("today","It is %b %e, %Y");
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: The day of today
by Anonymous Monk on Mar 07, 2008 at 16:34 UTC
|
when declaring multiple variables on one line, you should put them in parantheses.
my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime; | [reply] [Watch: Dir/Any] [d/l] |
|
Thanks all .... that looks much nicer to just specify the nth item in the array which localtime() has been sent to.
Cheers!!
gtrain
| [reply] [Watch: Dir/Any] |
|
use constant DAY_OF_MONTH => 3;
my $day = (localtime)[DAY_OF_MONTH];
print "Day is @{ [ ( localtime )[DAY_OF_MONTH] ] }\n";
While I like the hackishness of the @{[()[]]} approach to forcing interpolation of executing code, I think it's too unweildy--it's hard to type and hard to read. If you really want to go this route, check out Interpolation--an interesting module that started out as a joke.
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by jflevi (Beadle) on Mar 09, 2008 at 21:48 UTC
|
I do a lot of "date" stuff; the good Monks here recommended Date::Simple module; it is easy to use; even I got it to work the first time !!!Give it a try. Good luck !
Life is tough, it's tougher when you are dumb... jfl
| [reply] [Watch: Dir/Any] |
Re: The day of today
by jeepj (Scribe) on Mar 10, 2008 at 12:59 UTC
|
use POSIX qw (strftime);
my $now_string = strftime "%H:%M:%S %a %d/%m/%Y", localtime;
This was described already in this node strftime reference for Win32. | [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by nbartusi (Scribe) on Mar 07, 2008 at 19:05 UTC
|
While not the best way here is yet another way.
use strict;
use warnings;
my (undef, undef, undef, $dayOfMonth, undef, undef, undef, undef, unde
+f) = localtime();
print "Today is day $dayOfMonth.";
| [reply] [Watch: Dir/Any] [d/l] |
Re: The day of today
by sankarraman (Acolyte) on Mar 07, 2008 at 19:43 UTC
|
| [reply] [Watch: Dir/Any] |