Hello,

Hello, I come from a Computer Science background have have done a small amount of programming in my time (Mostly C, Pascal, Fortran, ADA, etc..) A couple of years ago I took a Perl class from Sun to help with some utilities I was writing and have loved it ever since!

I recently read Randal L. Schwartz's article in Linux Magazine regarding a "repackaging of data" challenge that was put forth by the folks here at the Perl Monestary. I took Randal's code and modified it to throw the information into a Java "Ticker tape" applet.

I have realized over the last couple years that there are quite a few different ways to do things in perl (to say the least). I was wondering if there were any kind souls in attendence that would like to look over my code and offer some positive feedback / criticizm. I would like to pick up on good technique and possibly learn some new tricks in the process. Here's the Code
#!/usr/bin/perl -w use strict; $|++; use LWP::Simple qw(mirror); use CGI qw(:all -no_debug col thead tbody); use HTML_TAGS; use Getopt::Long; my $CNN_URL = "http://headlinenews.cnn.com/QUICKNEWS/virtual/swf.he +adline.txt"; my $CNN_CACHE = "contest.cnn-cache"; # flat file my $DB_MEMORY = "contest.memory"; # dbmopen { my $s = mirror($CNN_URL, $CNN_CACHE); last if $s == 200; # we got new data last if $s == 304; # no new data, but we have to expire things die "status is $s, aborting\n"; } GetOptions( "refresh=i" => \ (my $REFRESH = 10), # meta refresh ti +me in minutes "output=s" => \ (my $OUTPUT = "/usr/local/apache/htdocs/ +headlines.html"), # output file "expire=i"=> \(my$EXPIRE= 15), # expire time in +minutes (Originally 1440) "clear!" => \ (my $CLEAR = 0), # clear the cache "<>" => sub { $Getopt::Long::error++; warn "Unknown arg: + $_[0]\n" }, ) or die "see code for usage\n"; dbmopen(my %DB, $DB_MEMORY, 0644) or die "Cannot dbmopen $DB_MEMORY: + $!"; open STDIN, $CNN_CACHE or die "Cannot open $CNN_CACHE: $!"; open STDOUT, ">$OUTPUT" or die "Cannot create $OUTPUT~: $!"; %DB=()if $CLEAR; $CGI::Q = CGI->new(\*STDIN) or die "Cannot parse $CNN_CACHE\n"; for (my $i = 1; my $headline = param("headline$i"); $i++) { my $state = param("state$i"); my $key = "$state\n$headline"; if (defined $DB{$key}) { # just update modtime $DB{$key} =~ s/\s\d+/" " . time/e; } else { # add the entry $DB{$key} = time . " " . time; } } for my $key (keys %DB) { delete $DB{$key} if $DB{$key} =~/\s(\d+)/ and $1 < time - $EXPIRE +* 60; } print start_html(-title => "Thunderheart -- Brought to you in Ronovis +ion!", head => meta({-http_equiv => 'refresh', -content => $REFRESH*60})); print <<'HTML'; <BODY BGCOLOR=#FFFFFF> <TABLE CELLPADDING=3 width="100%" BGCOLOR=4040A0> <TR><TH class="tit"> <FONT SIZE="+2" class="tit" COLOR=D0F0FF>Thunderheart</FONT> </TH></TR></TABLE> <CENTER> <CENTER><FONT SIZE=+1 COLOR=000000>Current Headlines from <A HREF=http://www.cnn.com>CNN.com</A></FONT></CENTER> <TD> <applet codebase="./" code="aTicker.class" archive="aTicker.jar" width +=880 height=20 MAYSCRIPT> <param name="_file" value="./mess.txt"> <param name="file" value="s"> <param name="cSep" value=";"> <param name="speed" value="2"> <param name="delay" value="30"> <param name="local" value="true"> <param name="bgcolor" value="13693183"> <param name="Font1" value="Verdana, 14, 0, 0"> <param name="Font2" value="Verdana, 14, 1, 1220"> HTML my $count = 0; while ((my $key, my $value) = each(%DB)) { (my $title, my $headline) = split(/\n/,$key); print "<param name=\"s$count\" value=\"...$headline...\ ; http://w +ww.cnn.com ; _load\">\n"; $count++; } print <<'APPLET'; </applet> APPLET print end_html; close STDOUT; exit 0; sub escapeHTMLnobreak { local $_ = escapeHTML("@_"); s/ / /g; $_; }
Any "positive" criticism would be greatly appreciated.

Thanks,
Rostiguy@ostiguynet.com

In reply to Code Critique? by rostiguy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.