Question

In perl I am able to get current seconds using this sequence of commands:

my @time = ($sec,$min,$hour,$day,$mon,$year_1900,$wday,$yday,$isdst)=localtime;
print $time[0]

Is there any equivalent of this but using hashes? So one can type something like this:

print $time{"sec"} 

I have tried:

my %time= ("sec","min","hour","day","mon","year_1900","wday","yday","isdst")=localtime;
print $time{"sec"}

But it ended with following error:

Can't modify constant item in list assignment at -e line 1, near "localtime;"
Execution of -e aborted due to compilation errors.

Thanks

Was it helpful?

Solution 2

You can use hash slice

my %time;
@time{"sec","min","hour","day","mon","year_1900","wday","yday","isdst"} = localtime;
# or shorter
# @time{qw(sec min hour day mon year_1900 wday yday isdst)} = localtime;

print $time{"sec"};

OTHER TIPS

Instead of storing everything in a hash, you can store it in an object using Time::Piece:

#!/usr/bin/perl

use strict;
use warnings;

use Time::Piece;

my $t = localtime;
print $t->sec;

Time::Piece can do date math, comparisons, parsing, and output, making it more flexible than a simple hash.

There are a few standard Perl modules that can help you.

The first is Time::localtime. Thie replaces the internal localtime command with a by name methods for accessing the various parts of the time:

use strict;           # Always
use warnings;         # Always
use feature qw(say);  # An improved version of the print command

use Time::localtime

my $time = localtime; # A Time::Tm object, not your standard localtime command

say $time->sec;       # Prints the seconds

This isn't quite the hash you've requested, but you can see that it greatly improves the access to the various pieces provided by localtime in a way that's almost like a hash.

You can also see the use of say which is like print except that you don't need that pesky \n on the end (like you forgot in your example).


Taking things to the next step...

Another nice standard module is called Time::Piece. It provides even easier ways to parse time and to display it.

use strict;           # Always
use warnings;         # Always
use feature qw(say);  # An improved version of the print command

use Time::Piece

my $time =  localtime;
say $time->sec;       # Prints the seconds. Looks pretty much the same

#
# But now look!
#

say $time->ymd;       # Prints the time as YYYY-MM-DD
say $time->ymd("/");  # Prints the time as YYYY/MM/DD
say $time->mdy("/");  # Prints the time as MM/DD/YYYY
say $time->month;     # Prints the name of the month
say $time->cdate;     # Prints date and time as a string

I prefer Time::Piece because of its flexibility and the ease of initializing a date that's not the current time. If you have a string for the date/time, and you can describe it, you can easily create a new Time::Piece date/time object that can be manipulated.


One of the many things Perl programmers miss is the cornucopia of standard Perl modules that are packed into almost every Perl distribution. These modules do not require downloading or installation. They're there and available on almost any computer that has that particular Perl release.

You can use File::Copy for that missing file copy command. You can use File::Basename for that missing basename and dirname command. You can use the hundreds of modules that come with Perl to make your life easier. Take a tour of the standard Perl documentation and play around with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top