Perl Lesson #1

Fun with variables:

Create a new variable with text in it and print it out.

  1. #!/usr/bin/per
  2. $textvar = "Some Text";
  3. print $textvar;

Create a new variable with a number in it and print it out.

  1. #!/usr/bin/perl
  2. $numbervar = 1;
  3. print $numbervar;

Create and increment a text variable. This will increment the letter in the variable by one alphabetical letter. For instance it will increment the letter “a” to “b”. It also understands case, which means that if you variable is an upper-case “A”, it will increment it to an upper-case “B”.

  1. #!/usr/bin/perl
  2. $lettervar = "A";
  3. $textvar ++;
  4. print $lettervar;

Create and increment a number variable.

  1. #!/usr/bin/perl
  2. $numbervar = 1;
  3. $numbervariable ++;
  4. # or
  5. $numbervar = $numbervar + 1;
  6. print $numbervar;

Create a variable with a lot of things in it, and split it up into individual variables on a “:” symbol, and print out the resulting individual variables.

  1. #!/usr/bin/perl
  2. $data = "1:2:3:4:5";
  3. ($a,$b,$c,$d,$e) = split (/:/,$data);
  4. print "$a, $b, $c, $d, $e";

Create variables with strings in them, and glue two strings together.

  1. #!/usr/bini/perl
  2. $numbervar = 1;
  3. $textvar = "String number:" . "-->" . $numbervar;
  4. print $textvar;

Perl Lesson #2

In this little script, we take the Apache access_log file, and read it into an array. Each element of the array is split on the space (” “), and we print out the first element, which is the IP address of the machine connecting to our website.

Here is the format for the log file:


69.47.145.197 - - [15/Jan/2006:22:35:12 -0500] "GET / HTTP/1.1" 301 354
69.47.145.197 - - [15/Jan/2006:22:35:39 -0500] "GET /content/images/oa1.jpg HTTP/1.1" 301 376
69.47.145.197 - - [15/Jan/2006:22:35:40 -0500] "GET / HTTP/1.1" 301 354
202.7.166.167 - - [16/Jan/2006:00:02:41 -0500] "GET /content/images/oa1.jpg HTTP/1.0" 301 376
202.7.166.167 - - [16/Jan/2006:00:06:23 -0500] "GET /content/images/oa1.jpg HTTP/1.0" 301 376
66.249.64.14 - - [16/Jan/2006:00:14:08 -0500] "GET /robots.txt HTTP/1.0" 301 364

  1. #!/usr/bin/perl
  2. open (FD, "/path/to/your/access_log");
  3. while (<fd>)
  4. {
  5. @array = split (/[" "]/);
  6. foreach $i (@array)
  7.         {
  8.         print $array[0] . "\n";
  9.         }
  10. }
  11. close (FD);

In this example we use the special variable “$_”. This is used to hold data from the file without having to do an explicit read operation. If we wanted to split the lines on other characters we could do something like this:

@array = split (/[-,:," "]/);

This would split it on the dash “-“, colon “:” and the space ” “.

If we wanted to read out each line as it is read in, we could simply replace:

print $array[0] . "\n";

With:

print $i . "\n";