开发者

How to change a variable string in scripts/modules when using ExtUtils::MakeMaker

开发者 https://www.devze.com 2023-02-19 05:47 出处:网络
I have some modules and scripts in an application written in perl. For its distribution I am using ExtUtils::MakeMaker.

I have some modules and scripts in an application written in perl. For its distribution I am using ExtUtils::MakeMaker.

The modules and scripts' target OS is Linux. The point is than I expect the final users will install the modules/scripts in non standard directories and maybe in their own directory, then they install like:

perl Makefile.PL PREFIX='/YOUR/DIRECTORY'
make
make test
make install

ExtUtils::MakeMaker solves the problem when the modules/scritps instalation directory are not standard and by PREFIX variable.

How can I copy or replace the PREFIX string' value into the scripts? for example, instead of hard code:

use lib '/YOUR/DIRECTORY';

I would like to replace it for a template like:

use lib '%%PREFIX%%';

where %%PREFIX%% is replaced in "compiling" time, and without set an environment variable ($EN开发者_JS百科V{'PREFIX'})

Thank for your answers!!


The thing to use is the PM_FILTER:

if (!($^O eq 'MSWin32' || $Config{'ccflags'} =~ /-D_?WIN32_?/)) {
    # doesn't work on windows: no sed
    $opts{'PM_FILTER'} = 'sed -e "s|/usr/share|$(PREFIX)/share|"';
}

Which works on linux and pretty much everything but windows (as you can see by the comments).

[Updated to include more usage detail:]

use ExtUtils::MakeMaker;

%opts = (
     'NAME'         => 'myModulesAndScripts',
     # ...
    );

if (!($^O eq 'MSWin32' || $Config{'ccflags'} =~ /-D_?WIN32_?/)) {
    # doesn't work on windows: no sed
    $opts{'PM_FILTER'} = 'sed -e "s|/usr/share|$(PREFIX)/share|"';
}

WriteMakefile(%opts);

(and as is pointed out the comments, you can try to call perl itself in the PM_FILTER so you don't need the windows check; in my example above, however, I found instances where perl wasn't in the exec path and the existing PREFIX path of /usr was already ok... You can define the PM_FILTER directly in the normal WriteMakefile argument list if you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消