
Jasmine
Administrator
Mar 6, 2002, 12:25 PM
Post #2 of 13
(1158 views)
|
No regular syntax to use OO? Hrm. Not true, unless I'm mistaken about what you meant by that. The first thing I'd suggest is to grab Damian Conway's excellent Object Oriented Perl. For lack of time or funds, head over to [url=http://perldoc.com]perldoc.com and read [url=http://perldoc.com/perl5.6.1/pod/perlboot.html]the Beginner's OO tutorial, the more in-depth OO tutorials ([url=http://perldoc.com/perl5.6.1/pod/perltoot.html]part 1 and [url=http://perldoc.com/perl5.6.1/pod/perltootc.html]part 2), [url=http://perldoc.com/perl5.6.1/pod/perlobj.html]Perl objects, [url=http://perldoc.com/perl5.6.1/pod/perlbot.html]Perl OO tricks and examples and maybe even [url=http://perldoc.com/perl5.6.1/pod/perltie.html]Perl objects hidden behind simple variables. With that many variables, I'd throw it into its own module as a hashref and make a sub to initialize it. Something like this: [perl]use MyModule::vars; my $vars = MyModule::vars->init(); [/perl] You can also export all of the vars into the caller's namespace, but with that many vars, that's a bit of namespace clutter and invites clashing (IMHO, of course). Because I'm not so busy today, here's a relevant example of OO and maybe a little inheritance for those who may not have delved into OO yet: First, here's a tiny program that'll receive email posts (guess you'll be using procmail or some other way to alias a specific email address to this program). [perl]#!/usr/bin/perl -w # let's call this mailpost.cgi use strict; use Topic::Submit::Email; my $message = Topic::Submit::Email->new( messagetype => 'mail' ); my $success = $message->post(); if ( $success ){ # do something, if you want } else { # do stuff when successfully posted. Maybe create a sub in # Topic::Submit::Email that will email the user with the reason # for failure } [/perl] Now, here's another tiny program that'll receive posts from a website: [perl]#!/usr/bin/perl -w # let's call this sitepost.cgi use strict; use Topic::Submit::SitePost; my $message = Topic::Submit::SitePost->new( messagetype => 'site' ); my $success = $message->post(); if ( $success ){ # do stuff when successfully posted } else { die "Nope. Didn't post."; } [/perl] Not much different, huh :) The magic is in the modules... Topic::Submit Topic::Submit::SitePost Topic::Submit::Email Here's Email.pm, which you use when receiving posts by email: [perl]package Topic::Submit::Email; use Topic::Submit; our @ISA = qw/ Topic::Submit /; sub new{ my ( $class, %args ) = @_; bless{ messagetype => $args{ 'messagetype' }, }, ref( $class ) || $class; } sub post { my ( $self ) = @_; my ( $username, $subject, $message ) = _parse_email(); my $newpost = Topic::Submit->new( username => $username || 'Anonymous', subject => $subject, message => $message, ); $newpost->commit_post() or return 0; return 1; } sub _parse_email{ # subroutine to extract post information from email. } 1;[/perl] Here's SitePost.pm, which you'll use if you're receiving posts from a website: [perl]package Topic::Submit::SitePost; use CGI; use Topic::Submit; our @ISA = qw/ Topic::Submit /; my $in = CGI->new(); sub new{ my ( $class, %args ) = @_; bless{ messagetype => $args{ 'messagetype' }, username => $in->param( 'username' ) || 'Anonymous', subject => $in->param( 'subject' ), message => $in->param( 'message' ), }, ref( $class ) || $class; } sub post{ my $self = shift; my $newpost = Topic::Submit->new( username => $self->{ 'username' }, subject => $self->{ 'subject ' }, message => $self->{ 'message ' }, ); $newpost->commit_post() or return 0; return 1; } 1;[/perl] And finally, here's Submit.pm, which handles the post, regardless of whether it submitted by email or from the website: [perl]package Topic::Submit; our @ISA = qw/ Topic /; sub new{ my ( $class, %args ) = @_; bless{ username => $args{ 'username' }, subject => $args{ 'subject' }, message => $args{ 'message' }, }, ref( $class ) || $class; } sub commit_post{ my $self = shift; # commit data, confirm, whatever # print "User $self->{'username'} just Posted."; } 1;[/perl] Later on, if you wanted to add another way to post, then you can use add another subclass like Topic::Submit::Telepathy. The only thing Topic::Submit::commit_post() cares about is receiving the username, subject and message. That's the beauty of OO modules. It lets you work on the little stuff (like different ways to post a topic) while keeping the big picture (like actually committing the post) intact and not having to rewrite different implementations of committing the post for each post method. Oops. Just ran out of time. Gotta fly, but if you have questions, holler
|