package Course::Generic; use Carp; use strict; use CGI; use vars qw( $VERSION $AUTOLOAD); $VERSION = '0.01'; # Preloaded methods go here. my $dbh; my $cgi = new CGI(''); # ripped from perltoot (well, plus some from perlbot). # Thanks, Tom! sub AUTOLOAD { # # DESTROY messages should never be propagated. return if $AUTOLOAD =~ /::DESTROY$/; my $self = shift; my $type = ref($self) or croak __PACKAGE__.": 'self' ($self) is not an object (I was tryting to find a $AUTOLOAD method for it) "; my $name = $AUTOLOAD; $name =~ s/.*://; # strip fully-qualified portion if (exists $self->{_autoguys}->{$name} ) { if (@_) { return $self->{$name} = shift; } else { return $self->{$name}; } } elsif (exists $self->{_from_parent}->{$name} ) { croak __PACKAGE__.": you have to have a _parent defined to use _from_parent magic" unless defined $self->{_parent}; # we might want question_id method called against # us to return the id method called against a parent my $proxy = $self->{_from_parent}->{$name}; if (defined $proxy) { return $self->{_parent}->$proxy(@_); } return $self->{_parent}->$name(@_); } else { croak __PACKAGE__.": Can't access <$name> field in class <$type>. You probably either mis-typed the name of the method, or forgot to put an entry for that field in the <$type> object's _autoguys hash."; } } # can be called as either a class method or an object method sub dbh { my $self = shift; if (ref $self) { if (@_) {my $foo = shift; $self->{dbh} = $foo }; # in case it hasn't been set, give the global # TODO this should be looked at, I'm sure return $self->{dbh} || $dbh; } else { if (@_) { $dbh = shift}; return $dbh; } } sub cgi { # maybe this should look like dbh() later. return $cgi; } sub cgi_save { my $self = shift; my $init = shift; my $cgi = $init->{cgi}; my $filename = $init->{filename}; open SAVE, ">$filename" or croak "no dice on open >$filename in ".__PACKAGE__."\n"; $cgi->save(\*SAVE); close SAVE; } # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__ =head1 NAME Course::Generic - Superclass for classes in the Course::* hierarchy =head1 SYNOPSIS use Course::Generic; @ISA = qw/ Course::Generic /; =head1 DESCRIPTION This serves mainly as a superclass for other stuff in the Course hierarchy. It defines an AUTOLOAD method as well as a cgi() and dbh() method. =item AUTOLOAD The AUTOLOAD method proxies for setting and getting the values of simple data members. It checks the object for a hash reference called "_autoguys" and checks to see if the autoloaded method is listed there. If so, it assigns any passed in value to the data member with the same name as the autoloaded method, and returns the data member. If it doesn't see the method name in "_autoguys", it looks for it in the "_from_parent" hash ref, and if it sees it there it just calls that method (with any passed-in arguments) on whatever is stored at "_parent" in the object. If it doesn't find the method name in either the "_autoguys" or "_from_parent" hash, it gives up and croaks. =item dbh Sets a database handle (if called with arguments), otherwise returns a database handle. It is assumed that in normal circumstances all modules will want to be accessing the database through the same handle. =item cgi This method simply shares a CGI.pm object (created via C) which is intended for use as a tag-generator, etc. It is not intended to be used as a means of getting and globally sharing information from a CGI form, which is why it is created with an explicitly empty parameter list. =head1 AUTHOR Michael South (msouth@fulcrum.org). =head1 SEE ALSO perl(1). =cut