
___X_CMD_PERL_LOADED=
___x_cmd_perl_loaded(){
    [ -n "$___X_CMD_PERL_LOADED" ]
}

___x_cmd_perl_load(){

    local installable=
    if [ "$1" = "--installable" ]; then
        installable=1
    fi

    if [ -z "$installable" ]; then
        ! ___x_cmd_perl_loaded || return 0
    else
        [ "installable" != "$___X_CMD_PERL_LOADED" ] || return
    fi

    ___x_cmd_perl_load_checkenv_core || {
        x:warn "Cannot find perl installed in this current environment"
        ___x_cmd_perl_load_env
        return
    }

    ___x_cmd_perl_load_checkenv_side || {
        # if x ui yesno "Your current perl environment is imcomplete without cpan or cpanm. Do you want to load x-cmd environment?"; then
            ___x_cmd_perl_load_env
            return
        # else
        #     x:warn "Loading abort. Exit."
        #     return 1
        # fi
    }

    if [ -z "$installable" ]; then
        ___X_CMD_PERL_LOADED=1
    else
        ___x_cmd_perl_load_checkinstallable || {
            x:warn "Current env is not installable"
            ___x_cmd_perl_load_env
            return
        }
        ___X_CMD_PERL_LOADED=installable
    fi

    # TODO: In the future, we need to check the current version is good enough.

    ___X_CMD_PERL_LOADED=1
}

___x_cmd_perl_load_env(){
    # TODO: add quiet
    x:info "Loading perl in this shell env using 'x env try perl'"
    x env try perl

    ___X_CMD_PERL_LOADED=installable

    ___x_cmd_perl___cmd(){
        command "$@"
    }
}


___x_cmd_perl_load_checkenv_core(){
    ___x_cmd_hascmd perl   || return
}

___x_cmd_perl_load_checkenv_side(){
    ___x_cmd_hascmd cpan   || return
    ___x_cmd_hascmd cpanm  || return
}

# TODO: Add yes and quiet in the future
___x_cmd_perl_load_checkenv(){
    ___x_cmd_perl_load_checkenv_core || return
    ___x_cmd_perl_load_checkenv_side || return
}

___x_cmd_perl_load_checkinstallable(){
    [ -w "$(command -v perl)" ]
}
