February 28, 2021
Hot Topics:

Dates and Times

  • By Joe Burns
  • Send Email »
  • More Articles »

Print It

These three lines print the date and time you saw in the example:

print "Today is $thisday, $thismonth, $mday, 200";
print $year-100;
print ", $thishour:$min:$sec ";
The first line used the array returns "$thisday" and "$thismonth". The day of the month, "$mday", returns correctly. It doesn't need an array.

That year's a pesky thing though. The variable "$year" returns 100. It's a Y2K thing believe it or don't. I tried setting up an array, but that would mean I would need 99 aray items before I got to what I wanted. No thanks. I just did some simple math.

I only want a single number return. That's why I have "200" waiting there. I'm just going to add the last digit later.

The next line of the code gets that last digit for me. I just took the $year and took away 100. That's zero. Add that zero to the end of the 200 and you get 2000.

We'll get into math inthe next primer, but real quick take a look at that line I just described. I do not want text returned from that line. I want the result of that math. To get a result rather than a simple printing, lose the double quotes. That's all.

Getting back to printing the time, the next line uses the "$thishour" array return and the "$min" and "$sec" round it out with colons between each return.

Ta da! You have the time.

The next little blip of PERL looks at the return from "$hour" . Get that? It uses the return from "$hour". Not the "$thishour" return.

if($hour > 11 && $hour < 18)
{print " PM. Good Afternoon.";}
elsif($hour > 17 && &hour < 24)
{print "PM. Good Evening.";}
{print "AM. Good Morning.";}

First we check to see if the return from "$hour" is greater than eleven AND (&&) less than 18. If so, print "Good Afternoon".

If the return is greater than 17 but less than 24, print "Good Evening". Otherwise, print "Good Morning".

I wrap it up then by sticking in a couple of returns shoing day of the week and day of the year just to show you the returns, nothing more.

I'll bet you can get this one. Set up a branch that acts as a reminder. Alter the script so that if it is past 6:30PM, a line will print to the page that it's dinner time and the user should go and eat.

Primer Eight Assignment

Here's a Possible Answer (This will open in a new window)

Page 2 of 2

This article was originally published on May 8, 2000

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date