将在Windows或Linux下运行的Perl序言

Joe*_*Fan 3 linux windows shell perl

是否有任何单一的魔术"前言"将使Perl脚本在Windows(作为批处理文件)或Linux(作为可执行文件)下运行,类似于那些使其在任何shell下工作的前导码?

Axe*_*man 7

我怀疑你可以让窗户接受一个shbang(#!).所以,如果你可以运行默认的shell(在我的情况下是bash),那么这在bash中有效:

@REM <<:END_REM
@echo off
echo This is DOS, this will only run in DOS.
perl -x -S %0 %*
goto over_nix
:END_REM
echo This is *NIX, this will only run in *NIX.
perl -x -S $0 $*

:<<__END__
#!perl
use 5.012;
use Data::Dumper; 

say Dumper( \%ENV );

__END__
@REM <<:over_nix
:over_nix
Run Code Online (Sandbox Code Playgroud)

这需要在我的NIX路径中调用可执行文件'@REM'.

echo >> ~/bin/@REM
chmod +x ~/bin/@REM
Run Code Online (Sandbox Code Playgroud)