- From the Editor
- Trivia
- New Look and Plugins
- The Login
- The Mentality
- Staff and Exec News
- Ikioi Interview
- Fun Stuff
- myViral
- Devil's Advocate
- Former Staff Bio: Ker Betterman
- Coders Corner: Intro to Perl IRC Bots
- Legality of The MP3
- Copyright Law
- MP3 effects on Copyright
- Anomalies of Code
- This Months Challenge
- Previous Months Challenge
- The Code
- Restructure
- myMusic
- Comments (3)
Anomalies of Code
By: Gen Zerhash
Test Code!
This Months Challenge
Context is perl. again.. i know
Previous Months Challenge
The previous month we were given:
-
s""hello";y{hello}}world};s;(.);\U$1;g;print
-
It is in perl context.
If you through this straight into the compiler you would get a simple output of:
WORRD
So how does this do it, and why is it so messy?
This falls in line with a word created amongst the perl community which sets out so make the code as uncomprehensible to the human eye as possible, “Obfuscation”.
There are other variations of it where the code is supposed to look like a picture. Others where all it outputs is “Just Another Perl Hacker” or JAPH. This is completely nonfunctional however it does show off skill in the perl syntax.
So now we will look at the code.
The Code
-
s""hello";y{hello}}world};s;(.);\U$1;g;print
-
This is a complete abuse of the regex engine. As a normal regex goes, it should look similar to
-
$var =~ m/regex/;
As a “feature” however the slashes can be substituted with any character. This can help in many circumstances where you want code to be more readable.
-
#to…
-
$var=~s|http://www\.cyberarmy\.net/my/profile/|http://profile.cyberarmy.net|;
There are rules that govern this which say that the operator needs to be present in order for the compiler to identify that it is a regex.
By the way, the three types used here are
m – match
s – substitute
y – tr – transliteration
In order to be identified a quote-like operator needs to be present. With the regular slashes, the match operator can be assumed.
Restructure
First lets lay this out neatly.
-
s""hello";y{hello}}world};s;(.);\U$1;g;print
-
-
#is the same as
-
-
s""hello";
Line one redefined looks like this.
-
s""hello";
-
#to…
-
$_=~s//hello/;
-
Nothing has been substituted with hello. The default operator is the default variable. Hence $_ gets operated on and now holds the value of “hello”
Line two redefined looks like this.
This is the transliteration operator so hello is being substituted letter for letter. ‘y’ is the old sed way of doing this. ‘tr’ works the same way.
There are more options which have similar characteristics. As multiple transliterations were given with ‘l’ from hello, only the first one is used. Therefore:
h = w
e = o
l = r
l = l… no = r, predeclared
o = d
as a result, we have now translated $_ to be worrd.
The third line redefined looks like this.
Not too much has changed here but we added the \U operator. The ()’s indicate that we want to capture what we have for later use. It gets preserved into $1 as it is the first capture. This $1 is now modified with \U to make it upper case. The option of g after the regex states that i want global pattern matching. Matching as many times as possible. As a result the entire phrase is now uppercase.
The last line
Another default. When not given anything, will print the default variable.
Quote and Quote-like Operators
Very nice! Overall looks amazing! Great job Zerhash and all who contributed!
Great job! I enjoyed the articles as well as the interview. Keep up the good work!
Answers for Trivia CAZine: issue 4, October 2009:
1. Cat
2. Pengo
3. Zebulun
4. Prothis
5. 2004