Wednesday, August 6, 2008

A Short Introduction To Perl

If you already understand what Perl is designed to do, know its features and limitations then you can skip this very small but highly informative section, over which I laboured long and hard for those that didn't know. If you are really sure, jump to the Setup Section.

What is Perl?
Perl is a programming language. Perl stands for Practical Report and Extraction Language. You'll notice people refer to 'perl' and "Perl". "Perl" is the programming language as a whole whereas 'perl' is the name of the core executable. There is no language called "Perl5" -- that just means "Perl version 5". Versions of Perl prior to 5 are very old and very unsupported.

Some of Perl's many strengths are:

Speed of development. You edit a text file, and just run it. You can develop programs very quickly like this. No separate compiler needed. I find Perl runs a program quicker than Java, let alone compare the complete modify-compile-run-oh-no-forgot-that-semicolon sequence.
Power. Perl's regular expressions are some of the best available. You can work with objects, sockets...everything a systems administrator could want. And that's just the standard distribution. Add the wealth of modules available on CPAN and you have it all. Don't equate scripting languages with toy languages.
Usuability. All that power and capability can be learnt in easy stages. If you can write a batch file you can program Perl. You don't have to learn object oriented programming, but you can write OO programs in Perl. If autoincrementing non-existent variables scares you, make perl refuse to let you. There is always more than one way to do it in Perl. You decide your style of programming, and Perl will accommodate you.
Portability. On the Superhighway to the Portability Panacea, Perl's Porsche powers past Java's jaded jalopy. Many people develop Perl scripts on NT, or Win95, then just FTP them to a Unix server where they run. No modification necessary.
Editing tools You don't need the latest Integrated Development Environment for Perl. You can develop Perl scripts with any text editor. Notepad, vi, MS Word 97, or even direct off the console. Of course, you can make things easy and use one of the many freeware or shareware programmer's file editors.
Price. Yes, 0 guilders, pounds, dmarks, dollars or whatever. And the peer to peer support is also free, and often far better than you'd ever get by paying some company to answer the phone and tell you to do what you just tried several times already, then look up the same reference books you already own.

What is ActivePerl? Are the other Perls inactive?
A company named ActiveState exists to provide Perl tools for the Win32 environment. ActiveState used to be ActiveWare, and before that it was sort of a part of Hip Communications. It now appears to be happy with its current name, having not changed it for over a year. Win32 means, at the time of writing, Windows 95, Windows 98 and Windows NT. It does not mean Windows 3.11, even with Win32s installed.

Prior to Perl version 5.005, there was one version of Perl for Win32, and another for all the other systems. The other version was known as the "native version".

The Win32 version was developed by ActiveState, called "Perl for Win32" and typically lagged slightly behind the native version. As of the 5.005 release, Perl for Win32 and the native version have merged -- the native version now supports Win32 directly and doesn't need any tweaking by ActiveState.

ActiveState have dropped "Perl for Win32" and renamed their distribution, which comes with an InstallShield installer, "ActivePerl".

Incidentally, a few months before 5.005 merge the native Perl version was changed so it would run on Win32 directly. This version was best known by the creator's name, "Gurusamy Sarathy". However, there were still quite a few differences between it and Perl for Win32, so many people ran both. The merge brought the best of both worlds together.

Can I run Perl on my computer?
Probably. Perl runs on everything from Amigas to Macintoshes to Unix boxen. Perl also runs on Microsoft operating systems, namely Windows 95, Windows 98 and Windows NT 3.51 and later. There are versions of Perl that run on earlier versions of these operating systems but they are no longer developed or supported. See for full details.

What can I do with Perl ?
Just two popular examples :

The Internet
Go surf. Notice how many websites have dynamic pages with .pl or similar as the filename extension? That's Perl. It is the most popular language for CGI programming for many reasons, most of which are mentioned above. In fact, there are a great many more dynamic pages written with perl that may not have a .pl extension. If you code in Active Server Pages, then you should try using ActiveState's PerlScript. Quite frankly, coding in PerlScript rather than VBScript or JScript is like driving a car as opposed to riding a bicycle. Perl powers a good deal of the Internet.

Systems Administration
If you are a Unix sysadmin you'll know about sed, awk and shell scripts. Perl can do everything they can do and far more besides. Furthermore, Perl does it much more efficiently and portably. Don't take my word for it, ask around.

If you are an NT sysadmin, chances are you aren't used to programming. In which case, the advantages of Perl may not be clear. Do you need it? Is it worth it?

After you read this tutorial you will know more than enough to start using Perl productively. You really need very little knowledge to save time. Imagine driving a car for years, then realising it has five gears, not four. That's the sort of improvement learning Perl means to your daily sysadminery. When you are proficient, you find the difference like realising the same car has a reverse gear and you don't have to push it backwards. Perl means you can be lazier. Lazy sysadmins are good sysadmins, as I keep telling my boss.

A few examples of how I use Perl to ease NT sysadmin life:

User account creation. If you have a text file with the user's names in it, that is all you need. Create usernames automatically, generate a unique password for each one and create the account, plus create and share the home directory, and set the permissions.
Event log munging. NT has great Event Logging. Not so great Event Reading. You can use Perl to create reports on the event logs from multiple NT servers.
Anything else that you would have used a batch file for, or wished that you could automate somehow. Now you can.

What can't I do with Perl ?
The question is, "what shouldn't I do with Perl". Write office suites is one answer. Perl, like most scripting languages, is a glue language designed for short and relatively simple tasks. Just don't equate this philosophy with a lack of power or "serious" features.

See the FAQs at Of course there are Usenet groups, but also many mailing lists. Microsoft Windows users will be interested in those hosted by which discuss all things Perl and Windows.

Please, before you ask any question, anywhere:

Make sure you read the group charter. Many people put time and effort into the creation of those charter in the interests of efficient discussion, so don't degrade the discussion quality and insult us by ignoring the guidelines.
Read the FAQs at least twice. Try and find related FAQs. Try hard. You won't be popular if you post a question starting "I've looked at all the FAQs..." and then ask something that actually is in the FAQs. Or the manual for that matter. Believe me, it will be patently obvious to all on the list if you haven't done your homework.
Carefully phrase the questions and provide source code because if you do that, you may well end up solving the problem yourself because you have thought it through a little more.
Think to yourself -- honestly -- if I was a busy Perl Professional, would I want to answer my own question?

Does it clearly state what I want an answer to? Preferably just one question at a time. Am I being unreasonable, for example asking for someone to code it for me? Have I shown evidence that I have tried to help myself? Have I made any mistakes in grammar? Is it polite? Is there enough information in there for the answer to be given?

Why should you care? Well, if you ask poorly-formed questions or those already answered in the FAQ...let's just say you won't get the answers you want. If you care about your online reputation and wasting other people's time -- two more reasons.

No comments: