将在Windows或Linux下运行的Perl序言
是否有任何一个魔法“序言”可以使Perl脚本在Windows(作为批处理文件)或Linux(作为可执行文件)下运行,类似于使其在任何shell下工作的序言?您的意思是像ActiveState Perl附带的这些包装器一样将在Windows或Linux下运行的Perl序言,windows,linux,perl,shell,Windows,Linux,Perl,Shell,是否有任何一个魔法“序言”可以使Perl脚本在Windows(作为批处理文件)或Linux(作为可执行文件)下运行,类似于使其在任何shell下工作的序言?您的意思是像ActiveState Perl附带的这些包装器一样 @rem = '--*-Perl-*-- @echo off if "%OS%" == "Windows_NT" goto WinNT perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl :WinNT perl -
@rem = '--*-Perl-*--
@echo off
if "%OS%" == "Windows_NT" goto WinNT
perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofperl
:WinNT
perl -x -S %0 %*
if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
if %errorlevel% == 9009 echo You do not have Perl in your PATH.
if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
goto endofperl
@rem ';
#!perl
#line 15
eval 'exec Z:\Software\Perl\5.8.8\bin\perl.exe -S $0 ${1+"$@"}'
if $running_under_some_shell;
#!/usr/bin/perl
# $Id: cpan,v 1.3 2002/08/30 08:55:15 k Exp $
use strict;
=head1 NAME
cpan - easily interact with CPAN from the command line
脚本以以下行结束:
1;
__END__
:endofperl
虽然我不确定您是否真的需要在末尾使用BAT标记“:endofperl”或在顶部使用“goto endofperl”(可以使用“exit”?)。我怀疑您是否可以让windows接受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
仅供参考,我花了一分钟的时间才弄明白如何使用它,但您希望在两行之间插入perl程序。此外,只要路径中有当前目录,就可以将@REM放在当前目录中。工作得很好!
echo >> ~/bin/@REM
chmod +x ~/bin/@REM