I was kicking around university the other day when I ran into a guy who was signing up to have dictionary.coms word of the day emailed to him every day. This seemed like a very wasteful cluttering of the inbox to me and I thought this could be achieved a lot better by having a Gtk/perl script pop up the info every day.. The other night I got bored and nocked up a script. I am but a newcomer to the holy temple so please monks be hard of me and tell me how I could do this better... Here is what I came up with:
#!/usr/bin/perl # # Word Of The Day Catching Script... # (V0.1) # Joe Higton # # And hey I was half asleep when I wrote this.... # # Gets the word of the day from www.dictionary.com # # use Gtk; use HTTP::Lite; init Gtk; sub msbox{ # Display some info. Nicely. Ish. my ($title, $message) = @_; my $scroller = new Gtk::ScrolledWindow; my $window = new Gtk::Window; my $label = new Gtk::Label; my $button = new Gtk::Button("OK"); my $pack = new Gtk::Packer; $window->set_title($title); $window->set_default_size(400,340); $window->signal_connect( "destroy" => \&Gtk::main_quit); $button->signal_connect( "clicked" => \&Gtk::main_quit); $label->set_justify(left); $label->set_text($message); $scroller->add_with_viewport($label); $pack->add_defaults($scroller,-top,-center,[-fill_x,-fill_y,-expan +d]); $pack->add_defaults($button,-bottom,-center,[-fill_x,-fill_y]); $window->add($pack); $window->show_all(); } $http = new HTTP::Lite; $req = $http->request("http://www.dictionary.com/wordoftheday/") or die "Couldn't retrieve the Word Of The Day."; # Ok grab the important bit of the web page. Handily kept between two +comments. $http->body() =~ /<!-- Begin content -->(.*)<!--End content-->/s; $wod = $1; # Capture the actual word its self from the cunning comment.. $wod =~ /<!-- WOTD="(.*)" -->/; $theword =$1; #Kill that HTML! I actually stole this regex off perlmonks # because I was too bloody tired to think of one of my own at the #time.. $wod =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs; #Fire all that off into a nice window.. msbox("The Word Of The Day IS: $theword",$wod); Gtk->main();
"blue berry muffins are not just a snack.... ..they are a way of life"

Edit: chipmunk 2001-06-21

Replies are listed 'Best First'.
Re: Dictionary.com (PLEASE critisise...)
by Mission (Hermit) on Jun 22, 2001 at 00:47 UTC
    Draxil, I tried to run your code, but it appears (after searching CPAN) that Gtk.pm is only availble for Unix (I'm on Win) so I couldn't test your code. I'll leave it up to you to determine if it works correctly. So I'll list some suggestions instead just from browsing through your code.
    • You should use #!/usr/bin/perl -w to turn on warnings. This is a comment that many monks suggest. It helps in debugging, so it's always a first step.
    • You should use strict; which will make sure that you are using 'clean' Perl code. (Again, this is something that almost every monk will tell you is necessary/manditory.) You will probably have to add a lot of 'my' declarations to your variables (ie: my $http= new HTTP::Lite;) to elminate some errors.
    • I'm not so sure about this point, but I know that I personally wouldn't use the variable $pack since pack is a function of Perl and described in Perlfunc. I guess I'd be leary of using such a variable... again this is a personal choice.
    • When you use your or die statement, make sure you include in your error message $! to include the system error messages, in addition to your message (ie: or die "Couldn't retrieve the Word Of The Day.$!";.)
    • I like the fact that you commented your code. It's easy to follow and the logic makes sense. I think the code is really good for being 'half asleep.'
    Please keep posting code and participating at Perl Monks. I learned a thing or two from your code just by doing a read through. If you have questions, post them, and if you have some help for someone, feel free to participate Draxil. Welcome to Perl Monks.

    - Mission
    "Heck I don't know how to do it either, but do you think that's going to stop me?!!"
      Gtk.pm is only available for *nix because Gtk.pm is a perl interface to GTK+'s (The Gimp Toolkit) widget set. It is the underlying power of the Gnome desktop environment. There is a native win32 port of Gimp/GTK+ available here. I don't know how successful you'd be if you tried to rig it up so that it would work with Gtk.pm, though.

      --Psi
      print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");

      Hey thanks!

      I have taken a while to write this reply because I have been trying out your advise on some more perl scripts ;) .

      It's a shame no-one has done bindings for Gtk for windows yet.. I might just use Tk for GUI stuff from now on so I can pass it on to Windows users. Soon I will build up confidence to post some non half asleep code.. All with use strict and warnings enabled :P


      DrAxIl

      "blue berry muffins are not just a snack.... ..they are a way of life"

Re: Dictionary.com (PLEASE critisise...)
by Hero Zzyzzx (Curate) on Jun 22, 2001 at 05:16 UTC

    Very nice, Draxil. I may modify this (of course, by adding strict) and add it to a cron job for my box at work. . .Just for kicks. This makes me want to play with HTTP::Lite a little.

Re: Dictionary.com (PLEASE critisise...)
by John M. Dlugosz (Monsignor) on Jun 22, 2001 at 23:43 UTC
    I couldn't try it because there is no Gtk for ActiveState Perl that I could find (regular TK works fine, though). But I noticed the lack of any proxy lines... in my own recient posts, I found that the proxy needs attention and doesn't just read out of env variables without being told.

    —John