perl by 1KeLzN7

VIEWS: 0 PAGES: 28

									Very Simplified Introduction to classical Perl
This note is for people, who knows next to nothing of Perl, and would like a "really quick " introduction to Perl (in the mode of a "classical" scriptin
As such, this note is on an very introductionary level , and is obviously very incomplete .

There are many excellent tutorials on the Net, so this note is probably (or likely) a bit unneccessary. But since it is supposed to be "a really quick in
So If you are really "blank" to Perl, this note should get you "on the track", in a couple of hours. So, I hope you like this one.

Version:         0.4
Date:            4/12/2008
Compiled by:     Albert van der Sel - Antapex Technologies B.V.


What is Perl, and why use it?
Perl, in the classical scriptlanguage form, is available on almost all platforms (computersystems), wether it is a mainframe, mini, Server or PC.
Even that fact alone, makes it very interresting to take a good look into this "language".
It's fair to say that you can use Perl for almost anything, but most people would agree on the fact that Perl is mostly used in creating (small) power
Those scripts could be all sorts of administrative tasks (e.g.: on a unix system), or all sorts of file manipulation where the format of those files gets
Ofcourse, there is much more to Perl than the above examples. For example, from your Perl code (for example, running on Windows, or Solaris, o
you can even access all popular databases (Oracle, DB2 etc..), if the right DBD-DBI driver is available.
Also, it's possible to use all sorts of interfaces like OLEDB, ADO etc..
So, from your Perl code, you can also retrieve Oracle data, manipulate it in a certain way, and put those results in a file.
There are so many additions to this classical scriplanguage, that perl has found (by long) it's way to all sorts of "Web-techniques" (e.g.CGI) as wel

We can think of a million other examples here. One last note on the immense power and versitality of Perl is this: if you install a major application
and you browse the directory tree of that application, what do think you will find "somewhere" in that tree? Perl !
Also, if you must install some major business application, and read the requirements, chanches are that you must have perl availble on your syste
begin installing that application.

In it's most common (and simplest) uses, Perl is an "interpreter". Or, in other words, you may call it a "scripting language". Once you have installe
a sort of "runtime" environment on your system. From that moment, you can create scripts. Those scripts are flat txt files (ascii), containing Perl co

Note: Some people might argue that "interpreter" is not the right qualifiction. That's true, because perl can also be used in compiling co
(for our purposes) is that if we feed Perl a script, Perl will execute it, without first creating permanent objectcode or executable on disk
In this mode, Perl parses the file, and creates a form of "perl-bytecode" suitable for running in the perl runtime.

So, when we create a script, by convention, the file extension should be ".pl", like in "testprg.pl", but that's not mandatory.
You can run those scripts by calling "perl" with as a parameter, your scriptfile, like in the following examples:

$/home/albert> perl testprg.pl                              (on unix)

C:\TESTS> perl testprg.pl                                   (windows)

Auto-associate:
On Windows systems, you might have associated the extension ".pl" with Perl, meaning that just calling the script will put Perl at work.
But you can also make that mechanism work in another way, or make it work on other platforms as well.
Suppose you are on Unix. If you put on the very first line of your script the following:
#!/usr/bin/perl

Or on Windows
#!c:\perl\bin\perl.exe

Then your shell "knows" that it's a script for Perl to execute. Ofcourse, make sure you use the right path to perl, because I have only used some ex
Note: take notice of the " ! " symbol

As from then, on Windows you can call the script directly, as in this example:
C:\TESTS> testprg.pl

And on unix systems, first put the "execute bit" on with the command "chmod u+x testprg.pl". Then you can call your script as in:
$/home/albert> ./testprg.pl

On most platforms, version 5.8.x is quite current . At the time of writing, 5.10.x is very current. But even if you use an older version like 5.6, it's OK

One last remark: this note deals exclusively with "classical Perl", which means that we are going to take a look at some traditional perl
extensions on Perl, like coding classes, ActiveX, or how to use Perl in webpages etc.. It's just about the very basic (classical) stuff.
Also, do not expect a nice graphical Workbench like in Microsoft products (e.g like Visual Studio). Here we just use an editor like vi on u




Chapter 1. Basic Perl syntax and examples.

Example 1:

A basic read file example. Put the following in the file "C:\TEMP\example1.pl"

  $var="c:/temp/sample.txt";

  open VAR, $var or die "Cannot open $var for read :$!";

  while (<VAR>) {
          print "The Line number $. contains : $_";
  }

Now suppose that the txt file "c:\temp\sample.txt" contains this:
hello appie
goodmorning, would you like some coffee?
Yes please!

Let's run our script, and see what happens:
C:\TEMP> perl example1.pl
The Line number 1 contains : hello appie
The Line number 2 contains : goodmorning, would you like some coffee?
The Line number 3 contains : Yes please!
C:\TEMP>

What it does:
What this script does, is open the file defined in $var, assign it to the filehandle VAR and then, while there are still lines left in the file,
print the line number $. and the current line of the file.
The first line "looks like" a variable declaration and assignment. The amazing thing is, that you can assign a file to $var, no problem.
Whenever you see "open YOURVAR", this means that a filehandle gets created. From that moment, you have an "entry" to the file, and you
can start to do whatever it is you want to do..
Also note the "exception handling", expressed in "open VAR, $var or die "yourmessage"
This is a sort of "try .. Catch" block, meaning that if the statement "open VAR" fails (because maybe sample.txt is not there), we exit (die) the scrip
The "$!" is a Perl variable that stores the errorcode, if indeed an error was encountered.

The "< >" is the line input operator, meaning that we read in a complete line of the file, all the way up to the newline, and put it into "$_".
The special variable "$." is the line number, which gets printed as well. Finally, the "while loop" makes sure we process all lines in the file.

Now since this example is executed in a DOS (cmd) box in Windows, there is a small issue with the "\" backslash.
Normally, in many scriptlanguages, that symbol is an "escape" character, so we just use the forward slash "/" in the path to sample.txt, which Perl
You could also have escaped the backslashes, that is, like so: $var="c:\\temp\\sample.txt";

Example 2

Lets do a basic "write" example. Put the following in the file "C:\TEMP\example2.pl"

  $out="c:/temp/out.txt";

  open OUT, ">$out" or die "Cannot open $out for write :$!";

  for $i (1..5) {
          print OUT "$i : The time is now : ",scalar(localtime),"\n";
  }

Lets run the script and see what happens:
C:\TEMP>perl example2.pl
C:\TEMP>type      out.txt
1 : The time      is now :     Tue   Dec     2   14:16:21    2008
2 : The time      is now :     Tue   Dec     2   14:16:21    2008
3 : The time      is now :     Tue   Dec     2   14:16:21    2008
4 : The time      is now :     Tue   Dec     2   14:16:21    2008
5 : The time      is now :     Tue   Dec     2   14:16:21    2008

The script resembles the code in example 1. Again we assign a variable to a file, this time "c:\temp\out.txt", which does not exist yet.
Again we create a filehandle with "open OUT", but then we see ">$out", which means by the symbol ">" that we are opening a file for write.
This is Perl, which at times can be a bit "cryptic", but from OS shells you may recall that the ">" means that you want to redirect the output (to a file
Then, in a "for loop", we print to OUT (the filehandle), some values.

Again, important is the addition of > to the filename. This opens it for writing. If we want to print to the file, we simply just specify the filehandle nam
which is the "entry point" to the file.
Also note the "\n" as the last symbol of what we want to print. It means "newline", and that makes sure we hop to the next line in the file.


>>>> 1. Some theory, before we move on: <<<<

Please read these notes first, before going to the next example (they are important).

Note 1: warnings and errors

If you run your script, and "something goes wrong" then you may get error messages, or you may get nothing. But you can always ru
perl -w progname
at the prompt. This will display warnings and other (hopefully) helpful messages before it tries to execute the program. To run the pr
perl -d progname

Note 2: variables

There are three common data types for variables we use in Perl:

1) Scalar:

The first one, and most commonly used, is a scalar. A scalar holds a single string, or number.
In this example: $message = "HELLO WORLD!"; contains a string.

As another example, the statement
 $priority=9;
sets the scalar variable $priority to 9

Or this example:
 $priority="high"
sets the scalar variable $priority to contain the string "high".

But this is possible too:
 $a = 1 + 2;              # Add 1 and 2 and store in $a

Sometimes you can encounter the "my" keyword in front of a variable:
 my $data_file = '/tmp/data.txt';

This is used in subfunctions (subroutines, or functions), where you want to denote that this variable is local to that subfunction. It's o
It ensures that the value can't be overwritten from outside the sub, which can be a neccessity sometimes.

2) Array:

The next type of variable is called an array. An array is prefixed by a @ instead of $.
An array holds a LIST of information as opposed to a scalar which can hold a single string.
 @food = ("apples", "pears", "eels");
 @sounds = ("whistle", "flute");

assigns a three element list to the array variable @food and a two element list to the array variable @music.
The array is accessed by using indices starting from 0, and square brackets are used to specify the index. The expression
 $food[2]
returns eels. Notice that the @ has changed to a $ because eels is a scalar.

>> If you want to add an element to an array, use "push":
 push (@food, "eggs");

which adds "eggs" to the end of the array.

>> If you want to remove an element of an array, use "pop":
To remove the last item from a list and return it use the pop function. From our original list the pop function returns eels and @food now has two e

 $grub = pop(@food);                          # Now the scalar variable $grub contains "eels"

What we have seen in example 1, that is opening a file and print the contents, could also be done using an array

 $file = "c:/temp/sample.txt";                #   Name the file
 open(INFO, $file);                           #   Open the file
 @lines = <INFO>;                             #   Read it into an array
 close(INFO);                                 #   Close the handle
 print @lines;                                #   Print the contents of the array


3) Hash:

