Module::Install 和 bash 完成脚本

Module::Install and bash completion scripts

我有一个 Perl 脚本 Makefile.PL:

# Load the Module::Install bundled in ./inc/
use lib '.'; # added since from Perl 5.26 '.' is no more in @INC
use inc::Module::Install;

##############################################################################
# Define metadata (we read it from the binary)

name              'check_updates';
version_from      'check_updates';
perl_version_from 'check_updates';
all_from          'check_updates.pod';

##############################################################################
# Specific dependencies

include 'version';

my %prereqs = (
    'Carp'     => 0,
    'English'  => 0,
    'POSIX'    => 0,
    'Readonly' => 0,
    'Monitoring::Plugin' => 0,
    'Monitoring::Plugin::Threshold' => 0,
    'Monitoring::Plugin::Getopt' => 0,
);

install_script  'check_updates';

auto_install;

tests 't/*.t';
test_requires 'Test::More'       => 0;
test_requires 'File::Spec'       => 0;

# https://metacpan.org/pod/release/DAGOLDEN/CPAN-Meta-2.142690/lib/CPAN/Meta/Spec.pm#license
license 'gpl_3';

WriteMakefile(
    PREREQ_PM            => \%prereqs,
    INSTALLSCRIPT        => '/usr/lib/nagios/plugins/contrib',
    INSTALLSITESCRIPT    => '/usr/lib/nagios/plugins/contrib',
    MAN1PODS             => { 'check_updates.pod' => 'blib/man1/check_updates.1', },
    MAN3PODS             => { },
);

我还想复制一个bash-完成脚本(即check_updates.completion)到正确的目录(由pkg-config --variable=completionsdir bash-completion给出,例如/opt/local/share/bash-completion/completions

有没有办法生成 Makefile 规则来将文件复制到目录中?

pkg-config --variable=completionsdir bash-completion 的执行也可以在 Makefile.PL 生成带有硬编码规则的 Makefile 中执行。

根据Module::AutoInstall中的documentation

Starting from version 0.43, Module::AutoInstall supports modules that require a MY::postamble subroutine in their Makefile.PL. The user-defined MY::postamble, if present, is responsible for calling Module::AutoInstall::postamble and include the output in its return value.

我用这个简单的 Makefile.PL:

测试了这个
use strict;
use warnings;
use inc::Module::Install;

name           'My-Module';
all_from       'lib/My/Module.pm';
include        'Module::AutoInstall';

my %prereqs = (
    'Carp'     => 0,
    'English'  => 0,
    'POSIX'    => 0,
    'Readonly' => 0,
);
install_script 'myscript';
auto_install;

WriteMakefile(
  PREREQ_PM            => \%prereqs,
);

sub MY::postamble {
    my $dest_path = "/opt/local/share/bash-completion/completions";
    my $script_name = "check_updates.completion";
    my $str = "install::\n\t$(CP) ${script_name} ${dest_path}\n";
    return &Module::AutoInstall::postamble . $str;
}

它在这里似乎工作正常(当您 运行 sudo make install 时它会将给定的文件复制到指定的路径)