°Û °Û ÞÜ ±Û °Û °Û ÜÛÛ ÛÜ ±Û ²Û°ÛÛÛÛß°Û ÜÜÜ ±Û ÜÜ ÜÛÛÛÜ°ÛßßßÛ°Û °Û ÛÛ ° ÛÛ±Û ±Û ÛÛ ±ÛÛßßßÛܱÛÛßß°ÛÜÜÜß °Û°ÛÛÛ ÛÛ ° ÛÛ±Û ±Û ÛÛ ±Û °Û±Û °ÛÜ °ÜÛßßÛ°Û °Û ßÛ ÛÛß °ÛÛÛ ßÛÛÜ°ÛßÛÛÛÛß±Û °ÛÛÛß°ÛÜÜÛ²°Û °Û Outbreak Magazine Issue #11 - Article 3 of 18 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' The art of perl v0.1 by buffer0verflow. In this paper I will try to explain the basics of perl, and why I think its so popular. By the end of this paper the reader should have a basic understanding of perl's data types, minimal regex, and perl syntax. At the end of the paper I will write a demonstration program, To put it all together. 1. What is perl? Perl is a interpreted programming language. Perl stands for Practical Extraction and Report Language, a.k.a. Pathologically Eclectic Rubbish Lister. Perl was developed by Larry wall.It includes many facilities reminiscent of awk. It is considered by sys-admins to be the language of choice. Why? probably cause it can interact easily with the system. And deals well with text, and many other things. As my friend once put it, "Perl is like a hamster with a swiss army knife." Perl was also gonna be named after Larry's wife. Then he decided against it. And moved onto the name 'Pearl' which was a no-go due to there being another language named pearl. So he chose perl. 2. Why its popularity? Alot of perl's popularity comes from the way it can handle text. Perl deals with text excellently. I don't think there is a language that can match perl's text capabilities. Another reason is it allows you to be sort of apathetic with your code. Perl is a pretty loosely typed language. It doesn't hold back much. It does what you tell it to do. Even if it doesn't like it. As I said before it deals with text excellently as you will later see. So sys-admins enjoy being able to parse through log files, and taking out the data they want. And leaving the data they don't need. Another reason, is it has a smooth learning curve. You can learn just the minimal basics and still be able to produce some useful programs. 3. Now onto the basics of the language. Now we'll get into the basics of perl. Alot of people say they can tell alot from the language by seeing how simple the hello world looks. Well in perl I don't think it gets any easier. Here is the hello world in perl: #!/usr/bin/perl print "Hello World\n"; The '\n' of course means to skip to the next line. Just like in many other languages. The first line sometimes referred to as the shebang line. Just makes it so you don't have to type perl hello.pl. You can just type the name of the file: #./hello of course it has to have execute permissions. Next we will deal with some data types. There is really only 4 basic types they go as follows: $string = "l33t"; # A string. $num = 1337; # A number notice they can each use the '$' @array = (10, 12, 12); # an @rray. %hash = ( age => 18, name => "Harry", ); # this is a hash which i will explain in a sec. Perl has the data type hash. Which is sort of like a structure of data. Accept it uses 'keys' to access the data. So say we wanted to get the value of what 'age' holds. We do that as follows: print $hash{age}; # would print '18'. $age = $hash{age}; # or we could store its value in the variable $age. Notice we used % when declaring our hash. But when you access it you use $hash{keyhere} its the same with arrays. As so: @array = (10, 20, 30, 40); print $array[0]; # prints the first value of the array '10'. Remember the array's subscript (value) starts at 0 and goes up. Not 1. You could also print the whole array if you wanted. As so: print @array; # prints all the values in the array all together. We could make that look cleaner alot of ways. Perl has a foreach control statement. We could do: foreach $value (@array) { print ("$value\n"); } Which loops through the array and stores each value in the scalar variable $value. Now we'll go on to some user-defined functions. A function is just a piece of code that usually has a specific job, like adding up variables passed to it etc. You declare a function with the sub keyword as follows: sub add { return $a + $b; } That returns one value, which would be whatever is in $a + whatever is in $b. Full example: $a = 10; $b = 20; $c = add($a, $b); print("\$c holds the value $c now\n"); sub add { return $a + $b; } The \ is for telling perl we want to print the $ symbol so we don't get it confused with the actual value in the variable. You can pass multiple things to functions, and perl can handle it pretty well. As long as the code deals with the variables correctly. And you might have noticed perl doesn't care when you declare variables. Like in some other languages you have to initialize them before their used. Perl don't care. Unless of course you tell it to. we can do that as so: use strict; my($a, $b); $a = 12; $b = 12; print("$a $b\n"); The use strict in the top of the code, puts the strict module into play. Which means we have to declare all of our variables before using them. The advantages are the code tends to run faster with strict. Disadvantanges It kind of bites declaring them before we use them. But its a good idea to use them. The my() function is used to declare your variables. The use functions puts the given module into play, in our case strict. But there are literally thousands of different modules out there. Some include Net::IRC, IO::Socket; CWD, and many others. Now its time for some simple regex. Regular expressions. Are useful for finding patterns in text, strings. Even from sockets. To show a brief example: $string = "Where the hell is the cake"; if($string =~ /cake/i) { print("Found..\n"); } else { print("I didn't find it..\n"); } Running that program will print Found.. since 'cake' is contained in the string.the ~ is a regex pattern telling perl we want to search for something /cake/ is the pattern telling perl we want to search for 'cake', and the i means ignore case. So, if it was say CAKE, or Cake, it will still match it since it ignores the case of the word. You can see how useful regex'es can be. Say we had a file, we wanted to find a pattern in it. Regex's would do the job. To sum it up we'll write a program called 'parse'. Which will take a specified pattern, and search for all occurences, and 'parse'(remove) it from the file. This can be useful. Say your on IRC and someone pastes you some code you want to run. Of course the stuff will be in there. And it would be very time consuming to go through and delete each manually. So we'll throw that data into a file, tell perl to open it up, find the pattern, and remove the pattern if it finds it. The code goes as follows. I will explain it in detail afterwords. # parse.pl $file = shift; $pattern = shift; $file2 = "parsedata.txt" die("usage: \n") unless defined($file) && defined($pattern); open(FILE, "$file") or die("Cannot open file\n"); open(FINAL, "$file2") or die("Can't open"); @arr = ; foreach $line(@arr) { if($line =~ /$pattern/i) { $parsd = (split(/$pattern/, $line))[1]; print FINAL $parsd; } } close FINAL; close FILE; print("New parsed data is in $file2\n"); $time = localtime(); print("Completed on $time\n"); And thats it. We'll start at the beginning $file = shift. This tells perl to get the file name on the command line. ie: when the program is being ran. Same goes for $pattern. If those values aren't defined the program will die printing the usage. A few new keywords were used. unless is another control structure which is pretty self-explanatory. defined() takes a value as its arguments. If its not defined then it will do as told. In our case die. die() is basically a function to signal an error. If the values are defined it moves on and opens the $file specified. If there is a problem opening it it will die. If all goes well it moves on. The @arr = ; stores all the data in the file into the array. Then we move on to the foreach it says. for each line in the array search for the specified pattern is the pattern exists parse it out uses the split function. which removes the $pattern. Then it prints the parsed information into $file2 which will contain what we want. Then it closes both of the files. and tells us the new data is in file2. Then a pointless addition $time = localtime(). The localtime() function is also pretty self-explanatory. Then it tells us its done. That's that. The program should work on any platform. Perl is very portable. The program could have been done in various ways. Perl uses the TIMTOWTDI attitude. There is more than one way to do it. 5. Summary. Hopefully you gained something from this tutorial. If not, I am a loser, and should be shot with a 9. If you have any questions, comments or whatever, you can email me at joey@hackernetwork.com. You can also find me on irc. The next perl tutorial will contain more advanced information with a demonstration on sockets. We'll be creating a simple IRC bot. That's all for now. Peace. buffer email: joey@hackernetwork.com AIM: buffer18@hotmail.com