The last type of variable is called a hash. This one is a bit vague at first, and everyone needs some time to let this one "sink in".
We will deal with hashes later, where it will be better understood.

Note 3: The special array @ARGV:

There is one special array: @ARGV: Command Line Arguments
Take a look at the following code in the sample script "C:\TEMP\test_args.pl"

foreach (@ARGV) {
        print "$_\n";
}

The array "@ARGV" gets filled with the values you have given along with the call to your script.
So let's try "test_args.pl" as follows:

C:\TEMP> perl test_args.pl Hello Johnny Woo
Hello
Johnny
Woo

So this scripts prints the array @ARGV which was filled by the command line arguments.

As another way to get "arguments" into the program, through the commandline, is illustrated by this:

C:\TEMP> perl myscript.pl finance.txt balance.txt

Where "myscript.pl" contains code as:

while (<>) {
        print;
}

Note 4: open a file for read or write in the "neat" way.

In examples 1 and 2 we have seen how we can open a file for read, or for write.
In general, we can make that look better by using a slightly different syntax.

To read or write to a file, you have to open a File-Handle.

open(FileHandleName, "[>]{>]Path_To_Filename")
And that can be used in the following ways:

#open a file for reading
  open(fileIn, "counter.txt");
#open a file for writing to
  open(fileOut,">counter.txt");
#open a file for appending to
  open(fileOut,">>counter.txt");

Remember that the ">" or ">>" symbols determine that you are planning to write, or append, to the file.

Take a look at the following code. We have put it in "c:\temp\copy.pl".

  open(infile, "c:/temp/sample.txt");
  open(outfile, ">c:/temp/sample2.txt");
  while (<infile>) {
    print outfile "$_\n";
  }
  close(outfile);

This is the same "c:\temp\sample.txt" file, as used in Example 1.
Let's run the code:
C:\TEMP>perl copy.pl
C:\TEMP>
C:\TEMP>type sample2.txt
hello appie

goodmorning, would you like some coffee?

Yes please!

C:\TEMP>

Looks like we have one newline too many. Indeed, if we omit the "\n" in the code, the file "sample2.txt" is exactly identical to "sample.txt".
Note that you sometimes need to experiment somewhat, in order to arrive at the desired result.
Also note that we have introduced the close() function, by which we have "closed" the output filehandle.

Note 5. STDIN and STDOUT

Just like in a unix, in Perl "stdin" corresponds to the keyboard, and "stdout" is your console (terminal).
They are also filehandles. Take a look at the following code:

  print "Please tell me your name: ";
  $name=<STDIN>;
  print "Thanks for telling me your name, $name !\n";

In this case, $name gets what you type on the keyboard until you press Return.
You know that <> can be used to read from a filehandle,
So we are reading from the STDIN filehandle. The value is assigned to $name and printed.
So, this can also be a nice way to get user input interactively.

We have put the upper code in the file "C:\TEMP\std.pl", and let's run it:

C:\TEMP>perl std.pl
Please tell me your name: Albert
Thanks for telling me your name, Albert
 !

C:\TEMP>


Note 6: Regular Expressions RE

This in part has to do with "string matching" and "string substitution", which can be very important if we need to manipulate (or transform) a file.
6.1 Matching:

A regular expression is contained in slashes, and matching occurs with the =~ operator. The following expression is true if the string the appears i

    $sentence =~ /the/                                      # means: if the contents of $sentence contains "the", then it's t

The RE is case sensitive, so if
 $sentence = "The steamboat from Spain";

then the above match will be false. The operator !~ is used for spotting a non-match. In the above example
  $sentence !~ /the/

is true because the string the does not appear in $sentence.

We already have seen the usage of "$_" in example 1 and example 2.
It acts like a sort of "default" variable. Sometimes you do not declare anything at all, and "automatically", $_ will be used (where a scalar like a strin
This also contributes that Perl code can be very compact.
Take a look at this:

if ($sentence =~ /boat/)
{
            print "We're talking about steamboats here\n";
}

which would print out a message if we had $sentence to contain:
 $sentence = "The steamboat from Spain";

But the code gets more compact if we assign the sentence to the special variable $_ which is of course a scalar. If we do this then we can even (th
the match and non-match operators and the above can be written as:

if (/boat/)                                                              # it's "automatically understood that $_ is the variable
{
                 print "We're talking about steamboats here\n";
}

You probably need some time to get used to these practices, but its simply the way how Perl "thinks".
Let's take a look at substitution.

6.2 Substitution:

If you are familiar with the vi editor, you probably know that you can substitute text in your file with the command "s/old_string/new_string/g".
Since Perl has borrowed many nice features from stuff like "C" and unix shells, this type of substitution syntax can also be found in Perl.

    $sentence =~ s/boat/ship/

will replace "boat" to "ship" if $sentence = "The steamboat from Spain";

and ofcourse we can use the $_ variable again:

    s/boat/ship/


6.3 The split function

Before returning to examples, we need to introduce the "split()" function, which can be of great help with string manipulations.
With split(), you can rapidly fill an array from a variable containing text, which would lend itself to be array members.
It works like this:

    $var = "Cinthia:Marlon:Harry:Albert";
    @persons = split(/:/, $var);

which has the same overall effect as
 @persons = ("Cinthia", "Marlon", "Harry", "Albert");

If we have the information stored in the $_ variable then we can just use this instead
 @persons = split(/:/);

So if you have a scalar containing text, seperated by some symbol like ":", you can immediately create a usefull array from it.

6.4 The chomp() function.

The chomp() function will remove (usually) any newline "\n" character from the end of a string. The reason we say usually is that it actually
removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.
Use the chomp() function like in this example:
chomp($text);


7. Subs

As the last note, we need to introduce subfunctions, or simply called subs.
You can place one or more subroutines in your code, which might do some specialized work. Once the subroutine is ready, you can call the routin
The structure is as follows:

sub mysubroutine
{
  print "I can do some specialized work\n";
}

Now you can call it in your code with:

 &mysubroutine

Note that you precede the call to the routine with the " & " symbol.

After digesting those 7 notes, we are ready to explore some more examples.


Example 3.

We will take a look at an example of file manipulation , where we want to alter the format of a file.
Suppose in C:\TEMP we have the file named "baddata.txt", and with the following content:

C:\TEMP>type baddata.txt
item | bycicle | in stock
item | computer | not present
item | book | in stock

C:\TEMP>

This time, we are not happy with those " | " symbols, and we want to get rid of them.
Now, don't say that you will grab your editor and do a search/replace, because we like to do it with a Perl routine.

At least we know by now how to read in the file, and put the contents, for example, in an array.
Then we could "process" the contents, and at first we will simply do no more that printing the arry.The following code in "C:\TEMP\example3.pl" wi

 $data_file="c:/temp/baddata.txt";
 open(DAT, $data_file) || die("Could not open file!");
 @raw_data=<DAT>;
 print @raw_data;
 close(DAT);

If we run that, we see that printing the array works, and this is what we see:

C:\TEMP>perl example3.pl
item | bycicle | in stock
item | computer | not present
item | book | in stock

Well, we need to kill those "|" pipe symbols, so we need a way to loop through each "record" and do the right processing.
we can use a loop to cycle through the content of the "@raw_data array", grab the variables we want, and use them. This is commonly done with a
foreach $line_var (@raw_data)                               # means: for each record in the array, at each loop, put it in $l
{
  commands...
}

So, the $line_var is a variable to represent each line in the array.
Now we need to do something inside the loop to split each line into variables we can use. This could be a job for the split() function, we have seen
Before we invoke the split though, we will want to chomp the \n character off the end of each line:

foreach $line_var (@raw_data)
{
  chomp($line_var);
}

Now we are ready to use the split method to create the variables we need each time through the loop. Since we used the pipe symbol as the sepa
that is the character we will use to split the data. Notice that the pipe symbol needs to be escaped with a \ character since it is a special character

foreach $line_var (@raw_data)
{
  chomp($line_var);
  ($v_item,$v_object,$v_present)=split(/\|/,$line_var);                                # remember split works like split( / symbol_to_split_on / ) and w
}

Now we can print the sentence using the variables we created, and it will print the sentence for every line_var from raw_data..

foreach $line_var (@raw_data)
{
  chomp($line_var);
  ($v_item,$v_object,$v_present)=split(/\|/,$line_var);
  print "The $v_item with the name $v_object is $v_present\n";
}

So, what we have now as our code is:

$data_file="c:/temp/baddata.txt";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;

foreach $line_var (@raw_data)
{
  chomp($line_var);
  ($v_item,$v_object,$v_present)=split(/\|/,$line_var);
  print "The $v_item with the name $v_object is $v_present\n";
}

close(DAT);

Well, let's try our code:

C:\TEMP>perl example3.pl
The item with the name bycicle is in stock
The item with the name computer is not present
The item with the name book is in stock
The   with the name is

C:\TEMP>

The last line of the output is caused by the fact that after the 3rd record in "baddata.txt", there is an empty line. If we remove it, we only see our 3 r
records printed.
So, we did the job allright !


Example 4.
Again we want to alter the content of a text file. This time, we want to alter all occurences of "string1" to be replaced by "string2".
Suppose we have the textfile "C:\TEMP\text1.txt", which contains the following data:

C:\TEMP>type text1.txt
The boat was sailing on open water.
Then suddenly, the captain saw an island on the horizon.
He turned the steering wheel, and the boat sharply went
starboard, and took a course towards that mystereous island.

C:\TEMP>

As you can see, this text contains the word "boat". We want to replace it with "ship".
Let's see if this code can do the job. Create a new script file in C:\TEMP and paste the code below into that file.
Give this script the name "chstring.pl"

#!/usr/local/bin/perl
#
# change all occurances of a string in a file to another string
#

if ($#ARGV != 3) {
    print "usage: chstring oldfile newfile oldstring newstring\n";
    exit;
}

$oldfile = $ARGV[0];
$newfile = $ARGV[1];
$old = $ARGV[2];
$new = $ARGV[3];

open(OF, $oldfile);
open(NF, ">$newfile");

# read in each line of the file
while ($line = <OF>) {
    $line =~ s/$old/$new/;
    print NF $line;
}

close(OF);
close(NF);

You should be able to understand this code by now. So let's try this one:

C:\TEMP>chstring.pl text1.txt text2.txt
usage: chstring oldfile newfile oldstring newstring

C:\TEMP>chstring.pl text1.txt text2.txt boat ship

C:\TEMP>type text2.txt
The ship was sailing on open water.
Then suddenly, the captain saw an island on the horizon.
He turned the steering wheel, and the ship sharply went
starboard, and took a course towards that mystereous island.

C:\TEMP>

Because of the line "#!/usr/local/bin/perl" as being the first line in our code, we can call the script directly, that is, we do not need the "perl scriptnam
In our first attempt to run the code, we gave the program the wrong number of arguments. So the script exits and prints how to use the program in

if ($#ARGV != 3) {
    print "usage: chstring oldfile newfile oldstring newstring\n";
    exit;

So that piece of code works.
In our second attempt we supplied the right number of arguments to the program.
C:\TEMP>chstring.pl text1.txt text2.txt boat ship

Here we tell the program that the new file will be called "text2.txt" and that we want to replace the word "boat" with "ship".

Again, we did the job allright !


>>>> 2. Some More theory, before we move on: <<<<

First, let's sum up a few of usefull other functions and syntaxes. Some we have already seen, and others might be new:
Just browse trough the list, and see which ones makes sense to you.

Purpose:                                                                 Syntax or Example:
Find the length of a string:                                              $length = length($string)

Subroutine for sorting an array:                                         sub get_array
and returns the sorted array                                             {
                                                                           @my_array = (23, 13, 43, 56, 72, 1, 10);
                                                                           @sorted = sort(@my_array);
                                                                           return @sorted;
                                                                         }

sort(@arr)                                                               @data = ('oranges', 'peaches', 'grapes', 'apples', 'lemo
function can be used to sort the elements
of an array or hash.                                                     # sort alphabetically
                                                                         @sorted = sort(@data);
                                                                         print "@sorted ";

                                                                         Output:
                                                                         apples grapes lemons oranges peaches

adds a new element to the end of an array                                @data = ('a', 'b', 'c');

                                                                         # add element
                                                                         push(@data, 'd');
                                                                         print "@data";
                                                                         Output:
                                                                         a b c d

removes an element from the end of the array.                            @data = ('a', 'b', 'c');

                                                                         # remove last element
                                                                         pop(@data);
                                                                         print "@data ";
                                                                         Output:
                                                                         a b

adds a new element to the                                                @data = ('a', 'b', 'c');
beginning of an array.
                                                                         # add element
                                                                         unshift(@data, 'z');
                                                                         print "@data ";
                                                                         Output:
                                                                         z a b c

removes an element from the beginning                                    @data = ('a', 'b', 'c');
of the array.
                                                                         # remove element
                                                                         shift(@data);
                                                                         print "@data ";
                                                                         Output:
                                                                         b c
split($delim, $str)                         # define string
decomposes a string by splitting it on      $str = "cat,hog,dog,log";
delimited $delim
and returns the individual components       # split string on comma delimiter
as elements of a                            @words = split(",", $str);
numerically-indexed array.                  foreach $w (@words) {
These elements can then be processed        print "$w\n";
in a loop.                                  }

                                            Output:
                                            cat
                                            hog
                                            dog
                                            log

Other example:                              $data_file="c:/temp/baddata.txt";
Read in a certain file which contains       open(DAT, $data_file) || die("Could not open file!");
an unwanted character. In this example      @raw_data=<DAT>;
it is the "|" symbol, but in can
be any other symbol.                        foreach $line_var (@raw_data)
                                            {
                                              chomp($line_var);
                                              ($v_item,$v_object,$v_present)=split(/\|/,$line_var);
                                              print "The $v_item with the name $v_object is $v_presen
                                            }

                                            close(DAT);

join($sep, @arr)                            # define array
joins the various elements of an array      @data = ("Harry", "Joan", "Tom");
into a single string,
using the value of $sep to separate them    # create string from array
from each other.                            print join(" and ", @data) . " are friends";

                                            Output:
                                            Harry and Joan and Tom are friends

Read in a file, and change "old_string"     # read in each line of the file
in "new_string"                             while ($line = <OF>) {
For example, to change "boat" in "ship",        $line =~ s/$old/$new/;
s/boat/ship/                                    print NF $line;
                                            }

Translate (tr) some characters into other   # read in each line of the file
characters.                                 while ($line = <OF>) {
This example translates each 'a' and 'b'        $line =~ tr/ab/cd/;
into an 'e' and 'f'                             print NF $line;
                                            }

for loop in "C" style:                      for($I = 0; $I <= $#_; $i++)
                                               { commands }

                                            for ($i = 0; $i < 10; ++$i)
                                               { commands }

Example while loop, and                     print "Password? ";         # Ask for input
example of user input:                      $a = <STDIN>;               # Get input
                                            chop $a;                    # Remove the newline at end
                                            while ($a ne "secret")      # While input is wrong...
                                            {
                                                print "sorry. Again? "; # Ask again
                                                $a = <STDIN>;           # Get input again
                                                chop $a;                # Chop off newline again
                                            }
Special characters in RE                                             .                    #   Any single character except a newli
                                                                     ^                    #   The beginning of the line or string
                                                                     $                    #   The end of the line or string
                                                                     *                    #   Zero or more of the last character
                                                                     +                    #   One or more of the last character
                                                                     ?                    #   Zero or one of the last character

Example matches:                                                     t.e                  #   t followed by anthing followed by e
                                                                                          #   This will match the
                                                                                          #                   tre
                                                                                          #                   tle
                                                                                          #   but not te
                                                                                          #           tale
                                                                     ^f                   #   f at the beginning of a line
                                                                     ^ftp                 #   ftp at the beginning of a line
                                                                     e$                   #   e at the end of a line
                                                                     tle$                 #   tle at the end of a line
                                                                     und*                 #   un followed by zero or more d chara
                                                                                          #   This will match un
                                                                                          #                   und
                                                                                          #                   undd
                                                                                          #                   unddd (etc)
                                                                     .*                   #   Any string without a newline. This
                                                                                          #   the . matches anything except a new
                                                                                          #   the * means zero or more of these.
                                                                     ^$                   #   A line with nothing in it.

                                                                     [qjk]                #   Either q or j or k
                                                                     [^qjk]               #   Neither q nor j nor k
                                                                     [a-z]                #   Anything from a to z inclusive
                                                                     [^a-z]               #   No lower case letters
                                                                     [a-zA-Z]             #   Any letter
                                                                     [a-z]+               #   Any non-zero sequence of lower case

use strict                                                           You might see programs where this command is the first (
                                                                     line of the program.
                                                                     You already know that Perl is very flexible in variables
                                                                     A command like "use strict" is called a pragma. Pragmas
                                                                     to do something special when it runs your program. use s
                                                                     to write bad software: It makes you declare all your var
                                                                     and it makes it harder for Perl to mistake your intentio

These operators are used to compare                                  == Equal to
two numbers, but not to compare strings                              != Not Equal to
                                                                     > Greater than
                                                                     < Less than
                                                                     >= Greater than or Equal to
                                                                     <= Less than or Equal to

String Comparison                                                    eq    Equal to
                                                                     ne    Not Equal to
                                                                     gt    Greater than
                                                                     lt    Less than
                                                                     ge    Greater than or Equal to
                                                                     le    Less than or Equal to




Let's go back to the examples.


Example 5:

Here are a few short codes, found on the Net, which you could try out.
sample 1: try again getting user input via STDIN, and usage of IF.. ELSEIF

                 print "Please tell me your name: ";
                 chop ($name=<STDIN>);

                 print "Please tell me your nationality: ";
                 chop ($nation=<STDIN>);

                 if ($nation eq "British" or $nation eq "New Zealand") {
                            print "Hallo $name, pleased to meet you!\n";

                 } elsif ($nation eq "Dutch" or $nation eq "Flemish") {
                            print "Hoi $name, hoe gaat het met u vandaag?!\n";

                 } else {
                                 print "Ohoh I don’t speak your language\n";
                 }

Also note the comparision operator which was used as " $nation eq "Dutch"

sample 2: how to split an array in a new array from member "n" to member "m".

                 @names=("Muriel","Sarah","Susanne","Gavin");

                 &look;

                 @middle=splice (@names, 1, 2);

                 &look;

                 sub look {
                         print "Names : @names\n";
                         print "The Splice Girls are: @middle\n";
                 }

This was a new trick.

sample 3. Exercise with a Regular Expression

                 print "What do you read before joining any Perl discussion ? ";
                 chomp ($_=<STDIN>);

                 print "Your answer was : $_\n";

                 if ($_=~/the faq/) {
                          print "Right ! Join up !\n";
                 } else {
                          print "Begone, vile creature !\n";
                 }

Remember that you could also have used "if (/the faq/)"

sample 3. Substitute text. Not always that simple.

                     $_='Us ? The bus usually waits for us, unless the driver forgets us.';

                     print "$_\n";

                     s/Us/them/;        # operates on $_, otherwise you need $foo=~s/Us/them/;

                     print "$_\n";

I have pasted that code in "C:\TEMP\repl.pl". Let's try it.
C:\TEMP>perl repl.pl
Us ? The bus usually waits for us, unless the driver forgets us.
them ? The bus usually waits for us, unless the driver forgets us.

Not all "us" occurences were replaced. Actually, this is by design, because substitution is case sensitive.
And what if we try this in the code "s/Us/them/i". Note that we have added the "I" of case-insensitive.

C:\TEMP>perl repl.pl
Us ? The bus usually waits for us, unless the driver forgets us.
them ? The bus usually waits for us, unless the driver forgets us.

Still no succes. Now we try this "s/Us/them/ig". Note that we have added the "g" of global.

C:\TEMP>perl repl.pl
Us ? The bus usually waits for us, unless the driver forgets us.
them ? The bthem themually waits for them, unless the driver forgets them.

Ok, success, but maybe you realize that there is actually too much substitions made.
So when you substitute a string that can be part of another string, ugly surprises can pop up.

So, what is the solution to this problem?
Found a bug? Found incorrect information?
Please report to:
albertvandersel@zonnet.nl
Thanks.
uction to Perl (in the mode of a "classical" scripting language).


ary. But since it is supposed to be "a really quick intro ", it might even be useful.
 o, I hope you like this one.




, wether it is a mainframe, mini, Server or PC.

that Perl is mostly used in creating (small) powerfull scripts.
manipulation where the format of those files gets altered to a desired format.
e (for example, running on Windows, or Solaris, or AIX etc..),



 to all sorts of "Web-techniques" (e.g.CGI) as well.

ity of Perl is this: if you install a major application like Oracle on your system,

are that you must have perl availble on your system, before you can even


ll it a "scripting language". Once you have installed Perl on your system, you have
e scripts are flat txt files (ascii), containing Perl code.

because perl can also be used in compiling code. What we mean here,
g permanent objectcode or executable on disk.
ng in the perl runtime.

but that's not mandatory.




 calling the script will put Perl at work.




ht path to perl, because I have only used some example paths here.




hen you can call your script as in:


ut even if you use an older version like 5.6, it's OK.

e going to take a look at some traditional perl scripts, but we don't consider all the modern
 just about the very basic (classical) stuff.
 Studio). Here we just use an editor like vi on unix, or notepad or textpad on Windows.




while there are still lines left in the file,

an assign a file to $var, no problem.
ment, you have an "entry" to the file, and you


ybe sample.txt is not there), we exit (die) the script with "yourmessage".


 ay up to the newline, and put it into "$_".
 akes sure we process all lines in the file.


 ard slash "/" in the path to sample.txt, which Perl accepts.
mp\out.txt", which does not exist yet.
 mbol ">" that we are opening a file for write.
means that you want to redirect the output (to a file, mostly).


o the file, we simply just specify the filehandle name. You print to that filehandle,

s sure we hop to the next line in the file.




or you may get nothing. But you can always run the program with warnings using the command

 it tries to execute the program. To run the program with a debugger use the command




 this variable is local to that subfunction. It's optional, but can be handy at times.




 he index. The expression
p function returns eels and @food now has two elements:




me time to let this one "sink in".
e2.txt" is exactly identical to "sample.txt".




t if we need to manipulate (or transform) a file.
owing expression is true if the string the appears in variable $sentence.

sentence contains "the", then it's true, otherwise it results in false.




atically", $_ will be used (where a scalar like a string is appropriate).




course a scalar. If we do this then we can even (this is Perl!) avoid using


ly understood that $_ is the variable involved




h the command "s/old_string/new_string/g".
tution syntax can also be found in Perl.




elp with string manipulations.
create a usefull array from it.



he reason we say usually is that it actually




ce the subroutine is ready, you can call the routine as required.




 .The following code in "C:\TEMP\example3.pl" will just do that.




 do the right processing.
want, and use them. This is commonly done with a foreach loop:
he array, at each loop, put it in $line_var




ould be a job for the split() function, we have seen in note 6.3.




loop. Since we used the pipe symbol as the separator,
d with a \ character since it is a special character in Perl:




plit works like split( / symbol_to_split_on / ) and we need to "escape" the "|" with a "\"


very line_var from raw_data..




an empty line. If we remove it, we only see our 3 reformatted
ng1" to be replaced by "string2".




 directly, that is, we do not need the "perl scriptname" syntax.
e script exits and prints how to use the program in a correct way.
 word "boat" with "ship".




13, 43, 56, 72, 1, 10);




 'peaches', 'grapes', 'apples', 'lemons');
e) || die("Could not open file!");




,$v_present)=split(/\|/,$line_var);
 with the name $v_object is $v_present\n";




 @data) . " are friends";




         #   Ask for input
         #   Get input
         #   Remove the newline at end
         #   While input is wrong...

         # Ask again
         # Get input again
         # Chop off newline again
 Any single character except a newline
 The beginning of the line or string
 The end of the line or string
 Zero or more of the last character
 One or more of the last character
 Zero or one of the last character

 t followed by anthing followed by e
 This will match the
                 tre
                 tle


 f at the beginning of a line
 ftp at the beginning of a line
 e at the end of a line
 tle at the end of a line
 un followed by zero or more d characters
 This will match un
                 und
                 undd
                 unddd (etc)
 Any string without a newline. This is because
 the . matches anything except a newline and
 the * means zero or more of these.
 A line with nothing in it.

 Either q or j   or k
 Neither q nor   j nor k
 Anything from   a to z inclusive
 No lower case   letters

 Any non-zero sequence of lower case letters

ams where this command is the first (or second line, directly after #!/path_to_perl)

at Perl is very flexible in variables.
 strict" is called a pragma. Pragmas are instructions to the Perl interpreter
cial when it runs your program. use strict does two things that make it harder
re: It makes you declare all your variables (``strict vars''),
der for Perl to mistake your intentions when you are using subs (``strict subs'').

								
To top