Hello, World. /cliché

I've only recently taken on the tremendous task of learning Perl (with the aide of O'Reilly's Learning Perl). I have a little background in programming as I am a Computer Engineering & Computer Science student at the Florida Institute of Technology. Actually, I was a student there. I can't quite afford it at this point, and I'm going to attempt to transfer elsewhere (UCF, USF, UF? Something with a U and an F, I'd suppose).

Well, on to something a bit more on-topic. I'm midway through Chapter 5 in Learning Perl and I felt the need to create something that the book didn't explicitly tell me to. I eventually want to build my own forums software, but this small 'Journal' or 'Diary' script will have to do for now.

#! /usr/bin/perl

print "Press CTRL-D to end journal entry.\n";
my $success = open LOG, ">>logfile";
if ( ! $success) {
die "Cannot create logfile: $!";
}

select LOG;
print "\n\n", scalar localtime(), "\n";
print <STDIN>;


I'm sort of surprised that this blog software doesn't have a specific 'code' tag. Perhaps I'm missing something?

The script above isn't very complex, and if you're reading this blog I'm sure you've already noticed that what I've done in a few lines could have been done a single line. Unlike most programmers, I prefer to write things out the long way for readability's sake. I wonder if Larry Wall would hate me for it?

Edit - Thanks for the input! The script was simplified to four simple lines:

print "Press CTRL-D to end journal entry.\n";
open my $log, '>>', 'logfile' or die "Cannot open logfile: $!";
print {$log} "\n\n", scalar localtime(), "\n";
print {$log} <STDIN>;

Also, there's an alternative to specifying {$log} each time:

select $log;
print "\n\n", scalar localtime(), "\n";
print <STDIN>;

7 Comments

Just one quick tip. The more modern and recommended way of opening files is using 3-argument open() with a lexical filehandle, like this:

my $success = open my $log, '>>', 'logfile';

Welcome!! :)


A few notes, if I may...

First of all, here's a good line from perlstyle: "Perl is designed to give you several ways to do anything, so consider picking the most readable one."

So don't be afraid to write more readable. It's recommended!


Now, to nitpick in your code:
- You should probably open to a scalar ($log) and not just a bareword (LOG).
- Try to start using "my" whenever you can to initialize variables. If you aren't there yet, don't worry about it.
- open() should best be called with three arguments: variable name, type of opening (read, write, append, etc.) and what file.

my $result = open my $log, '>>', 'logfile'


Also, since open() returns a result, you can use "or" to check stuff. This is actually the most known idiom of opening a file and checking it:
open my $log, '>>', 'logfile' or die "cannot open file: $!\n";

You could probably write more cleanly the last line as such:
print {$log} "\n\n", scalar localtime, "\n";


I hope I'm not overwhelming you here. If it scares you, don't worry - you'll end up getting to all of it at some point, so no rush. :)


Good luck with studying!

A few comments the others haven't brought up...

1) Larry has explicitly said that you can write "Baby Perl" and it's just fine.

2) UCF has a pretty good CS department, I dropped out of it at one point so I can tell you first hand it is really nice. Of the schools you mentioned it is probably also the easiest to get to from Melbourne. If you can't tell I grew up beachside, and I'm dying to get back home.

3) You should come to Perl Oasis next January (or if I get my ass organized, the next Orlando.pm meeting).

Welcome
-Chris

Other than specifying that Perl Oasis will be at the Four Points Sheraton in Orlando on Martin Luther King's Birthday Weekend (January) I have no other details about it yet. http://perloasis.org will eventually contain the details.

As for the Orlando.pm meetings, http://orlando.pm.org is the spot to keep track of.

Leave a comment

About masterrex

user-pic I blog about the dumb little things I'm doing with Perl. I am by no means an expert; if you find that I've done something wrong or in a poor fashion, please correct me! I appreciate any and all knowledge you'd care to part with :)