
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

package Common::Plugins::LoadRun;

=encoding utf8

=head1 NAME

Common::Plugins::LoadRun

=head1 DESCRIPTION

Load the module dynamically and run the requested function.

=cut

use cPstrict;
use Cpanel::Exception ();

=head1 FUNCTIONS

=head2 run_from_module($module, $method, @args)

This function finds a module, loads it, and runs a command on it if it exists.

=head3 ARGUMENTS

=over

=item module - string

Full module name to load.

=item method - string

Required. Functions to attempt to run.

=item args - arrayref

Optional. Arguments to pass to the function.

=back

=cut

sub run_from_module ( $module, $method, @args ) {

    _load_module($module);

    my $method_ref = $module->can($method);
    if ($method_ref) {

        # run the method if present
        return $method_ref->(@args);
    }

    return;
}

sub _load_module ($module) {
    local $@;
    eval "require $module;";    ## no critic qw(BuiltinFunctions::ProhibitStringyEval)
    if ( my $exception = $@ ) {
        die Cpanel::Exception::create( 'InvalidParameter', "Could not locate the module '$module'." );
    }

    return 1;
}

1;
