Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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.
So there ya go a perl program that does absolutly jack shit.
juanvaldes is offline  
Old 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.
juanvaldes is offline  
Old 11-21-2003, 08:57 AM   #3 (permalink)
I am Winter Born
 
Pragma's Avatar
 
Location: Alexandria, VA
Excellent tutorial - the first post was by far the best! Making a program that does -- well, nothing.
__________________
Eat antimatter, Posleen-boy!
Pragma is offline  
Old 11-21-2003, 09:11 AM   #4 (permalink)
Right Now
 
Location: Home
I like how this is going. Perl scares a lot of people. It's hard to be scared by your first example. Thanks!
Peetster is offline  
Old 11-21-2003, 12:50 PM   #5 (permalink)
Darth Papa
 
ratbastid's Avatar
 
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.
ratbastid is offline  
Old 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*
cheerios is offline  
Old 11-21-2003, 04:28 PM   #7 (permalink)
Banned
 
Location: shittown, CA
Quote:
Originally posted by Pragma
Excellent tutorial - the first post was by far the best! Making a program that does -- well, nothing.
Gotta start somewhere

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
juanvaldes is offline  
Old 11-22-2003, 05:35 AM   #8 (permalink)
A Real American
 
Holo's Avatar
 
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
Holo is offline  
Old 11-22-2003, 06:29 AM   #9 (permalink)
Darth Papa
 
ratbastid's Avatar
 
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?
ratbastid is offline  
Old 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> &lt; </td><td>lt</td></tr>
<tr><td>Is greater then</td><td> &gt; </td><td>gt</td></tr>
<tr><td>Is less then or equal to</td><td> &lt;= </td><td>le</td></tr>
<tr><td>Is greater then or equal to</td><td> &gt;= </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.
}
If statement's work the way they do in natural language, "if $bob is equal to "hello", then we enter the if block and execute it's instructions, when done we continue down the program. If $bob does not equal "hello" then we skip the entire block and continue.

Code:
while( 'control expression' )
{
	# your code goes here.
	# increment before the next iteration of the loop.
}
A while loop is just like an if statmement but with one difference. After you are done executing the code in the while block, you go back up and check the while control comparison again. This allows you to loop through the same code many times.

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.
}
A until block is just like a while loop but the value of the control expression is used in the opposite way. The loop will run forever UNTIL the control expression returns true.

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 is a while loop that takes care of itself. The loop counter can be set to any value desiered here. Next is the control expression your familer with. Finally the best part of the for loop over a basic while loop, the increment counter part. You can move the counter up or down in the for loop declaration instead of doing yourself inside the while loop. This results in cleaner code, the meat of the logic is inside the loop less lines that power the loop getting in your way and reducces the number of stupid infanite loop mistakes.

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.
A few more examples...

Code:
$foo = 42;
if($foo > $input)
{
        print "The input is less then the value 42.";
}
Every one of these loops will output the same string 5 times.
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..
juanvaldes is offline  
 

Tags
learn, perl

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:04 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360