How can I check if today is Monday in Perl?
What modules needs to be installed?
Can anyone help me with an example?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With