Thursday, March 31, 2011

Regular Expressions: What they are, and why they make you want to drown kittens.

Ok. There are these things called regular expressions. I'm not sure what that means outside of perl. This is what it means inside perl:

You have a file and you want to know what is in it. Maybe your friend is trying to quit porn cold turkey and you think he's been squeezin the hog when no ones looking. Perhaps you have an old hard drive, really old, like super duper old, and you don't know where you put that REALLY IMPORTANT STUFF. Maybe you stalk young women online and you just grepped a bunch of stuff and you want to find her diary..er.. or whatever.

In come regular expressions!



We have quantifiers like:

*
and

+

So let's use them in a regular expression.

we want to find if a file contains the word sabrina so in perl we say


if (/sabrina/) {
print "We found it!.";
}

But how do we know that we're really looking for sabrina? Maybe we should look for Sabrina capitalized.We don't know. So let's just look for both.

Using the OR function which looks like this:

|


our new code to find either or is:

if(/sabrina|Sabrina) {
print "We found a match!";
}

ok but what if want to find sabrina's secrets? If you don't know most people hide things in folders named secret. It makes them feel like james bond or computer haxors or something. So let's fix that regular expression up to represent the new search.

if(/sabrina|secrets|Sabrina/) {
print "Elementary my dear watson...elementary.";
}

Look at that shit! All we had to do was add the word secrets and another OR function! Good stuff!

Ok well now you get into sabrinas secret file and it's empty except for one line!!!
It reads: computer haxors are stupid l33t xxx.

Well this must be some sort of code you reason, so you try to search for that.

you type

if(/l33t|xxx/) {
print "Gotcha bitch!";
<>;
print "I'm rick james bitch!";

And your console dutifully tells you that it has, of course, 'got u bitch'. It also adds that it is Rick James, in case anyone was in doubt.

You go to the files named l33t and xxx and they are also empty. You tell rick james to fuck off. Afterwards you get an idea.

You decide it's been really retarded what you've been doing. You just need to search for dated files. Those would be the juicy ones. Of course. For posterity..er..whatever.

So you use character classes.

Bust out that fuckin script:

if ...

and around this time you realize that you hardly know perl and regex (regular expressions) have made you want to drown a kitten. Your puppy starts to look dehydrated and you think how he would feel so much better submerged in the bathtub so you skip ahead a chapter in the llama book and do the problems, fail, skip to the answers in the back, do them again, fail, skip again to the answers in the back, memorize the answers and then move on to the next. And promise to not touch another regex until you know what the hell you're doing.

Saturday, March 26, 2011

Sub routines in perl: why they kick ass; what you need to know about them.

A sub routine is a special section of code in a program that doesn't give any input unless coupled with another piece of code.

Think of it like a GPS. It will take you where you wanna go, but not unless you pick it up and turn it on.

The code shown in the llama book is this:

sub total {
my $sum;
foreach (@_) {
$sum += $_;
}
$sum;
}


That will go through whatever list you've given it and add all the numbers up: returning the result in $sum. Theres only three basic things you HAVE to know when using sub routines.

1. & is the symbol for a subroutine, when invoking them you must type

&subroutine


whatever input you want to give the subroutine goes on the end like

&total(1..1000) 
or
&total(@lines) 


2. A sub routine ALWAYS returns the last part of it's code. So if you want &total to return $sum (like it should) you have to make sure $sum is the last coded line of the subroutine (as shown at the top).

3. You MUST declare the variable you will be using. (also shown above.)

Thats all.

Thursday, March 24, 2011

Perl: search for whatever damn keywords in a file with four easy lines.

while (<>) {
chomp;
if ($_ eq "then") {
print "\n$_\n";
}}


To use it just open the folder holding the files you want to search through, and place the perl program in the same damn folder. Now open a konsole pointing at the same folder and type:

 ./myperlprogram name-of-file-you-want-to-use-it-on

So for me I had to type:

./f.pl names

f.pl is the name of the perl program. names is the file.








Lets look at the code again:

while (<>) {
chomp;
if ($_ eq "then") {
print "\n$_\n";
}}


See the if statement?

if ($_ eq "then") { 

This is the part you want to change to make the program search for whatever damn keyword you want.  I was searching the string "then".

(Don't ask me why I'm searching for the word "then". I wouldn't fucking tell you anyway.)

Ok, say you had a file with numbers in it and you wanted the program to return all the numbers greater than 5. You would change the if statement to this:

new statement: if ($_ > 5) {

old statement:  if ($_ eq "then") {


See the difference?

That's all. See what kind of crazy hacks you can do by just changing the if statement.

Sunday, March 20, 2011

Perl won't run in terminal Ubuntu.

Script kiddies skip to the bottom for solution.


Ok so I was pisssssssssssssed.

I did a 7 way wipe on my disc and ever since perl wouldn't run from a Konsole? What the hell?

I checked my path and it was pointing at /usr/bin/. I added various directories to the bashrc file hidden in root and tried a few codes that I copied and pasted. Yes. I'm a fan of ctrl+v: so shoot me.

Anyway I found a solution. Although I'm still not sure why it works. I used to be able to navigate to the dir containing the file and then just run it like

myfile.pl

I can't do that now but the quick fix I found was to put ./ in front of the file.

so if your trying to run

myfile.pl

in a konsole simply navigate to the directory containing the file and enter this



./myfile.pl



full example:



problem:

jackie@jackie-desktop:~/Desktop$ file.pl
 

file.pl: command not found



 solution:


jackie@jackie-desktop:~/Desktop$ ./file.pl
 

Hello Assface!


p.s. Yes, my test script was programmed to print "Hello Assface." Getting it to work was the best feeling I've ever had while being insulted by a machine.