=head1 NAME

CFX5::PkgLoader - on-demand package loading and error reporting

=head1 SYNOPSIS

  use CFX5::PkgLoader qw(cfx5_require);

  if ($need_something) {
    cfx5_require('CFX5::Something');
  }

=head1 DESCRIPTION

This package provides supporting code for on-demand package loading within CFX-5.  It is basically identical 

  $Id$

=cut

package CFX5::PkgLoader;

use Exporter;
@ISA = qw(Exporter);

@EXPORT_OK = qw(cfx5_require);

use strict;

#use CFX5::Output qw(:all);

=head2 PACKAGE LOADING

=head3 cfx5_require

=cut

sub cfx5_require {
  for my $pkg (@_) {
    # if the output is fully initialised, record what we're doing.  We
    # are called as part of the initialisation, though, so if that's
    # the case don't try it!
    #if (defined &CFX5::Output::is_init and CFX5::Output::is_init()) {
    #  CFX5::Output::_debug("require $pkg");
    #}
    eval "require $pkg;";
    if ($@) {
      (my $msg=$@) =~ s!^! !mg;
      eval {
        require CFX5::Output;
        CFX5::Output::internal_error("Required module $pkg could not be loaded, execution cannot continue.  The error given was:\n\n$msg");
      };
      if ($@) {
        die "Required module $pkg could not be loaded, execution cannot continue.  The error given was:\n\n$msg";
      }
    }
  }
  1;
}

1;
