- [[#About Perl]] - [[#What goes in a Perl File]] - [[#The String value in Perl]] - [[#Scalar Variables in Perl]] - [[#Using Scalar Values in Perl]] - [[#Arrays (Lists)]] - [[#Hashes (also known as Objects or Collections)]] # About Perl Perl stands for **P**ractical **E**xtraction and **R**eporting **L**anguage. It is a server-side language, which employs scripting "behind the scenes". With an HTML file the process is as follows; - User’s computer asks for the HTML file. - The Server sends the HTML file. - The User’s computer interprets the HTML file. - Sometimes the HTML file tells the User’s computer to ask for more files, like CSS or Icons. This is simple and straightforward because the HTML file already exists. The challenge is that HTML files are static, and cannot respond to any information the User needs to supply. It never changes. On the other hand Perl (and other server-side scripts) is good for when the HTML file needs to be created automatically based on what the User asks for. The HTML file doesn't already exist, but Perl makes an HTML file and sends it back to the User. The User never knows the HTML file didn't already exist. *It is an important step in greater interactivity with your Users.* An example process would be as follows; - The User’s computer asks for the Perl file, usually a .cgi extension - Sometimes the User also sends additional information with the request - The Server finds the Perl file and runs the program. - If there was more information sent from the User or saved on the server, it adds that information. - If you tell it to, the Perl creates a document that acts like HTML. - The Server sends that document back to the User - The User’s computer interprets the HTML file. - Sometimes the file your Perl script made tells the User to ask for more files, like CSS or Icons. # What goes in a Perl File First, you need to tell the server what type of language you’re using.  We do this with the **shebang**. ``` perl #! /usr/bin/perl ``` Every statement must end with a **semicolon** to say “this is the whole command”. ``` perl print "I would buy you a monkey"; ``` Every statement is either successfully completed or not.  If it is successful, it returns “true”, or in computer-speak, 1.  If it is not, it returns “false”, or in computer-speak, 0. ``` perl dance like a monkey;  # returns 0, Perl doesn’t have a “dance” command. print "Haven’t you always wanted a monkey?";  # returns 1 ``` # The String value in Perl When discussing text, we call collections of text characters **strings**. Strings can be resolved using quotations or double-quotations, but they do different things. - A string with ‘single quotations’ renders exactly as it looks. - Makes it easy to know what to expect - Can’t use apostrophes without it breaking the string - Can’t render whitespaces or invisible characters well - A string with “double quotations” renders extra information included in it. - You can sneak extra things into it like variables - Can use apostrophes without breaking the string. - Can render whitespaces using the escape character `\` such as a newline `\n` ## Examples ``` perl print 'Dance\nMonkey'; # Dance\nMonkey print "Dance\nMonkey"; # Dance # Monkey ``` # Scalar Variables in Perl In algebra we see statements like `2n + 4`.  In that example, `n` is a **variable** that can be substituted with any numerical value. The same concept applies to variables in computing.  In Perl, **Scalar** variables can hold many different types of data, the simplest being; - Strings - Numbers - Booleans (true or false statements represented as zero (false) or one (true) A scalar is any value preceded by a dollar sign.  You can name a scalar anything you want.  Some possible examples are below; - `$input` - `$message` - `$CaseMattersAndLongRidiculousNamesAreAllowed` Values can be assigned to a scalar with the equal sign; - `$input = 4572;` - `$message = “You are a kind and caring person.”;` - `$isTrue = 1;` # Using Scalar Values in Perl Scalar values can be interpolated (substituted) directly into double-quoted strings; ``` perl print "My name is $userName."; ### if $userName = “Zeus”, the result would say My name is Zeus. ``` Scalar values can also be concatenated (added to) single-quoted strings; ``` perl print 'My name is ' . $userName; ### will produce the same result as the previous example. ``` Scalar values can also be added to statements using the print-function command `printf`. ``` perl printf('My name is %s', $userName); ### will produce the same result as before. ``` - The `%s` indicates that `$userName` will be placed into the statement as a string (not a number or other form). Scalar values that are numeric can be used in mathematical calculations; ``` perl $decimal = $numerator / $denominator; ``` Scalar values can be checked for truthiness. ``` perl $isBigger = 23 > 12; print '23 is bigger than 12' if $isBigger; ### will print because $isBigger is true ($isBigger = 1) ``` # Arrays (Lists) Many scalars can be stored inside lists, oftentimes referred to as **arrays**.  Arrays are identified with the at-symbol prefix (`@`). Arrays can be populated by scalars of any type or value ... even “undefined” meaning they aren’t any kind, and are waiting for information; ``` perl @myList = ('A String', 4572, 0, $unknownValue, undef); ``` To access a particular scalar inside an array, you will use its **scalar reference**, which is it's name with a `
instead of an `@`. To point to a specific value inside it, use square-bracket notation and identify the **index** of the scalar (how far way from the front/top of the list the value is, so the first entry is always `0`) ``` perl print $myList[0]; # produces 'A String' from the Array defined above $total = $myList[1] + $myList[2]; #total will equal 4572 ``` If you need to do something with every scalar in an array, you can use a `for` loop; ``` perl for $thisOne (@myList) { print $thisOne . "\n"; } ``` The output for the `for` loop shown above would be as follows; ``` A string 4572 0 I made this up Error: undefined value. ``` # Hashes (also known as Objects or Collections) Many scalars can be stored in specialized lists called **hashes**. The difference between an *array* and a *hash* is that in a hash each *variable* is given it's own name. We call these names **keys**, and those keys "unlock" their **value**. Hashes are identified with the `%` prefix, and its inner contents can be accessed from its *scalar reference* (the `
) followed by the *key* quoted inside curly brackets `{ }`. ``` perl %FORM = (); # starts the hash $FORM{'noun1'} = 'apple'; $FORM{'verb1'} = 'sing'; print $FORM{'noun1'} . "\n"; # apple print $FORM{'verb1'} . "\n"; # sing ```