Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Simple Greeter

by harley_frog (Novice)
on Apr 30, 2003 at 15:14 UTC ( [id://254330]=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info harley_frog
Description: This is my very first "original" Perl script, inspired by "Hello World" only more intelligent. I've just started reading the Llama book and applied some of my new found knowledge to this script. Working towards a future version that will automatically input the current user's name, thus eliminating the prompt. Also working on a 12-hour clock version.

Check for updated code below.

#!/usr/bin/perl -w
use strict
print "Please enter your name: ";
chomp($name = <STDIN>);
@greeting = ("morning", "afternoon", "evening");
@days = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturday /
+;
@months = qw/ January February March April May June July August Septem
+ber October November December /;
$hour = (localtime(time()))[2];
$min = (localtime(time()))[1];
$day = (localtime(time()))[6]; 
$month = (localtime(time()))[4];
$mday = (localtime(time()))[3];
$year = (localtime(time()))[5] + 1900;
if ($min < 10) {
  $min = 0 . $min;
}
else {
  $min = $min;
}
$current_time = $hour.":".$min;
$current_date = $months[$month]." ".$mday.", ".$year;
if ($hour < 12) {
  print "\nGood $greeting[0], $name.\n"
}
elsif ($hour >= 17) {
  print "\nGood $greeting[2], $name.\n"
}
else {
  print "\nGood $greeting[1], $name.\n"
}
print "Today is $days[$day], $current_date.\n";
print "The time is now $current_time.\n";
Replies are listed 'Best First'.
Re: Simple Greeter (well, it's a start)
by grinder (Bishop) on Apr 30, 2003 at 21:40 UTC

    By visual inspection I can see a number of errors.

    use strict is lacking a semicolon. If you mean to use strictures then you must predeclare all your variables as either package variables (with use vars) or lexical variables (with my).

    There is a big conceptual problem with calling localtime repeatedly. What happens if you make the individual calls at the exact moment when the minute, hour, day... wraps around? Instead of getting 4:59:59, you might wind up with 5:00:59. localtime will return an array. Call it once and save the result as an array and then pick out what you want, rather than calling it repeatedly and carving out what you want with array slices.

    To zero fill a value, as you do for the minutes, it's much simpler to write $min = sprintf('%02d', $min). Saying $min = $min is what's known as a nop.

    Other than that, you're off to a good start. Keep it up, and read as much code by other people as you can.

    ____________________________________________________________
    Join the monks coming to YAPC::Europe 2003 in Paris, 23-25 July 2003.

      Thanks for the input. I did notice the missing semi-colon after  use strict last night and made the correction. (Must have deleted it while demoing it to a friend. (don't ask))

      Thanks, too, for the heads up on the  localtime. I figured there had to be a better way, but considering that I started this last Friday and I only started reading the Llama book the following day, I thought I did fairly well for a clueless newbie. ;)

      Frog

Re: Simple Greeter
by Jaap (Curate) on May 01, 2003 at 13:25 UTC
    #!/usr/bin/perl -w use strict; print "Please enter your name: "; chomp(my $name = <STDIN>); my @greeting = ("night", "morning", "afternoon", "evening"); my @days = qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday +/; my @months = qw/January February March April May June July August Sept +ember October November December/; my @currentTime = localtime(); print "\nGood " . $greeting[int($currentTime[2]/6)] . ", $name"; ...
    You can make it a lot shorter, like the code above shows part of.
      Thanks for the code. I'll print it out and study it. I figured there had to be a easier (and shorter) solution, but since I'm only just starting out, I don't know enough to make the code better (yet). I did this mostly as a learning tool; one that I'll revisit time and again.

      Frog

      Update:
      I took your advice (and grinder's) and reworked the code. I forgot to bring the disk with me today, but it is much smaller now. Thanks.

      Frog

Re: Simple Greeter
by Aristotle (Chancellor) on May 02, 2003 at 01:18 UTC
    You can assign a list of results to a list of variables all at once. printf or sprintf are the tools of choice when you have to pad stuff for presentation on screen. Try to avoid superfluous temporary variables wherever possible.
    #!/usr/bin/perl -w use strict; print "Please enter your name: "; chomp(my $name = <STDIN>); my @greeting = ("morning", "afternoon", "evening"); my @days = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturda +y /; my @months = qw/ January February March April May June July August Sep +tember October November December /; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $year += 1900; my $greeting = $hour < 12 ? 0 : $hour < 17 ? 1 : 2; print <<"EOT", $hour, $min; Good $greeting[$greeting], $name. Today is $days[$day], $months[$mon] $mday, $year. The time is now %02d:%02d. EOT
    (Untested, might have a typo or two.)

    Makeshifts last the longest.

      Thanks for the input (and code). I've only started studying Perl a week ago, and all this information is helpful, even though I may not fully understand it until later. I will most likely revisit this program several times, making changes along the way as my skills progress. But for an absolute beginner in Perl (or any programming for that matter), I'm surprised that I wrote something that actually works without having to rip out my hair. LOL

      Frog

      Update: I ran the code and noticed one typo. $days[$day] should read $days[$wday]. The time at the end didn't print out correctly, but this is well beyond my minisucle bit of Perl knowledge to decypher. I'm going to tuck this code away and look at it when I know more and can understand better. Like I said, this script was written mostly as a learning tool for me. :)

      Frog

Updated code (was Re: Simple Greeter)
by harley_frog (Novice) on May 05, 2003 at 13:40 UTC
    Update: After making some modifications (thanks to Jaap, Aristole and grinder) and one change that I made after reading through the perldoc, I know have the script much closer to what I want.

    #!/usr/bin/perl -w use strict $name = getlogin; @greeting = ("morning", "afternoon", "evening"); @days = qw/ Sunday Monday Tuesday Wednesday Thursday Friday Saturday / +; @months = qw/ January February March April May June July August Septem +ber October November December /; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $year += 1900; my $greeting = $hour < 12 ? 0 : $hour < 17 ? 1 : 2; $min = sprintf('%02d', $min); $current_time = $hour.":".$min; $current_date = $months[$mon]." ".$mday.", ".$year; if ($hour < 12) { print "\nGood $greeting[0], $name.\n" } elsif ($hour >= 17) { print "\nGood $greeting[2], $name.\n" } else { print "\nGood $greeting[1], $name.\n" } print "Today is $days[$wday], $current_date.\n"; print "The time is now $current_time.\n";

    Frog

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://254330]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found