If you want to do it that way, use localtime's list context. You can avoid the string parsing which could change with locale1:
my @wday = qw/Monday Tuesday Wednesday Thursday Friday Saturday Sunday +/; printf "Today is %s.\n", $wday[ (localtime(time))[6] - 1 ];
See localtime for details on the returned list.
Update: A little lot more feedback.
I didn't comment on these originally as these issues are moot if you adopt my suggestion, above, but the feedback may be helpful in other situations nonetheless.
Your code:
my @date = split(" ", localtime(time)); my $day = $date[0]; my %days = ();
Your split splits the entire string, when you really only need the first three characters. There are tons of ways to do this (including split), but this is what substr is made for:
my $day = substr(localtime, 0, 3); # Sun..Sat
There is never any need to initialize %days with my %days = ();, since this is exactly equivalent to my %days;. Furthermore, since you go on to assign a new list to it in the very next statement, you may as well combine them, with my %days = (Mon => 'Monday', Tue => 'Tuesday', ...);
You can automate this even further with map:
my %days = map { substr($_,0,3) => $_ } qw/Monday Tuesday Wednesday Thursday Friday Saturday Sunday/;
(N.B.: you had Thursday abbreviated as "Thur"; shortening it to "Thu" makes it consistent with the others, further simplifying the logic).
This may not be the way I would do it, since modules like DateTime are ubiquitous and effortlessly scale beyond this simple example. But your method certainly works, so if it's what you prefer, great! The main point of this is to show one way your existing logic could be simplified. I know you're not the OP, but as you're new to the Monastery I thought you might appreciate the feedback.
_______________
1. Whoops. localtime: The format of this scalar value is not locale-dependent but built into Perl.
In reply to Re^2: Get day of week from current date
by rjt
in thread Get day of week from current date
by gavrilo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |