Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

The day of today

by gtrain (Novice)
on Mar 07, 2008 at 12:11 UTC ( #672746=perlquestion: print w/ replies, xml ) Need Help??
gtrain has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I'm writing a script that requires me to find out what day it is today (ie. 1-31). I have it working with the following code I sourced from the web, but there is so much stuff around dates, I haven't found anything else that is shorter:
==================================== #!/usr/local/bin/perl use strict; ### Create variables to split date to get what day of the month it is my $second; my $minute; my $hour; my $dayOfMonth; my $month; my $yearOffset; my $dayOfWeek; my $dayOfYear; my $daylightSavings; ### Split values into variables ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek +, $dayOfYear, $daylightSavings) = localtime(); print "Today is day $dayOfMonth."; ====================================
Is there a simpler/neater/cleaner way to get this? I know if I got rid of "use strict" it could be much shorter, but at work all scripts must have this. If anyone could point me in the right direction on this one it would be appreciated. Cheers!! gtrain

Comment on The day of today
Download Code
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

      This example confuses the issue. One might conclude that the + (plus) is necessary, when in fact it's not. It's only there to workaround print's brain-damage. It could be written less ambiguously as:

      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

        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

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 ;-)
Re: The day of today
by Erez (Curate) 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

    Software speaks in tongues of man.
    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphores.

      print "Today is day @localtime[3].";

      Doesn't your perl warn like

      Scalar value @localtime[3] better written as $localtime[3]

      ? You should do it ;-)

      -- Frank

        See? That's why you need to use strict and warnings :P

        Software speaks in tongues of man.
        Stop saying 'script'. Stop saying 'line-noise'.
        We have nothing to lose but our metaphores.

      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

Re: The day of today
by Corion (Pope) on Mar 07, 2008 at 12:30 UTC

    You can also declare your variables when assigning them:

    use strict; my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfW +eek , $dayOfYear, $daylightSavings) = localtime();
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;
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;
      Thanks all .... that looks much nicer to just specify the nth item in the array which localtime() has been sent to. Cheers!! gtrain

        If you take the route of specifying the offset, I would recommend using a constant instead of a magic number. It makes thins a tad easier to read.

        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.


        TGI says moo

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";
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.";
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");
Re: The day of today
by sankarraman (Acolyte) on Mar 07, 2008 at 19:43 UTC
    Use Data::Calc Module
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

Re: The day of today
by jeepj (Scribe) on Mar 10, 2008 at 12:59 UTC

    you could also use strftime from POSIX module

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://672746]
Approved by moritz
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-08-22 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (160 votes), past polls