package ClassObject;
# $Id: ClassObject.pm,v 1.3 2001/01/03 08:20:34 tom Exp $
################################################################

use strict;
use vars qw(@ISA @EXPORT);
use Exporter;

@ISA = qw(Exporter);
@EXPORT = qw(self_or_default);

my %Q;

{
    %Q = ();
}

sub new ($)
{
    my $class = shift;
    my $self = {};

    bless $self, $class;
    return $self;
}

################################################################
sub self_or_default
{
    my $default_class = "ClassObject";
#    print STDERR "self_or_default: ", ref $_[0], "\n";
    if (ref $_[0]){
#	print STDERR "obj->func()\n";
	# $obj->func()
    } else {
	my $class;
	if ((defined $_[0]) && (!ref $_[0])){
	    if ($_[0] eq $default_class ||
	      UNIVERSAL::isa($_[0], $default_class)){
		# TestClass->func()
#		print STDERR "TestClass->func()\n";
		$class = $_[0];
		shift(@_);
	    } else {
		die "illegal class: $_[0]";
	    }
	} else {
	    # TestClass::func()
#	    print STDERR "TestClass::func('$_[0]')\n";
	    $class = $default_class;
	}
	$Q{$class} = $class->new unless $Q{$class};
	unshift(@_, $Q{$class});
#	print STDERR "class: $class\n";
#	print STDERR "\@_:", join(", ", @_), "\n";

    }
    return @_;	    
}
1;
