11 minute read
Set Up Perl Windows 7
Set Up on Perl Windows7
Most modern Linux distros are shipped with Perl. However, it needs to be installed on Windows. Perl is a high level programming language, and was written by Larry Wall and others. Because of its process, file and text manipulating facilities, it is popular with programmers. Here’s a guide to setting up Perl on Windows 7.
Advertisement
Perl (Practical Extraction and Reporting Language) was developed by Larry Wall and has its roots in UNIX. Its inbuilt regular expressions engine, good execution speed, CPAN (Comprehensive Perl Archive Network) and, of course, the amazing Perl community, make it an ideal general-purpose programming language.
If you have Linux on your system, you are very likely to have some version of Perl, as well as an excellent tool chain such as gcc, make, etc. Windows users, however, are not so lucky. Perl has to be installed separately, and till a few years ago Activestate Perl was the only option available.
Another issue was that a lot of applications that depended on Perl would install (mostly) older versions of Perl, and if you did not have admin rights to the Windows Server/workstation, there was just no easy way to install your own version of Perl. Even if you were very careful, you could very well end up messing up the pre-installed Perl.
That is exactly where Strawberry Portable Perl comes in.
What's Strawberry Perl?
It is a Perl environment for MS Windows containing all you need to run and develop Perl applications. It is designed to be as close as possible to the Perl environment on UNIX systems.
It includes Perl binaries, a compiler (gcc) + related tools, all the external libraries (crypto, graphics, xml, etc), a lot of really great modules pre-installed, cpanm - a nice utility that makes module installation easier, all the bundled database clients, and a lot more.
In fact, the home page of Strawberry Perl has a statement from Larry Wall himself: “When I am on Windows, I use Strawberry Perl."
So let’s begin. Here's what I have installed on my system:
C:\Users\pmu>perl –version This is Perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2012, Larry Wall Binary build 1603 [296746] provided by ActiveState http:// www.ActiveState.com Built Mar 13 2013 13:31:10 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.
I installed this before I found Strawberry Perl and I let it be because, thanks to Strawberry Portable Perl, multiple versions of Perl can peacefully co-exist. I had encountered a similar situation on one of our SAN Management Servers. ActiveState Perl was pre-installed by another application which happened to use it for some heavy text manipulating
work. This server did not have access to an outside network. Some of the Perl scripts that I had written required a few modules, but due to the restricted access, I could not install them. Even if I did get access, I didn't want to fiddle with the pre-installed version of Perl.
Where do we get it?
Go to www.strawberryperl.com and under the section More downloads, click on Other releases: ZIP, Portable, older versions. Download the ‘Portable Zip’ editions of Perl. I downloaded 32- and 64-bit portable versions for Perl 5.16.3 and 5.18.2.1 as well as a 32-bit version of 5.10.1. Once you have completed downloading them, unzip them to separate folders. This is what it looks like, in my case:
C:\>dir | find /I "perl" 02/23/2014 06:23 PM <DIR> my32bitperl516 02/23/2014 02:12 AM <DIR> my32bitperl518 02/19/2014 12:13 AM <DIR> my64bitperl516 02/19/2014 12:16 AM <DIR> my64bitperl518 02/19/2014 12:12 AM <DIR> myperl510 02/18/2014 11:46 PM <DIR> Perl64
This is what each folder contains:
C:\my32bitperl516 Folder For 32 Bit Perl Version 5.16.3.1 C:\my32bitperl518 Folder For 32 Bit Perl Version 5.18.2.1 C:\my64bitperl516 Folder For 64 Bit Perl Version 5.16.3.1 C:\my64bitperl518 Folder For 64 Bit Perl Version 5.18.2.1 C:\myperl510 Folder For 32 Bit Perl Version 5.10.1 C:\Perl64 Folder For 64 bit ActiveState Perl Version 5.16.3.1
You can give any name you wish to the folders when you unzip them. Just ensure that there are no spaces.
Let's cd to the my32bitperl518 folder and see what it contains:
C:\my32bitperl518>dir
Volume in drive C is Windows7_OS
Volume Serial Number is 52DF-20FF
Directory of C:\my32bitperl518 02/01/2014 09:00 PM <DIR> . 02/01/2014 09:00 PM <DIR> .. 05/04/2011 09:21 AM <DIR> c 01/07/2014 07:19 PM <DIR> cpan 02/02/2014 12:17 AM <DIR> data 01/07/2014 07:19 PM 9,575 DISTRIBUTIONS.txt 01/07/2014 07:19 PM <DIR> licenses 01/07/2014 04:50 PM <DIR> perl 12/03/2013 11:47 AM 2,661 portable.perl 11/15/2013 11:16 AM 915 portableshell.bat 01/07/2014 07:39 PM 1,618 README.portable.txt 01/07/2014 07:19 PM <DIR> win32 4 File(s) 14,769 bytes 8 Dir(s) 138,036,543,488 bytes free C:\my32bitperl518>
Go through the README.Portable.txt file. It’s got a lot of useful information. There are a few environment variables that you can add to make a Portable Perl load up automatically.
Next, run the portableshell.bat file as shown and check the Perl version:
C:\my32bitperl518>portableshell.bat
Welcome to Strawberry Perl Portable Edition! * URL - http://www.strawberryperl.com/ * see README.portable.TXT for more info ---------------------------------------------Perl executable: C:\my32bitperl518\perl\bin\perl.exe Perl version : 5.18.2 / MSWin32-x86-multi-thread-64int C:\my32bitperl518> C:\my32bitperl518>perl --version This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x86-multi-thread-64int Copyright 1987-2013, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. C:\my32bitperl518>
Note how easily I can switch to this Perl Version. It also installed gcc and make. Here's the output:
-C:\my32bitperl518>gcc --version gcc (release with patches / build 20130526 by strawberryperl. com) 4.7.3 Copyright (C) 2012 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. C:\my32bitperl518>make --version GNU Make 3.82 Built for i686-w64-mingw32 Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/ licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. C:\my32bitperl518>
Now let’s try installing a module.
Note: Ensure that you are in the relevant Perl directory while installing modules.
In the example below, you will cd to the directory where the 64-bit Perl version 5.16 is installed, execute the portable. bat file and install the Net::SSH2 module. Net::SSH2 is a great module that you can use to write Perl scripts to log in to Linux/UNIX servers (or any device based on these operating systems), run commands and capture the outputs.
C:\>cd C:\my64bitperl516 C:\my64bitperl516>portableshell.bat ----------------------------------------------
Welcome to Strawberry Perl Portable Edition! * URL - http://www.strawberryperl.com/ * see README.portable.TXT for more info ---------------------------------------------Perl executable: C:\my64bitperl516\perl\bin\perl.exe Perl version : 5.16.3 / MSWin32-x64-multi-thread C:\my64bitperl516>perl --version | find /I "version" This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-thread C:\my64bitperl516>cpanm Net::SSH2 --> Working on Net::SSH2 Fetching http://www.cpan.org/authors/id/R/RK/RKITOVER/NetSSH2-0.53.tar.gz ... OK Configuring Net-SSH2-0.53 ... OK Building and testing Net-SSH2-0.53 ... OK Successfully installed Net-SSH2-0.53 (upgraded from 0.48) 1 distribution installed
Cd to the my32bitperl518 directory, run portableshell.bat and you will have the 32-bit Perl version 5.18 running!
C:\Users\pmu>cd C:\my32bitperl518 C:\my32bitperl518>portableshell.bat ----------------------------------------------
Welcome to Strawberry Perl Portable Edition! * URL - http://www.strawberryperl.com/ * see README.portable.TXT for more info ---------------------------------------------Perl executable: C:\my32bitperl518\perl\bin\perl.exe Perl version : 5.18.2 / MSWin32-x86-multi-thread-64int
C:\my32bitperl518>perl --version | find /I "version" This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x86-multi-thread-64int
C:\my32bitperl518>cpanm Net::SSH2 --> Working on Net::SSH2 Fetching http://www.cpan.org/authors/id/R/RK/RKITOVER/NetSSH2-0.53.tar.gz ... OK Configuring Net-SSH2-0.53 ... OK Building and testing Net-SSH2-0.53 ... OK Successfully installed Net-SSH2-0.53 (upgraded from 0.48) 1 distribution installed
Note that it's installed the module again. It’s because we were in the directory in which the 32-bit Perl version 5.18 is installed, that it went ahead and installed the module. The advantage is that we can selectively install modules for different versions of portable Perl! Note that in both the cases, it upgraded a pre-installed module of Net::SSH2. This is another advantage of Strawberry Perl. A lot of modules come pre-installed.
Check the outputs below. The first output shows that the module Net::SSH2 is not installed for the Activestate Perl.
C:\Users\pmu>perl --version | findstr /I "version active" This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x64-multi-threadBinary build 1603 [296746] provided by ActiveState http://www.ActiveState.com C:\Users\pmu>perldoc perllocal | find /I "Net::SSH2" C:\Users\pmu>
The same is true for Perl version 5.10:
C:\Users\pmu>C:\myperl510\portableshell.bat ----------------------------------------------
Welcome to Strawberry Perl Portable Edition! * URL - http://www.strawberryperl.com/ * see README.portable.TXT for more info ---------------------------------------------Perl executable: C:\myperl510\perl\bin\perl.exe Perl version : 5.10.1 Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\pmu>perldoc perllocal | find /I "Net::SSH2" C:\Users\pmu>
Similarly, let’s install another module, Config::Tiny. This is another handy module that allows you to have a nice config file, from which your scripts can pick up parameters, such as a username, a password, a command that you would want to run, or the location of a file:
C:\my32bitperl518>cpanm Config::Tiny --> Working on Config::Tiny Fetching http://www.cpan.org/authors/id/R/RS/RSAVAGE/ConfigTiny-2.20.tgz ... OK Configuring Config-Tiny-2.20 ... OK
Building and testing Config-Tiny-2.20 ... OK Successfully installed Config-Tiny-2.20 1 distribution installed
We then move in to the directory that has the 64-bit Perl version 5.16 installed, run the portableshell.bat command and install the same module:
C:\my64bitperl516>cpanm Config::Tiny --> Working on Config::Tiny Fetching http://www.cpan.org/authors/id/R/RS/RSAVAGE/ConfigTiny-2.20.tgz ... OK Configuring Config-Tiny-2.20 ... OK Building and testing Config-Tiny-2.20 ... OK Successfully installed Config-Tiny-2.20 1 distribution installed
And in the 64-bit Perl version 5.18 directory as well, don't forget to run portableshell.bat:
C:\my64bitperl518>cpanm Config::Tiny --> Working on Config::Tiny Fetching http://www.cpan.org/authors/id/R/RS/RSAVAGE/ConfigTiny-2.20.tgz ... OK Configuring Config-Tiny-2.20 ... OK Building and testing Config-Tiny-2.20 ... OK Successfully installed Config-Tiny-2.20 1 distribution installed
Now, let’s write a script and run it against each Perl version.
Note: When you run scripts, you don't need to change to the specific directory where the desired Perl Version is installed. Just run the portableshell.bat from the desired direrctory. If I run C:\my32bitperl516\portableshell.bat, I get to use the 32 bit Perl Version 5.16, and after I am done and want to usePerl Version 5.18 64 bit, I just run C:\ my64bitperl518\portableshell.bat. You just need to hit exit a few times to get back to the earlier version of Perl.
You just need to hit Exit a few times to get back to the earlier version of Perl.
Let's write a small script that uses the Smartmatch feature. Nothing fancy, but let’s create two arrays, each with names of different Linux flavours, and then use the Smartmatch operator - ~~ - to compare them. It's on the way to being deprecated and has moved to ‘experimental’ in Perl Version 5.18.
#!/usr/bin/perl #script name pl1.pl use warnings; use strict; my @firstarray = qw (debian redhat ubuntu centos mint stella elementaryos); my @secondarray = qw (debian redhat ubuntu centos mint stella elementaryos); print "The arrays have the same elements!!\n" if @firstarray ~~ @secondarray;
When it’s run using Perl version 5.16 (whether 32- or 64bit will not matter here), you will get the following output:
C:\Users\pmu>perl --version | find /I "version" This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x86-multi-thread C:\Users\pmu>perl pl1.pl The arrays have the same elements!!
And with Perl version 5.18, you’ll get the following output:
C:\Users\pmu>perl --version | find /I "version" This is perl 5, version 18, subversion 2 (v5.18.2) built for MSWin32-x64-multi-thread C:\Users\pmu>perl pl1.pl Smartmatch is experimental at pl1.pl line 8. The arrays have the same elements!!
Note the line in the output that says ‘Smartmatch is experimental at pl1.pl line 8’. As mentioned before, Smartmatch has been moved to ‘experimental’ with Perl version 5.18, and that's why we get the message when the script is run with Perl version 5.18. If you were to run a script with ‘given-when’ statements, you'd get something similar. This way, you can run the same script against different Perl versions and see how it works.
You can also copy the required folder to a USB drive, paste it into another computer, run the portableshell.bat command, and you've got Perl running in there too!
Contrary to what you might have heard from fellow techies or read on the Internet, Perl is very much alive and is still being actively used. A number of organisations, ranging from financial giants, media companies and many others, still use Perl for a lot of critical tasks. It's truly disheartening to see that such a powerful language is sometimes mistakenly referred to as ‘write only’, ‘ugly’, etc. It's not a shortcoming of a language if people write unreadable/unmaintainable code in it. One can write well written/formatted code in any language, including Perl. This is one language that is very versatile and is bound to meet your needs.
Yes, there is a steep learning curve with the advanced topics, but once you start getting the hang of it, you'll use Perl a lot more than you’d imagine.
By: Pritesh Manohar Ugrankar
The author is an open source enthusiast who loves Perl and Linux. You can reach him at pritesh.ugrankar@gmail.com