PERL

The is the introductory chapter of the PERL tutorial. If you are new to PERL then this is defintely a good place to start.
  •  Introduction
  •      What Is PERL
  •      PERL and HTML
  •      Perl Basics
  •      Servers,PERL and CGI.PM.
  •      How To Obtain A PERL Interpreter
  •      Installing A Personal Server
  •      Running test scripts on your Windows server.
  •      Forms (Getting data from visitors to your site.)
  •      Enviroment Variables
  •      Getting Data into the Script.
  •      Beginning operations with Scalars
  •      Conditionals and Loops.
  •      Arrays.
  •      Subroutines
  •      Hashes
  •      Analyzing and Manipulating Data
  •      Using Perl and HTML to Format Text
  •      Security
  •      Inputting and Outputting Data from Files and Directories.
  •      Uploading Files.
  •      Debugging.
  •      Unix Permissions
  •      Unix Basics.
  •      Resources for PERL.
  • Choose a link or scroll down the page

    1.001

    What is Perl ?

    Perl was developed in 1986 by Larry Walls to create reports for a company he worked for. Perl was not really designed for the web ,insomuch that it was found that the two sort of went togeather like coffee and donuts. It is said that PERL stands for Practical Extraction and Report Language.

    Since its creation Perl has become a very popular programming language among web developers .It has evolved into a powerful full featured programming language though only parts of this robust language are used for web development.

    You will be hard pressed to find another web development tool that has more support,public code,and followers as Perl does. It has matured and been added to and updated along the way to become the popular language its become.

    Its an easy to learn language and you will find few arguments among site designers about the benefits of adding it to your skills as a web site designer.

    Perl is used for a wide variety of things but this tutorial will focus only on its popular use as a tool to make web sites interactive.

    Perl scripts can process forms,create guestbooks,web-based bulletin boards,count page hits,and a lot more.

    Perl is well suited to Web-related tasks for three reasons.

    1. It is a powerful text manipulation tool.
    2. Perl is easily ported,(moved from one platform to another).
    3. Perl is a friendly easy to use programming language.
    Home

    Table of Contents


    1.002

    Perl and HTML.

    Perl and HTML make good partners.

    HTML allows you to create links and forms that access or activate your Perl scripts.

    Perl allows you to generate HTML code in the background of your web pages which will display the result of the script to visitors at your site. Pretty cool , huh?

    Examples will follow.

    If you need to review HTML visit this link. HTML Primer

    Home

    Table of Contents


    1.003

    Perl Basics

    .
    Statements

    Often computer languages are compared to human languages. The similarities stem from the communicating syntax elements of each.

    A typical written sentence contains various syntax elements as does a line of computer instruction. A computer has its "nouns" (variables),"verbs"(commands),and "objects"(operands and arguments).

    A computer program is a series of line statements that instruct the cpu to perform a certain function.The program lines can instruct the computer to perform an action, or simply provide information related to the program.

    The elements of each statement are the functions and expressions.Again if you compared the computer statement to a human language ,functions would be verbs, and expressions would be complex clauses,and nouns would be variables.

    Each statement in Perl ends with a semicolon,not a period.

    A working knowledge of human language grammar isnt neccesary to learn programming,but if you are familar with the grammar of human language then the comparison is helpful.

    Data

    Information is provided to a computer program in the form of data. Text,images,and instructions acted upon are all code data that the computer understands,namely ones and zeroes at the processor level.

    Programs hold this data for the computer in variables.Variables are places in memory (addresses) but you need not worry about how the computer decides where to store variable data. You merely specify a variable name within the program to represent or hold the value of some information until the computer needs to access it from within the program.

    In a nutshell,variables represent numbers,bits of text,words,expressions,and maybe even other variables.

    In Perl there are three types of variables.


    Scalar,List,and Hash Variables

    Scalars:   Scalar variables hold single values.Any value held in a scalar variable is considered to be a single,discreet unit.The scalar variable is indenified by the $ (dollar sign) in front of the variable name.

    $scalar=1

    This declares a scalar variable named scalar and assigns the value 1 to it.


    Lists:    Also called Array variables,list variables hold multiple values. A single unit of data,(an item) in a list variable is called a list element.

    Lists are idenified by the @ sign in front of the variable name. Scalars and Lists are related variables in that list variables contain scalar variables.

    Each individual item in a list variable is a scalar variable. You can access the single elements (scalars) within a list by referring to it as a scalar and referencing it by its index number that corresponds to its position in the list.

    Example: Let us say we have a list variable called @list and the third element can be idenified as $list[2].The third element is designated as 2 because pc's typically start counting at 0. Therefore 0=the first element ; 1= second element,2=third element, and so on.


    Hash     The third variable type is the hash. The hash is a special kind of list variable where the elements are related to each other in a specific way.

    Elements within a hash are grouped in pairs. Each pair contains a "key" and a "value".

    Values are accessed by referring to their associated key.

    %hash= [key1, value1], [key2, value2], [key3, value3]

    The key is usually the idenifier and any [value] can be extracted from the hash by referring to its key.

    so the value of:

    $hash[key2]

    would be value2.

    A more practical hash might look like this.

    %address= (
    	   "number" => "2135",
    	   "street" => "Elm St.",
    	   "City"   => "Cleveland",
    	   "State"  => "Ohio",
    	)

    The symbol => is a convention used in hash data to make the key/value pairs easier to see.It is synomynous with the comma.

    To extract the city from this hash you refer to it as

    $hash{"city"}

    this will represent the value Cleveland.

    Notice that each element of a hash is a scalar and is referred to it with the dollar sign, just as the list variable.


    Operators    Operators perform or describe some kind of relationship between two things. Some common operators used in programming are the mathematical operators.+, -, *, /, . There are more matematical operators then these basic ones but thats for a later section. Besides mathematical operators there are comparison operators such as = and +=,and increment and decrement operators such as ++ and --.

    One typical use of operators is to create or check a condition and have the program perform some operation if the condition matches or doesnt match some predetermined conditions.

    An easy condition for example is to compare two numbers to see if they are equal or not equal,or if one is bigger or smaller than the other.This comparision will return either a true or false value.

    Text strings can be compared for equality as well . Search tools and your find file programs use such operators to find string matches.

    More detail about operators will be introduced in the Conditional and Loops section.


    Functions    Functions are the heart and soul of programming. Functions are like operators with names,that perform some kind of operation on the data.They are named,case-sensitive,and always in lowercase letters.Generally they perform more complicated operations on the data then operators.

    An example of a Perl function is a print function that sends specified data to the specified output ,which may be a printer or file.

    Some functions work on arrays,some on hashes or scalars. Others may work differently depending on where you use them,or what you use them on.


    Handles    Handles are special keywords that help identify "something" as a target for certain functions.A file handle is the most common type of handle. It is a label that is linked to a filename. The label is referred to when operations such as opening,closing,reading and writing need to be performed on the file. Handles for directories can also be created for databases and network connections.


    Program Grammar     

    Home

    Table of Contents


    1.004

    This command will show the path and your current working directory.

    Use ls ; pwd to find out the current working directory and its contents.

    Home

    Table of Contents


    1.005

    Home

    Table of Contents


    1.006

    Redirect output to a new file.

    Home

    Table of Contents


    1.007

    Using Wildcards

    Wildcards act as placeholders for letters or words . If you are looking for a file and are not certain what the name is you can use a wildcard to search for that file. For example:

    ls this*

    This command would list all files beginning with "this"....thisfile.txt  thisprogram.exe ...and so on.

  • Use "?" to search for one character.
  • Wildcards act as placeholders for missing letters,or text.
  • You can put the wildcard at any place in a name: at the beginning: "*this" ;in the middle: "th*is" or at the end "this*".
  • These examples would search for all "type" files beginning with "this". To search for a type of file,use a wildcard like so; "*.txt"  would search for all ".txt"  file types and give a listing of them. "ls this*.txt" would list all text files beginning with "this".

    Home

    Table of Contents


    1.008

    Viewing File Contents with More.

    As you become more comfortable with the UNIX commands you will probably want to start probing a little deeper and believe me we have only scratched the surface.

    This help file shows you how to explore the contents of files,scripts,and programs as well as files that you have or intend on creating.The more command is the easiest way to view the contents of a file.This command instructs UNIX to display files onscreen one page at a time.

    Here is how you use the more command.

  • At the prompt,type in the more command and the name of the file you want to look at.
  • Press the spacebar to see the next screen.
  • Type "q" to return to the UNIX shell prompt.

    Here is how you type it in.

    more yourfilename

    Home

    Table of Contents


    1.009

    Displaying File Contents with Cat.

    You can use the cat command to display file contents however it displays the file/s all at once .You will only be able to read the last few lines. But a useful function of cat is that you can redirect file/s .

    type in

    1. cat yourfile.programs 
    2. cat youroldfile>yournewfile 
    3. The cat command is useful for redirecting files
    #1 This command lists yourfile.programs.

    #2 Redirects your file called youroldfile to another file called yournewfile.

    Home

    Table of Contents


    1.010

    Exploring the UNIX System

    Now that you have the basic commands you can start exploring your UNIX system. You will soon learn whats available and get the experience you need by entering commands.

    Don't worry about doing something that will cripple the UNIX system, its basically designed to be "breakproof".Explore to your hearts content.You wont hurt anything.

    The table below lists some of the more useful directories you will find on most UNIX systems.

    Enter the commands below to be on your way.

    1. cd /usr/bin

      This command changes your working directory to bin which is where most installed UNIX programs are.

    2. ls | more

      This command lists the programs in bin and pipes the output to more so can read the names one screen at a time.

    3. telenet

      Type in a program name.This program "telenet" will allow you to connect to another system and use it like you are using your UNIX now.

    4. You can get help on UNIX programs by typing man followed by the file name.See the next section for more information about UNIX help files.
    Home

    Table of Contents


    Table 1
    /bin /etc /home
    /sbin /temp /usr/bin
    /usr/local /usr/local/man /usr/share/man













    Back to Exploring Unix


    Help with the Man Command

    If you need help about a particular command or a topic, UNIX has a help file to assist you. Use the  man command,(which is short for manual). You can look up specific UNIX commands and related topics.

  • Accessing a Manual Page

    Bring up a manual page by typing in the manual command man plus -k and the command or topic you wish to see the manual for. I.E. 

    man -k commandname.

    This will bring up a list of possible man page names,command names and descriptions. Make a note of the man page and number.

    Below is a sample of the all the manual pages available for the command man -k passwd.

    chpasswd (8)               - update password file in batch.
    gpasswd (1)                 - administer the /etc/groupfile
    mkpasswd (1)               - generate new password, optionally apply it to user
    mkpasswd (8)              - update passwd and group database files
    passwd (1)                   - update a user's authenication token(s)
    passwd (5)                  - password file
    userpasswd (1)             -A graphical tool to allow users to change their passwords

    In the table there are 7 files listed . To access one of these, type in the man command ,

    then the manual page or section #, and the name of the command.

    Like so:

    man 5 passwd .

    Home

    Table of Contents


    Logging Out of UNIX

    To ensure that nobody else accesses your files,make sure you log out of Unix when you are finished.

    Logout by entering the logout command.

    logout

    Its that simple, just type in logout.

    Some systems allow you to use the commands, quit,or exit,or ctrl-d.

    Home

    Table of Contents