Perl on windows

From Svendsen Tech PowerShell Wiki
Jump to: navigation, search




General Information

Perl is available for Windows. I will take the liberty of saying the two major competitors are Strawberry Perl and ActiveState Perl. They're both available for download for free. I have used both and they both have their draw-backs and advantages. Some tongues might have been heard saying Strawberry Perl gave ActiveState a needed incentive for improving their product.

Personally, I have historically been opting for ActiveState in most cases, but I recommend you try them both and make up your own mind. Larry Wall, the creator of the Perl language, uses Strawberry Perl on Windows - which might be construed as a strong endorsement!

You can find some more information about Perl on Windows here at perl.org.

Advantages and Features of Perl on Windows

Perl can be installed and set up with modules on a local computer - and to create a working "snapshot", or copy, of that Perl installation, all you have to do is copy the drive:\Perl directory to \\server\share\perl and access it using a UNC path (or drive letter) with a script as a parameter. Or you can share the local drive:\Perl directly. So you could make a batch file that just calls \\server\share\perl\bin\perl.exe \\server2\share\fix.pl. I demonstrate a method for creating a self-contained batch/cmd file with a working Perl script run from a network share (or locally or on a mapped drive) here.

Note that you might get issues about missing DLL files being used by some modules, such as vcr70.dll, ssleay32.dll, etc. If your script's modules use DLL files, you must still find a way to bundle/install the DLL files. Packaging it in an MSI file (Microsoft Installer) is probably most convenient, but to my knowledge there aren't too many free MSI editors (if any?).

So putting Perl on a network share can be very useful. In a way it's like having Perl installed on every client in a domain, since you can use Perl in startup scripts, logon scripts or whatever you might need it for. This need for "portability" kept me using Perl for a good long while because I needed something that I could guarantee the client had - and I've never been a fan of VBScript. I've been using Perl for Windows systems administration tasks for years. With Windows 7 came native PowerShell support, but many still have XP clients to support. With WSUS, Zenworks and similar products you can "push" PowerShell and .NET, so it is likely to be adopted by more people in the future - and must probably be considered commonly available these days (February 2012).

Sometimes I have needed to send people working executables and PAR is a handy way of packaging a Perl executable. Read more about packaging a Perl executable here.

CPAN - The Comprehensive Perl Archive Network

CPAN is probably the most significant advantage for Perl developers.

As of late summer (northern hemisphere) 2011 there are almost 100,000 pre-written Perl modules, by well over 9000 different authors on CPAN. Some of these represent a lot of work, and the modules are free to be reused easily. Installing them is also simple, either using tools that download and compile packages from CPAN directly or with PPM (Perl Package Manager) which installs precompiled binaries.

Compiling and Installing Modules

Both Activestate Perl and Strawberry Perl can download modules from CPAN and compile them. Strawberry Perl uses this as the normal approach whereas ActiveState traditionally has used repositories, but if you issue the command "ppm install MingW" from cmd.exe or Powershell, it will install the necessary components for compiling (x86/32-bit ActiveState Perl version only, as of July 2011). Strawberry Perl can also install ppms, but I have not done this myself.

You can install modules from the command line:

  • Activestate
    • >ppm install Module::Name
    • >cpan -i Module::Name


  • Strawberry Perl
    • >cpan -i Module::Name

Getting a Decent perldoc Pager - less.exe from UnxUtils

UnxUtils doesn't exactly seem very maintained, but I know UnxUtils' less.exe does a better job than "more.com" (an assembly app in this day and age?). You can download UnxUtils from SourceForge.

First, you might be used to setting the %PAGER% environment variable in Windows XP, but I'm having some issues with this on Windows 7.

When I now set the %PAGER% variable, or $env:PAGER as it is in PowerShell, I'm seeing this error, and the pager isn't being used:

C:\>perldoc -v $]
'less.exe' is not recognized as an internal or external command, operable program or batch file.

You can set it in the GUI (system properties, advanced, environment variables). You have to log off and back on for system environment variables to take effect. This doesn't seem to be useful in Windows 7 though.

So I decided to write a little wrapper function. Now, this was a bit harder than I initially thought, due to some parameter parsing stuff in PowerShell. The easiest thing to do might just be "perldoc -f stat | less", as in piping manually to less.exe, which you put in a path directory.

PS C:\> perldoc perlop | less

Below is the "perldok" function I wrote. Put it in your PowerShell profile (external Microsoft site link) so it's run automatically when you start PowerShell.

function perldok { param([Parameter(Mandatory=$true)][string[]] $Doc,
                         [switch] $f,
                         [switch] $q,
                         [switch] $v)
    
    # Too bad I can't use the switch statement.
    if ($f) {
        
        perldoc -f $Doc | less.exe
        
    }
    
    elseif ($q) {
        
        perldoc -q $Doc | less.exe
        
    }
    
    elseif ($v) {
        
        perldoc -v $Doc | less.exe
        
    }
    
    else {
        
        perldoc $Doc | less.exe
        
    }
    
}

As you can see, I only added support for the parameters I use most commonly myself, namely: -f, -q and -v, and simply a doc name if none of those are specified. If you want to use other perldoc parameters, you will need to use perldoc (not perldok) and pipe to less manually - or customize the function.

The obvious advantages of using less.exe are being able to move both up and down with the arrow keys or the "Page up" and "Page down" keys. You can also search by entering a slash, "/", and then the search term (followed by enter of course). To jump to the next search hit, press "n" (presumably a mnemonic for "next").

NB! Press "q" (for "quit") to exit less.exe.

Self-contained Batch File With Perl Code

See this article: Self-contained batch file with Perl code for information on how to put a Perl script in a batch/cmd file that can be run from PowerShell or cmd.exe, and contains the Perl code within itself.


Perl from PowerShell

I separated the "Perl from PowerShell" stuff out into this article about running Perl one-liners and scripts from PowerShell.

Simple Text Processing Comparison Between Perl and PowerShell

See the following article: PowerShell vs Perl with regards to text processing.

The Theoryx Repository

There's some additional information about ppm from the command line and the Theoryx repository in this separate article.

The Theoryx repository has many useful modules.

To add the Theoryx ppm repo for ActivePerl for Perl 5.8.x, use this command:

ppm repo add Theoryx http://theoryx5.uwinnipeg.ca/ppms/package.xml

To add the Theoryx ppm repo for ActivePerl 5.10.x, use this command:

ppm repo add Theoryx http://cpan.uwinnipeg.ca/PPMPackages/10xx/

To add the Theoryx ppm repo for ActivePerl 5.12.x, use this command:

ppm repo add Theoryx http://cpan.uwinnipeg.ca/PPMPackages/12xx/

To add the Theoryx ppm repo for ActivePerl 5.14.x, use this command:

ppm repo add Theoryx http://cpan.uwinnipeg.ca/PPMPackages/14xx/

A Windows Port of the Linux Rename Utility

See this article: Rename Unix Utility - Windows Port.

Creating Portable Windows Executable Files From Perl Code

See this article: Compiling or packaging an executable from Perl code on Windows.