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

Hello all. I am still very new to perl, slowly working my way through learning/programming perl which i find are two great books.

But I'm finding it hard to get off the ground with things to code, I have made a few simple apps like calulators and nslookup tools. So I picked up a CGI book as I run a server and thought it would be a good place to start coding something practical.

My question is I made this to start out with and what I don't understand why if all the counter code(within comments) is placed at the bottom the whole page wont display?

#!/usr/bin/perl -w print "Content-type: text/html\n\n"; $code = "index.html"; $counter = "counter.txt"; # countercode open(COUNTER, "< $counter") or die "$_"; $hits = <COUNTER>; close(COUNTER); # countercode open(PAGE, "< $code") or die "$_"; while(<PAGE>) { chomp($_); print "$_\n"; } close(PAGE); print "<CENTER>Hits: $hits</CENTER>"; print "<CENTER>Process ID: $$ Time: $^T</CENTER><BR>"; print "</HTML>"; #countercode open(COUNTER, "> $counter") or die "$_"; $hits++; print COUNTER "$hits"; close(COUNTER); #countercode
So I broke it up and It all comes out fine... also this seems very long winded, if anyone has any comments I would very much like to hear them. Also if anyone has any ideas for good apps/projects to get started with.

Replies are listed 'Best First'.
Re: Understanding ::
by bobf (Monsignor) on Jul 26, 2006 at 01:14 UTC

    Welcome to PerlMonks!

    First, thanks for making the effort to make your code block legible, but please note that <pre> tags are discouraged here. If you want something displayed in fixed-width font, please use <code> tags. Writeup Formatting Tips and Perl Monks Approved HTML tags are good references.

    Here are some general observations that may help you with this and future programs.

    • use strict; It will help you avoid bugs.
    • Limit the scope of your variables. Adding strictures and declaring all of your variables with "my" is a good start, but it doesn't help as much if they are all in file scope (not in blocks).
    • When you die, you probably want $!, not $_.
    • Kudos on checking the return value of open! The 3-argument form of open with a lexically-scoped filehandle might be a better way to go: open( my $fh, '<', $filename ) or die $!;
    • Note that you don't need to quote variables for things like die $!; and print $hits;
    • When you read the $code file you chomp each line, but then you add a newline right back on when you print it. Unless there is code between those lines that you aren't showing us, you can skip both steps.
    • Instead of printing out the HTML, consider using a module (like CGI) to do it for you. It is much more robust and it will clean up your code. It will also help you avoid errors like forgetting to put in a starting <HTML> tag (although it may be in the $code file). :-)

    HTH. Please don't be discouraged by these comments. We all learned the basics at one point. :-)

      Forgetting to put in a starting <html> tag is not an error. Tags <html> and </html> are optional (although <!DOCTYPE> is required).

      Update: Oh I see. He used </html> without <html>. Ignore this post.

Re: Understanding ::
by GrandFather (Saint) on Jul 26, 2006 at 01:10 UTC

    If you really mean that when you put all the counter code at the end of the script, then it may be because $hits is undefined when used in the print statement.

    In general it is strongly recommended that you put:

    use strict; use warnings;

    at the start of your script. (The -w does much the same as use warnings;.)


    DWIM is Perl's answer to Gödel
Re: Understanding ::
by planetscape (Chancellor) on Jul 26, 2006 at 06:07 UTC

    In regard to "good apps/projects to get started with"... I have compiled a list of such ideas here:

    Re: Real Life Perl Exercises

    HTH,

    planetscape
Re: Understanding ::
by bart (Canon) on Jul 27, 2006 at 01:45 UTC
    I find it surprising that nobody slapped your wrist about this yet... but the way you implemented your hit counter, is a recipe for disaster. If you expect there's even the tiniest slight chance that more than one browser might be retrieving the same page at the same time, then you need to use file locking.

    Still, it's not trivial to do it right, but you can do worse than looking in the official perlfaq: I still don't get locking. I just want to increment the number in the file. How can I do this?. Try not to mind the bigotry in the text.