Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

storing date into variable

by Anonymous Monk
on Nov 10, 2009 at 12:32 UTC ( #806206=perlquestion: print w/replies, xml ) Need Help??

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

folks, When executing the date command, I get something like :
Tue Nov 10 15:24:50 SAUST 2009
I am trying to store the first three outputs into variables , something like :
the day will be Tue the month will be Nov the day num will be 10
I am having hard time getting this right ,, here what I have been trying :
my $todayDate = system qq(date); ($day, $month, $dayNum, $var4, $var5) = split(" ", $todayDate); print "the day will be $day\n"; print "the month will be $month\n"; print "the day num will be $dayNum\n";
I get nothing printed , here is what it looks like :
the day will be the month will be the day num will be
Any advice plz

Replies are listed 'Best First'.
Re: storing date into variable
by Corion (Pope) on Nov 10, 2009 at 12:37 UTC

    Maybe you want to use localtime instead of running the external program?

    Consider printing $todayDate, because system does not return what you think it does.

      I just tried the OP's code and indeed it doesn't do at all what the OP seems to think it does.

      I am running on Windows Vist so that could effect it; it seems that very many of the system() calls that work on Unix and Unix-like systems don't work like (or at all) on Windows. So that might be a part of what I'm seeing.

      On windows the

      my $todayDate = system qq(date)

      ends up opening a command window and holds until you release the window (close it or exit from it) and then, when I print $todayDate I get a fairly long string that says:

      The current date is: 11/11/2009 Enter the new date: (mm-dd-yy) $todayDate

      So, as Corion noted, when it is split the various variables that the OP assigned to do not, at all, have what the OP seems to think.

      Using the Perl "localtime" or "gmtime" in a way similar to:

      my($sec,$min,$hr,$day,$month,$yr) = (localtime())[0..5];

      or,

      my($sec,$min,$hr,$day,$month,$yr) = (gmtime())[0..5];

      depending upon whether the local or gmt time is wanted, should work much better and it works on both Unix, Unix-like, and Windows systems just fine.

      Just my humble opinion.

      ack Albuquerque, NM
Re: storing date into variable
by moritz (Cardinal) on Nov 10, 2009 at 12:39 UTC

    That's because $todayDate is empty, system doesn't capture the output of the command.

    But a better way to get the current is the built-in function localtime.

    Perl 6 - links to (nearly) everything that is Perl 6.

      Curious.

      When I run the OP's code (as I noted in my response to Corion's OP response), I do get information returned from system() that gets stored into $todayDate. It's just not at all what the OP seems to think (based upon the OP's code).

      Of course as I also noted in my response to Corion, it may be because I'm working in Windows. On my windows box, system() does return a result...but maybe not on Unix or Unix-like systems. I rarely work on Unix systems so I'm not that familiar with how system() works on them.

      ack Albuquerque, NM
        I never said that system doesn't return a result (it does indeed, and it's well documented), just that it doesn't capture the output.
        Perl 6 - links to (nearly) everything that is Perl 6.
Re: storing date into variable
by JavaFan (Canon) on Nov 10, 2009 at 13:03 UTC
    I agree with the other posters that localtime is better to use. But I disagree with the implied suggestion that one should parse the output of localtime. Nor would I use localtime in list context and do some mapping from numbers to strings.

    POSIX::strftime is your friend (man POSIX).

Re: storing date into variable
by skangas (Novice) on Nov 10, 2009 at 13:55 UTC

    The other posters have said mostly everything there is to say.

    I would just like to point out that you might want to try DateTime that's available from CPAN. While localtime is a builtin, and therefore automatically available on all Perl installations everywhere, in my opinion it's a drag trying to remember the format of localtime. When it comes to dates and times, a good rule of thumb is that it's always better to use DateTime. So this means sure, you could waste some time learning localtime just for this time, and next time you need something more than showing a date you can learn DateTime. Or, you can go with DateTime right now and skip the intermediate step. Personally, I'd go with the latter.

Re: storing date into variable
by biohisham (Priest) on Nov 10, 2009 at 14:45 UTC
    Using localtime in scalar context and then splitting the scalar around spaces to get a list of (day, month, date, hour and year) values into the hash slice keys...
    use strict; use warnings; my $time_var = localtime; my %hash; my @keys = qw(day month date hour year); @hash{@keys}= split " ", $time_var; print "$_->$hash{$_}\n" for keys(%hash);
    In addition to all that is suggested, you may want to take a look at modules that can give you more control , like Time::localtime and Time::gmtime.


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: storing date into variable
by kyle (Abbot) on Nov 11, 2009 at 04:40 UTC

    If you want to get the output from a shell command, use backticks or the qx form of quotation.

    my $todayDate = qx(date); my ( $day, $month, $dayNum ) = split ' ', $todayDate; print "the day will be $day\n"; print "the month will be $month\n"; print "the day num will be $dayNum\n"; __END__ the day will be Tue the month will be Nov the day num will be 10

    As others have noted, this may not be the best way to get the date and time, but it is how to do what you were trying to do. Note that what's returned will typically have a newline at the end, and it behaves differently in list context.

    For the full scoop on qx, see perlop in the section "Quote and Quote-like Operators".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-05-13 10:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?