Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check today is Monday in Perl ?

How can I check if today is Monday in Perl?

What modules needs to be installed?

Can anyone help me with an example?

like image 761
Monisha Avatar asked May 29 '13 09:05

Monisha


1 Answers

Simplest way would be to use localtime. It returns a list of values. The seventh of these is the weekday, starting at Sunday. Thus, Monday has the value 1. If no argument is given, it uses the current time (time), which is what you want.

if ( (localtime)[6] == 1) {
  print "Today is Monday!\n";
}

Since we only need the index 6 (seventh return value), we can put parens around localtime to force it into a list, and access the index directly from that list. We can compare that scalar value to 1.

localtime is a built-in function. No need for any additional modules, not even one included in the Perl Core. This just works out of the box.

like image 175
simbabque Avatar answered Sep 19 '22 18:09

simbabque