11-20-2003, 12:57 AM | #1 (permalink) |
Banned
Location: shittown, CA
|
So ya want to learn perl do ya?
I'm kinda on the bored side and since I have been picking up php lately I figured I'd follow in the spirit of the php newbies thread and make one for perl. Perl is often called the glue language because it is varey flexable and can be used in all sorts of situations for all sorts of purposes.
So lesson 1 - What makes a perl program? /Comments Code:
#!/usr/bin/perl # This is a comment # Any time you want to make a comment # you need to use the pound sign '#' # now the key to running any perl program is the first line. # #! (read as pound-bang) followed by the path to your perl executable. # /usr/bin on my system. # this lines tells the system what kind of file this is and what to do with it. |
11-20-2003, 10:04 PM | #2 (permalink) |
Banned
Location: shittown, CA
|
This lesson will cover the basic perl data type and how to print to the console. This basic unit is called a Scalar variable and it can hold integers(1, 2, 3, 4, ...), floats (3.1549...), characters(a, b, c, etc...) and strings("bob is a good dog."). Scalars are a very versitile datatype that will be commonly used.
Also of note is that perl like many other common languages uses a semi-colon ( ; ) to signal the end of the line. Missing semi-colons is the cause of untold bugs so if something at first does not work check to see if you forgot that little guy. Code:
#!/usr/bin/perl $myFirstVariable; # this is a empty variable it contains no data. $myFirstVariable = "Hello World!"; # I have assigned the string "Hello World!" to the variable. # You can also assign a value when you declare the variable like this $foo = "bar"; print $myFirstVariable . "\n"; # This outputs the string that is inside the $myFirstVariable out to the screen. # After the variable is a period this is the concatenation operator and it glues # the piece on the left with the part on the right, in this case "Hello World!" and "\n". # Lastly "\n" is the newline escape sequence. |
11-21-2003, 12:50 PM | #5 (permalink) |
Darth Papa
Location: Yonder
|
Neat idea, juan!
I want to contribute to this! Give me a topic, I'll write a tutorial or two! Arrays, maybe? And introducing function calls by way of the various array operations? That might be a few tutorials from now, though. Ooh, ooh, and I'd LOOOOVE to write an introductory tutorial on regular expressions! I've dispelled about a dozen cases of severe regex-phobia so far. |
11-21-2003, 04:23 PM | #6 (permalink) |
Banned
Location: 'bout 2 feet from my iMac
|
rat: if you can SLOWLY CLEARLY walk me throug reg expressions, I will love you forever. really, I will. your wife will understand, I'm sure. it doesn't even have to be perl-related, just in general. i have such a hard time w/ em *pout*
|
11-21-2003, 04:28 PM | #7 (permalink) | |
Banned
Location: shittown, CA
|
Quote:
rat: hit me up (AIM, email, PM etc) I got a rough idea of how I want lay out pieces of the language. But Regular Expressions is ALL yours |
|
11-22-2003, 05:35 AM | #8 (permalink) |
A Real American
|
thanks to all who have and will post these...very nice to have an interactive learning source.
__________________
I happen to like the words "fuck", "cock", "pussy", "tits", "cunt", "twat", "shit" and even "bitch". As long as I am not using them to describe you, don't go telling me whether or not I can/should use them...that is, if you want me to continue refraining from using them to describe you. ~Prince |
11-22-2003, 06:29 AM | #9 (permalink) |
Darth Papa
Location: Yonder
|
Here's the short version of How to Run Perl on a Windows machine!
First, download the latest version of ActiveState's ActivePerl from HERE. ActiveState is the company that maintains the latest Perl port for Windows. They tend to lag a version or two behind the latest release for *nix, but for a beginner it won't matter a whit. You'll get a .exe installer. Run it. It'll install some stuff at (by default) c:/perl, most importantly, a Perl executable at c:/perl/bin/perl.exe. AND it'll associate the .pl file extension with that executable, which means that if you double-click a .pl file, it'll launch a terminal window (a lot like the MS-DOS prompt) and execute your script. So now you can edit .pl files in notepad or wordpad (or one of a dozen editors BETTER than notepad or wordpad--syntax coloring and parathesis-balancing are wonderful things), save, and double-click to execute. Neat, hunh? |
11-22-2003, 04:11 PM | #10 (permalink) |
Banned
Location: shittown, CA
|
Control Structures.
Perl like many languages common today have a varierty of looping structures that are very useful. The keywords are "if", "while" and "for". You can nest these structures inside one another allowing your program to have powerful logic. Logical expressions are created using relationsal operators. Perl has two types of basic comparison operations, the numeric set and the string set. <table> <tr><td>Operation</td><td>Numeric</td><td>String</td></tr> <tr><td>Is equal to</td><td> == </td><td>eq</td></tr> <tr><td>Is not equal to</td><td> != </td><td>ne</td></tr> <tr><td>Is less then</td><td> < </td><td>lt</td></tr> <tr><td>Is greater then</td><td> > </td><td>gt</td></tr> <tr><td>Is less then or equal to</td><td> <= </td><td>le</td></tr> <tr><td>Is greater then or equal to</td><td> >= </td><td>ge</td></tr> </table> examples: $foo = 15; $bar = $foo; # same as $bar = 15; $foo > $bar # this is false, 15 > 15 is not true. $foo <= $bar # this is true, 15 <= 15 is true because of the equal part. $bar == $foo # this is true, 15 == 15 is true. other useful operators include ++, --, &&, and ||. ++ is called "increment" and adds one to your variable. example: $foo = 3; $foo++; # $foo = $foo+1, so $foo = 4 print $foo; #prints '4' Decrement is symbolized by --, and does the opposite as increment, subtracting one from the variable each time. example: $bar= 3; $bar--; # $bar = $bar-1, so $bar= 2 && is the "and" operator. if the two objects you are anding are both true, then the whole expression is true. if either or both objects are false, the expression returns false. this is a good way to combine checking multiple conditions at once. || is the "or" operator. if the 2 objects being compared are both true, or if EITHER of them are true, then the expression is true. the expression is only false if both items are false. this will become useful when we look at control structures. //Control Structures When a control structure (such as an if statement, or a while loop) is encountered the system will check the expression and if it evaluates to true the code inside the control structure will be executed. Otherwise it will skip the block of code and continue execution. Looping structures usually use this pattern for the logical expression: "something is less then something else". Or "something is not equal to something else". There are many variants I will get to shortly. While this is not the only way to use loops it is a very common useage of the structure. So I will wrap this up into a single unit I will call the "control expression". 'control expression' = 'left comparison' 'compare operator' 'right comparison' Both the left comparison and right comparison can be either variables (Scalars) or hard coded literals (the number 1 or the string "hello") these will always be the same unlike variables which are dynamic and can be different depending upon other code in the system. Here is the skeleton of the control structures: Code:
if( 'control expression' ) { # your code goes here. } Code:
while( 'control expression' ) { # your code goes here. # increment before the next iteration of the loop. } Be careful though because if your control expression does not get modified while inside the while loop you may never break free and get stuck in a 'infinite loop'. Code:
until( 'control expression' ) { #code goes here. } Because while and until loops are so similer you can convert one to the other by changing the logic of the control structure, it is mearly provided for the convience of the programmer to not have to deal with too many opposites at a time. Code:
for( 'loop counter' ; 'control expression' ; 'increment counter' ) { #code goes here. } A for loop can be convired to a while loop like this. Code:
for($bob = 0; $bob < 10; $bob++) { # code here. # each time the loop executes, bob is increased by one. } # is the same as... $bob = 0; while($bob < 10) { #code here. $bob++; } # Both of those loops do the exact same thing. Code:
$foo = 42; if($foo > $input) { print "The input is less then the value 42."; } Code:
#!/usr/bin/perl $foo = 5; print "While loop:\n"; while($foo > 0) { print "We will output now\n"; $foo--; } print "Until loop:\n"; until($foo >= 5) { print "We will output now\n"; $foo++; } print "For loop:\n"; for($foo = 0; $foo < 5; $foo++) { print "We will output now\n"; } Last edited by juanvaldes; 11-24-2003 at 06:21 PM.. |
Tags |
learn, perl |
|
|