package Cpanel::Easy::Utils::Perl;

# cpanel - Cpanel/Easy/Utils/Perl.pm              Copyright(c) 2014 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

use strict;
use warnings;
no warnings qw(redefine);
use Cpanel::SafeRun::Errors ();

our $PERLINSTALL = '/usr/local/cpanel/scripts/perlinstaller';

# Wrapper around cPanel/WHM /usr/local/cpanel/scripts/perlinstaller.  As of
# this writing, perlinstaller doesn't properly detect if a module failed to
# load.  The only real way is to examine the STDOUT output.  That's highly
# error proned, so this is here to execute the installation.. then check if
# the resulting module was installed.
sub perlinstall {
    my $self    = shift;
    my @install = @_;
    my @ret     = ( 1, 'Ok' );
    my @failed;

    # 1. Install Perl modules
    {
        # Case 62973: Get around /usr/local/cpanel/scripts/perlinstaller bug
        # using wrong perl path
        local $ENV{'PATH'} = "/usr/bin:$ENV{'PATH'}";
        Cpanel::SafeRun::Errors::saferunnoerror( $PERLINSTALL, @install );
    }

    # 2. Verify all modules installed
    for my $module (@install) {
        Cpanel::SafeRun::Errors::saferunnoerror( '/usr/bin/perl', "-M$module", '-e', 1 );
        push @failed, $module if ( ( $? >> 8 ) != 0 );
    }

    @ret = ( 0, q{Failed to install CPAN Perl module(s)}, @failed ) if @failed;

    return @ret;
}

1;

