Quizzer::AutoSelect - automatic FrontEnd selection library.


Quizzer documentation Contained in the Quizzer distribution.

Index


Code Index:

NAME

Top

Quizzer::AutoSelect -- automatic FrontEnd selection library.

DESCRIPTION

Top

This library makes it easy to create FrontEnd and ConfModule objects. It starts with the desired type of object, and tries to make it. If that fails, it progressivly falls back to other types.

METHODS

Top

frontend

Creates and returns a FrontEnd object.

AUTHOR

Top

Joey Hess <joey@kitenet.net>


Quizzer documentation Contained in the Quizzer distribution.
#!/usr/bin/perl -w

package Quizzer::AutoSelect;
use strict;
use Quizzer::Config;
use Quizzer::Log ':all';

my $VERSION='0.01';

my %fallback=(
	# preferred frontend		# fall back to
	'Web'			=>	'Gtk',
	'Dialog'		=>	'Text',
	'Gtk'			=>	'Dialog',
	'Text'			=>	'Dialog',
);

my $frontend;
my $type;

sub frontend {
	my $script=shift;

	$type=Quizzer::Config::frontend() unless $type;

	my %seen;
	while ($type ne '') {
		debug 1, "trying frontend $type" ;
		$frontend=eval qq{
						use Quizzer::FrontEnd::$type;
						Quizzer::FrontEnd::$type->new();
				};
		last if defined $frontend;
		
		warn "failed to initialize $type frontend";
		debug 1, "(Error: $@)";

		# Only try each type once to prevent loops.
		$seen{$type}=1;
		$type=$fallback{$type};
		last if $seen{$type};

		warn "falling back to $type frontend" if $type ne '';
	}
	
	if (! defined $frontend) {
		# Fallback to noninteractive as a last resort.
		$frontend=eval qq{
						use Quizzer::FrontEnd::Noninteractive;
						Quizzer::FrontEnd::Noninteractive->new();
				};
		die "Unable to start a frontend: $@" unless defined $frontend;
	}

	return $frontend;
}

1