TOTKat - TOT: The One True (prefixing a name),
			coined by The Gotdammerell for TOTKat, originally to distinguish herself from Kat Knight.
 

TOTKat: (hand coded) online journal of a girl geek since April 1999.
WARNING: contains personal opinions. A sense of perspective is required when reading.

 


     
   
   
   
   
   
   
   
   
   
     
   
   
   
   
   
   
   
   
   
   
   
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 
  


Introduction to Perl

The very fact that it's possible to write messy programs in Perl is also what makes it possible to write programs that are cleaner in Perl than they could ever be in a language that attempts to enforce cleanliness.
--Larry Wall, Linux World 1999


| Intro | Start | Style | Basics | Example | Links |


The Camel Book

This section is being developed bit by bit. As and when I get time to write useful things, they will be added. I would strongly advise the purchase of the Camel book as it is an invaluable reference text. Also, there is an excellent primer at Leeds University.

Introduction

Perl - Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister - however you want to think of it, it is probably one of the most invaluable weapons in the arsenal of the UNIX admin/user or NT admin, and in fact web developer. It is pretty much platform independent, the most fiddling you may need to do to a script to port it from NT to Solaris, for example, is to edit any system functions you may have used.

Have you ever had an output from a shell command that was a great long list, that you wanted to perform repetitive operations on some of that list? Ever wanted to repeatedly create user accounts under NT? How about purging all files that are older than 30 days from a particular directory and it's subdirectories? If you can truthfully say no to these or any other repetitive task; then quit now and don't bother reading on.

So... you're all still with me, aren't you? Good. Perl is a very important tool for many reasons. It is more powerful than shell scripting and less complex and difficult than C. However, it will give you more than enough rope with which to hang yourself. For example; if you have done any programming before now, you will be familiar with the concept of variable declaration - i.e. telling the compiler/interpreter that you would like to reserve certain label for a certain task, for example it is going to be used integer maths. Perl does not insist that you do this, and in fact, it means that you can actually reduce the number of variables you need, for example if you intend doing operations on values like the length of the variable contents. This can lead to awful messes when you end up doing mathematical logic operations or integer mathematics on a text string when you really intended something different. You have to think carefully, but if you want to do something, you can and There's More Than One Way To Do It (the perl motto - TMTOWDI). You will often find that if you give two Perl programmers the same task to perform, the resulting scripts can be almost completely different.



Return to [top]  [Intro]

Getting Started With Perl

As with all programming and scripting, getting started with learning it can be very dry if you have no little project to write with it. The reason I went off and learned Perl was due to a deficiency in the admin suite for Windows NT (what a surprise!). There was a nice GUI tool that would let me look at a user's account and show me what global groups they were a member of but I could not find out of which local groups they were a member without clicking on each local group in the user manager for that local resource server and seeing who was a member of that group to find the relevant user name. This had become a rather cumbersome task when the number of local groups had exceeded 170. So, I sat down and wrote a Perl script. Or rather, I knew what I wanted to do, constructed the script structure on paper and then looked up which commands I'd need for each function and wrote a rather awful, yet functional, script. Once that was working, I ended up being asked to do more scripts as I'd realised that this Perl stuff was going to be rather useful. More commands and functions got learned as I needed to do more and different things. OK, so this is a very unstructured approach to learning, but it works. You remember what you have done and why.

Once you combine the desire, and need, to write a script with the man pages and The Camel Book (Programming Perl - Larry Wall; pub. O'Reilly Associates ISBN:1565921496) - thus called because there is a picture of a camel on the front - you will be off to a flying start. There is another O'Reilly book - Learning Perl, if you really feel the need, but most people can get to grips with Perl straight from the Camel Book.



Return to [top]  [Start]

Style

| Style Guidelines |

Perl as a fashion statement... well... kind of. Larry Wall (the guy who developed Perl in the first place - God bless him) has given a number of talks on this subject area, most pertinently "Perl, The First Post-Modern Computer Language" in which he expounds on Perl Culture. Although the talk makes for fascinating reading, it is not what I'm talking about here.

I'm talking about the way the programs are written; to make them work logically, beautifully and with readable source. Larry wrote a set of guidelines for Perl Style, with most of which I agree. The guidelines and my exceptions are [here].

The basic idea is to ensure that statements are aligned vertically and that the code is commented sufficiently, but not overly.



Return to [top]  [Style]

Basics

| Editors | Syntax | Variables | Commands |

Editors... such a choice. You can edit your perl in any old text editor that you have to hand. If you are editing on a web server it is more than likely that you'll have vi available and that does fine for short scripts. By preference, I use EditPlus as it has templates for various file types and handles both perl and HTML very nicely. In any case, you really don't need anything fancy, just make sure that whatever you do use, it doesn't add in any control characters that could make a mess of your hard work.

All scripts should start by referencing the perl interpreter:-

   #! usr/bin/perl -w

Which calls the interpreter, and the -w is a switch that is most advisable as it causes the interpreter to use the warnings pragma i.e. you will get some useful diagnostics out. -w is not mandatory, but it is rather a good idea, especially during any debugging phase.

As with shell scripts and C, each line is terminated with a ";"

   print "Hello world!";

Whitespace is ignored, and lines of code may span lines in the editor, so long as they are appropriately terminated with the ";"

Comments are preceded by a "#"

   #This is a comment

   print "Hello world"; # This is also a comment

Every variable kind (scalar - $, array - @, hash - %) has its own namespace. This means that $foo and @foo are two different variables. [It also means that $foo is a part of @foo, not a part of $foo. This may seem a bit weird, but that's OK, because it is weird.]

There are important predefined variables that can be extremely useful for shortcutting around what could be lengthy code. $_ and @_ are probably the most commonly used, where $_ is a component of @_.



Return to [top]  [Basics]

Links


Mailto:webmaster@totkat.org

Site statistics