Perl Tutorial Part 2:
The thing I liked from the start about perl is that you can execute mini scripts from within the unix shell using the "perl -e" (-e means execute) command (which are sometimes called one-liners). I believe this can be done in Windows too, but you may have to change directories to the Perl directory to successfully execute them. The perl -e command can execute hex encoded shellcode on the fly for finding/exploiting bugs in code (See my overflow.pl script in the code bank). Adding padding, and other evasion techniques, is done on the fly too. Execution in one command from the shell is what first drew me to it. Now after learning more and more perl, I see how quickly one may "want" to learn perl, once you have had a taste of it (something I never felt with Python,C or javascript until after learning Perl). I think it's just a smooth stylish language with tons of supporters and a great community behind it.
Now something quick that I forgot to go over; variables can be assigned in many different ways, even as so:
$a=$b=$c=42;
Perl is a very flexible language and very understanding where a lot of other languages are not. Anyway, back to conditionals...
12) Perl conditionals - In a conditional statement, a condition must be met for a block of code to be executed. This gives a program more flexibility and options. By using "user input" or "user access," a user can control which block of code gets executed and which ones don't, basically controlling the program as in 10c.
Here is an example of how the Perl conditional works:
Code
12b) Or, to add an actual program where this is used for understanding:
Code
OUTPUT and Program Info: The program asks for your name, you give your name. If your name starts with a capital A,B, or C the program will output
your name begins with A, B or C.
Possible Problems with this program-> This programs outputs only if you use capitalization, so if you were to put "anthony" instead of "Anthony" the program wouldn't execute the first block of code outputting your name starts with an "A"
How do I fix this? -> the unary operator lc (lower case conversion) or uc (uppercase conversion) would fix this:
Code
More on Unary Operators:
13) Unary Operators - there are too many unary operators to print, but they can be very useful when needed, and save a lot of time in the long-run.
a) Operator Sample Results
b) int int(4.65887) Returns the integer portion of its argument (4).
c) length length("hacker") Returns the length of its string argument (6).
d) lc-> my $shuttup = lc("I'M SHOUTING") <-Returns its argument shifted to lowercase letters ("i'm shouting").
e) uc-> my $screamloud = uc("i'm not calm") Returns its argument shifted to uppercase letters ("I'M NOT CALM").
f) cos->my $r = cos(50) Returns the cosine of 50 in radians (.964966).
g) rand-> my randomNo=rand(5) Returns a random number from 0 to less than its argument. If the argument is omitted, a number between 0 and 1 is returned.
To get more go use "perldoc" or type perldoc in the terminal and you should see all the Unary operators you will ever need.
14) Perl string operators - these are the numerical operators Perl gives us for comparison tests that I forgot to add earlier:
Code
Ok, when using operators in comparisons, we need to know when to use = or eq, etc. Perl makes it simple for us. If your comparing strings use eq for the string operator, which is a string in and of itself. When we comp
are numeric data we use the < > !=, etc which are numeric operators. Often of n00bs, or for a better word, people new to Perl mix this up and wonder why their programs don't output or run what they want.
Looping With while And for
14) While - like most things, the while statement in Perl isn't much different than with Python and other lower level languages. In Perl, its purpose is "looping". As you move along in Perl, you need to be able to execute multiple blocks of code instead of just one conditional block of code. I mean if you want your program to be able to make actions based on multiple decisions you need to be able to repeat certain blocks of code conditionally. This is where the "while" looping comes into play.
It works like this:
while(expression) block
Ex/-*Example of a simple "while" script that counts back from 10 to 1:
Code
Line 1- counter is started
Line 3- expression $counter > 0 is checked, if its true the code is run.
Line 4- The value of $counter is decreased by 1
Line 5- Line 5: The } or bracket marks the end of the block started on line 3 with a {. At this point, Perl returns to the top of the while loop and re-evaluates the conditional expression.
Output of our program:
h4x@slax:~/$perl countdown.pl
Still counting down...10
Still counting down...9
Still counting down...8
Still counting down...7
Still counting down...6
Still counting down...5
Still counting down...4
Still counting down...3
Still counting down...2
Still counting down...1
For loops: The for expression(mentioned in section 9) is the most complicated and flexible of Perls looping constructs and follows these general rules:
for ( initialization; test; increment ) block
a) The initialization expression is first checked or evaluated.
b) The test expression is then evaluated, and if true, the block of code will run.
c) After the code is executed, the increment is performed, then the test is evaluated again. If the test is still true, the code will run again. This process will continue until the test expression is false.
An example of for looping:
Code
This script will keep running and looping, starting with 5, adding 2 each newline until it becomes false.
Output:
hax@slax:~/Desktop$ perl for.pl
a is now 5
a is now 7
a is now 9
a is now 11
a is now 13
a is now 15
a is now 17
a is now 19
See, the code ran until the expression became false. It became false when $a became greater than 20. Therefore, the looping stopped and the code stopped looping.
Perl Arrays
14) Arrays and Literal lists - To create an Array in Perl is different than in other languages, because you don't have to tell Perl that you're creating an array or how large the array will be or end up. To create an array and fill it with a list of objects or items, all one has to do is:
my @OS=qw( Windows OSx Linux ); #qw has limitations which will be noted in the 3rd part of this tutorial, but for now its a nifty little operator that makes our arrays more readable.
Moving along....
- Arrays can also involve other arrays and even empty lists:
my @colors=my @paint;
my @chores=();
^^
Above the @chores array, @colors would become part of the @paint array. If @paint had elements before this statement, the previous elements will now be lost. And after the second statement is executed @chores will become empty, so remember assigning an empty list to an array variable will remove all elements from the array.
Now, if a list contains other lists or arrays, these lists will be made into 1 large list as so:
Code
The smaller lists will be flattened by perl into the one Catalog Array. You can check this out by adding the print command to our script:
Code
and our output:
h4x@slax:~/Desktop$ perl array2.pl
Shirts Sneakers Pants Bikes Rims Snowboards Magazines Vitamines Skiing
^^
So, we can extract all the data from our array, but how do we extract particular pieces of data from the array? In perl, like in a lot of languages, the first positive number is 0, and then 1. So if we wanted to pull the first and second(Shirts and Sneakers) items out of our array, we would simply add this to our array:
Code
This would give the expected:
h4x@slax:~/Desktop$ perl arrays.pl
Output:
Shirts Sneakers
And this works all the way down the line. To see further down the list, or to extract elements further down the list, you would just add the numbered value of that item counting from left to right using zero as the first positive value. For example, to see Bikes and magazines, you would us:
No comments:
Post a Comment