Self-contained batch file with perl code

From Svendsen Tech PowerShell Wiki
Jump to: navigation, search




Self-contained Batch File With Perl Code

Here you will find a Perl and PowerShell script that creates a batch wrapper for running Perl code from a Perl installation copied to a mapped network drive, a UNC share or a local drive directly from a cmd/batch file that contains the actual Perl code. A single file can be cleaner and easier than creating a separate batch wrapper file.

The Perl version is also available as a stand-alone executable file (exe file). Read more about packaging portable, 32-bit Windows executable files from Perl code here.

Read more about Perl on Windows here.

Download Perl2Cmd



Documentation

Perl Version Docs

Here are the top comments from the actual Perl script code and the help text you get from the prompt without the required parameters/arguments being specified.

############################################################################
## Prepare a perl script for being run from a network share               ##
## or an UNC path. If you have the path \\server\share\perl\bin\perl.exe  ##
## the specified path should be "-s \\server\share\perl".                 ##
## If it's on a mapped drive, it would be "-s X:\somedir\perl"            ##
############################################################################

PS E:\temp> perl .\Perl2Cmd.pl
Usage:
Perl2Cmd.pl -f "file.pl" -s "\\server\share\perl"
-o "outputfile.cmd"

-f : .pl file to convert to .cmd
-s : Server and share where Perl should be run from. Should be the
     Perl root directory as "\bin\perl.exe" will be appended.
-o : Output file name. You will be asked to overwrite if it exists.

Examples:
Perl2Cmd.pl -f "get_files.pl" -s "\\server.domain.com\perl" -o "test.bat"
Perl2Cmd.pl -f "do_stuff.pl" -s "\\dev-server\perl" -o "T:\do_stuff.cmd"

You don't need to quote arguments unless they contain spaces.
You can use either the .bat or the .cmd extension. The cmd extension allows
for more advanced batch features, but does not run on NT4/Win98. None of
these batch features are being used, so use whatever you prefer, with a
note that .bat is more backwards-compatible.

Author: Joakim Svendsen, Svendsen Tech, http://www.powershelladmin.com

Here is an example of its use:

PS E:\temp> type .\perlcode.pl
use warnings;
use strict;
print "This is the Perl code!\n";

PS E:\temp> perl .\Perl2Cmd.pl -f .\perlcode.pl -s C:\Perl -o perlcode.cmd
Done! Output file for use with C:\Perl: perlcode.cmd

PS E:\temp> .\perlcode.cmd
This is the Perl code!

PowerShell Version Docs

Here is the integrated help text from the PowerShell version:

PS E:\> Get-Help .\Perl2Cmd.ps1 -detailed

NAME
    E:\Perl2Cmd.ps1

SYNOPSIS
    Prepares a Perl script for being run from a Perl installation available from
    a UNC path, a local drive or a network drive, in a self-contained cmd/batch file,
    that also contains the Perl code.

    If you have the path \\server\share\perl\bin\perl.exe, the specified path to
    Perl2Cmd should be "-PerlShare \\server\share\perl". If it's on a mapped drive,
    it would be "-PerlShare X:\somedir\perl"

    Author: Joakim Svendsen
    http://www.powershelladmin.com/wiki/Self-contained_batch_file_with_perl_code


SYNTAX
    E:\Perl2Cmd.ps1 [-PerlShare] <String> [-PerlScript] <String> [-OutputFile] <String> [<CommonParameters>]


DESCRIPTION


PARAMETERS
    -PerlShare <String>
        Path to root Perl directory, containing a bin subdirectory with perl.exe and
        a full Perl environment such as from a regular Strawberry or ActiveState Perl
        installation.

    -PerlScript <String>
        Path to the Perl script you want to turn into a self-contained batch file.

    -OutputFile <String>
        The name of the output file. If it exists, you will be prompted to overwrite.

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        "get-help about_commonparameters".

REMARKS
    To see the examples, type: "get-help E:\Perl2Cmd.ps1 -examples".
    For more information, type: "get-help E:\Perl2Cmd.ps1 -detailed".
    For technical information, type: "get-help E:\Perl2Cmd.ps1 -full".

Here is an example of the Powershell version:

PS E:\temp> .\Perl2Cmd.ps1 -PerlShare '\\2008r2esxi\perl' -PerlScript 'perlcode.pl' -Output test.cmd
##########################################################
### Creating cmd file: test.cmd
### Using Perl from:   \\2008r2esxi\perl\bin\perl.exe
### From:              perlcode.pl
##########################################################

Done!
PS E:\temp> .\test.cmd
This is the Perl code!

Example cmd file

Here you can see the structure of the cmd/batch file and how it works. Basically, you "cheat", and exploit the fact that batch isn't compiled and just runs sequentially, and that @rem signifies an array in Perl and a hidden comment in batch. It's sort of neat.

PS E:\temp> type test.cmd
@rem = '-- Batch wrapper created with Svendsen Tech Perl2Cmd --
@echo off
\\2008r2esxi\perl\bin\perl.exe %0 %1 %2 %3 %4 %5 %6 %7 %8 %9

goto endofperl

@rem ';

use warnings;
use strict;
print "This is the Perl code!\n";


__END__
:endofperl

I've been using it like this with the "__END__" section at the bottom followed by ":endofperl", but you could also exploit the special batch ":EOF" marker, so you can use "goto :EOF" instead of "goto endofperl", and omit the "__END__" part.

@rem = '-- Batch wrapper created with Svendsen Tech Perl2Cmd (PowerShell version) --

@echo off

\\server\perl\bin\perl.exe %0 %1 %2 %3 %4 %5 %6 %7 %8 %9

goto :EOF

@rem ';

use warnings;
use strict;

print "This is Perl code!\n";
print "This is even more Perl code\n";

my $i = 42;
print $i, "\n";

Output from this script on my computer:

PS E:\> .\temp.cmd
This is Perl code!
This is even more Perl code
42
PS E:\>


Keywords: perl2bat, perl2cmd, perl to batch file, perl2batch