![]() |
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 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 |
Excellent tutorial - the first post was by far the best! Making a program that does -- well, nothing. :D
|
I like how this is going. Perl scares a lot of people. It's hard to be scared by your first example. Thanks!
|
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. |
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*
|
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 ;) |
thanks to all who have and will post these...very nice to have an interactive learning source.
|
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? |
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' ) Code:
while( 'control expression' ) 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' ) 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' ) A for loop can be convired to a while loop like this. Code:
for($bob = 0; $bob < 10; $bob++) Code:
$foo = 42; Code:
#!/usr/bin/perl |
All times are GMT -8. The time now is 02:06 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project