App-Packager

 view release on metacpan or  search on metacpan

lib/App/Packager.pm  view on Meta::CPAN

    foreach ( @INC ) {
	return "$_/$RESNAME/$file" if -e "$_/$RESNAME/$file";
    }
    undef;
}

sub U_GetResourcePath {
    return if $RESNAME eq "";
    foreach ( @INC ) {
	return "$_/$RESNAME/res" if -d "$_/$RESNAME/res";
    }
    undef;
}

#### Usually, this is all what is needed.

sub getresource {
    my ( $file ) = @_;

    my $found = App::Packager::GetUserFile($file);
    return $found if defined($found) && -e $found;
    $found = App::Packager::GetResource($file);
    return $found if defined($found) && -e $found;
    return unless $App::Packager::PACKAGED;
    return if $RESNAME eq "";

    foreach ( @INC ) {
	return "$_/$RESNAME/user/$file" if -e "$_/$RESNAME/user/$file";
	return "$_/$RESNAME/res/$file"  if -e "$_/$RESNAME/res/$file";
	return "$_/$RESNAME/$file"      if -e "$_/$RESNAME/$file";
    }

    return;
}

#### Import handling.
#
# Bij default, the getresource routine is exported, but its name
# can be changed by using ":rsc" => "alternative name".

sub import {
    my $pkg = shift;

    my @syms = ();		# symbols to import
    my $rsc = "getresource";

    while ( @_ ) {
	$_ = shift;
	if ( $_ eq ':name' ) {
	    SetResourceName(shift) if @_ > 0;
	    next;
	}
	if ( $_ eq ':rsc' ) {
	    $rsc = shift if @_ > 0;
	    next;
	}
	push( @syms, $_ );
    }

    if ( $rsc ) {
	my $pkg = (caller)[0];
	no strict 'refs';
	*{ $pkg . "::" . $rsc } = \&getresource;
    }

    # Dispatch to super.
    $pkg->export_to_level( 1, $pkg, @syms );
}

# Unknown routines are dispatched to Cava::Packager, which provides
# packaged and non-packaged functions.

our $AUTOLOAD;

sub AUTOLOAD {
    my $sub = $AUTOLOAD;
    $sub =~ s/^App\:\:Packager\:\://;

    eval { require Cava::Packager } unless $Cava::Packager::PACKAGED;
    my $can = Cava::Packager->can($sub);
    unless ( $can ) {
	require Carp;
	Carp::croak("Undefined subroutine \&$AUTOLOAD called");
    }

    no strict 'refs';
    *{'App::Packager::'.$sub} = $can;
    goto &$AUTOLOAD;
}

1;

=head1 NAME

App::Packager - Abstraction for Packagers

=head1 SYNOPSIS

App::Packager provides an abstract interface to a number of common
packagers, trying to catch as much common behaviour as possible.

The main purpose is to have uniform access to application specific
resources.

Supported packagers are PAR::Packer, Cava::Packager and unpackaged. In
the latter case, resources are looked up in @PATH, and the name of the
application package must be passed to the first C<use> of
App::Packager.

For example:

    use App::Packager qw(:name My::App);
    print "My packager is: ", App::Packager::Packager(), "\n";
    print getresource("README.txt");

=head1 EXPORT

By default, function C<getresource> is exported. It can be exported
under a different name by providing an alternative name as follows:

    use App::Packager( ':rsc' => '...alternative name...' );



( run in 2.451 seconds using v1.01-cache-2.11-cpan-2398b32b56e )