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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.