Perl preamble that will run on Windows or Linux

Is there any single preamble magic that will make Perl script work under Windows (as a batch file) or Linux (as an executable file), like the preambles that make it work under any shell?

+3
source share
2 answers

I doubt that you can force windows to accept shbang ( #!). Therefore, if you can work with the default shell (in my case bash), then this works in 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

This required an executable file in my NIX path named '@REM'.

echo >> ~/bin/@REM
chmod +x ~/bin/@REM
+7
source

, 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 -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

script :

1;    
__END__
:endofperl

, BAT "endofperl" "goto endofperl" ( "exit"?).

+3

Source: https://habr.com/ru/post/1778996/


All Articles