Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

a learning exercise - diary program

by ailie (Friar)
on Jan 26, 2001 at 23:57 UTC ( #54624=perlquestion: print w/replies, xml ) Need Help??

ailie has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks. If you'd be willing to lend me some of your time and expertise I would appreciate it.

I'm a newcomer to Perl, at the stage where I'm trying to take some of the stuff I've learned from lessons in books and put it into my own little programs. I thought that the diary program (below) would be a good place to start.

I had a few critera I wanted to satisfy with this program - I wanted the entry to be written to two files, a large diary file with all the other entries in it, and to a smaller file with just that date's entries in it. I wanted the date and time to appear before the entries in both files, and for kicks I had the weather in my hometown put into the date file.

I'd like some feedback on this program - I'm sure I have bugs, needlessly long lines, etc. Also, what else would be neat to have put into the date file for posterity? Bone::Easy, perhaps?

Thanks for your time!

#!/usr/bin/perl -w ### # The latest version of my diary program ### use strict; use POSIX 'strftime'; # declare the variables my @diary = "/path/to/diary/diary.txt"; my $filename=strftime("/path/to/diary/%m-%d-%Y.entry.txt",localtime); my ($minute, $hour, $day, $month, $year) = (localtime)[1,2,3,4,5]; # format the time properly $month++; $year += 1900; # open the files for writing/appending open FILE, ">>$filename" or die "Can't open file: $!\n"; open DIARY, ">>@diary", or die "Can't open file : $!\n"; # put the time in the diary, time and weather in the date page printf DIARY ("%02d\/%02d\/%04d %02d\:%02d \n", $month, $day, $year, $ +hour, $minute); printf FILE ("%02d\:%02d \n", $hour, $minute); weather(); # write the diary entry print "Go ahead and write. Hit ^D to end. \n"; @diary = <STDIN>; print FILE "@diary \n"; print DIARY "@diary \n"; # close the files close(FILE); close(DIARY); # the weather subroutine sub weather { use Geo::Weather; my $weather = new Geo::Weather; my $current = $weather->get_weather('07060'); print FILE "Temperature in Plainfield, NJ\: $current->{temp} degree +s\n\n"; }

Replies are listed 'Best First'.
Re: a learning exercise - diary program
by ColonelPanic (Friar) on Jan 27, 2001 at 01:42 UTC
    Nice, looks like solid coding altogether. Here are a few thoughts:

    It doesn't really matter in a simple program like this, but in general you shouldn't open files sooner than you need them, it wastes memory. You could open the file after the diary entry is entered. Or, you could open the file beforehand and use an implementation like this:
    #files are open already print "Write to diary. Type quit when finished."; while (($_=<STDIN>) != "quit\n") { print FILE $_; print DIARY $_; }
    Now you aren't reading the entire diary entry into memory, only one line at once. Like I said earlier, it is trivial in this program. But it will pay later to think about things like this. Overall, though, it looks good. I've seen plenty of code created for commercial use that is a lot worse than this.

    When's the last time you used duct tape on a duct? --Larry Wall
Re: a learning exercise - diary program
by Elgon (Curate) on Jan 27, 2001 at 01:11 UTC
    OK, I am also a mildly inexperienced Perlmonk but here are my thoughts: all to be taken with a pinch of salt:

    1. File storage: have you though about using a database such as the great MySQL instead of writing to files. This will allow you do neat things like search your diary more easily (I think).

    2. Interfaces: if you know any Perl/Tk (I don't, yet ;-) then you could use that to whip yourself up a spiffy interface. The way I would do it at the moment, before I will have learned Tk, is by reading the program as a CGI script to use your browser as an interface. (This has the added advantage of making you use or CGI:Lite, which is my second-favourite module after

    As always, constructive criticism welcome.


Re: a learning exercise - diary program
by chromatic (Archbishop) on Jan 27, 2001 at 05:17 UTC
    Things I would do slightly differently:
    • Use a separate variable for the DIARY location (reusing variables for different things can be confusing)
    • Return text from weather() instead of printing within the subroutine (do your printing all in one place)
    • Create a timestamp() subroutine to format the times nicely
    • (some time in the future) Create a separate get_diary_text() subroutine so you can add different entry methods as Elgon suggests
    Nothing scares me about what you have now. It's solid code.
      Just a guess, but there may be a misunderstanding in the reuse of *diary.
      @diary = 'path/to/diary.txt'; open(DIARY,">>@diary") or die "can't open file: $!";
      (its a good idea to do 'or die "can't open @diary: $!"' well not '@diary' but the var holding the file name in the die stmt, as in 'or die "can't open diary file $file: $!"') is odd and just sort of lucky. You're mixing the Array @diary in a scalar context, er, I think that'll just make
      $diary[$[] eq "/path/to/diary.txt";
      fortunately, putting the @diary in a quote context unrolls it as if join("", @diary) which gives you back "/path/to/dairy.txt"


($code or die): Re: a learning exercise - diary program
by $code or die (Deacon) on Jan 27, 2001 at 07:44 UTC
    Just a quick comment that's already been suggested...

    If you can use a database interface, apart from making diary searches easier, and allowing your diary to grow without a memory overhead, etc, it will allow you not to worry about multiple users. As your code stands, it's fine for a sigle user, but you could have problems if more than one instance of the script runs at the same time. I.e. it could corrupt your text file. If you run a database, it will be much easier to insert\update new diary entries.

    I know that using a database is scary at first, but once you try and find it works, you'll wonder how you managed with flat-file databases! There are some great nodes on this site that help with Database access, reply if you'd like me to mention some of my favourites.

    Which reminds me - I need to update my home node with links. I'll do that this weekend!

    $code or die
    Using perl at
    The Spiders Web

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54624]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2021-12-01 18:28 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (13 votes). Check out past polls.