XP is just a number | |
PerlMonks |
Evolution of an Obfuscated Throwaway Scriptby neshura (Chaplain) |
on Oct 06, 2000 at 04:37 UTC ( [id://35503]=obfuscated: print w/replies, xml ) | Need Help?? |
Well, they jest don't work me all that hard here at my
happy company, so when a coworker approached and asked if I
could write him a quick script to format the output of
bdf so he could import it into Excel, I said
"Abso-positively, homedude! I've got 8 hours to kill!"
So what's this bdf thing? Read the man page for more details. All I knew was that he tried to capture the output from the screen and paste it into Excel, but the formatting was just ALL wrong. He gave me a sample of the normal screen output from bdf. and also a sample of the fnarky output. I wanted to do it in a one-liner originally, so I could impress "Joe" with my Perl knowledge. Unfortunately, I haven't had occasion to use all the marvelous tools such as map and grep and so on. I decided just to try and get something that functioned first. So I pulled out my dusty Camel 2 and started coding. It took me a while -- I started with about 16 or 18 lines, and there was an annoying little bug which resulted in some empty cells in the final CSV output. I fixed said bug with a grep on /./. What follows was my proud presentation to the occupants of #perlmonks, without the newlines or comments. Doh! Thanks zdog, I COULD have done (scalar...) || (push...). Quoth zdog, "I picked it up reading 'jcwren code'". Excellent. Let's put it on one line. Well this works fine. So I send it to "Joe", and his eyes get wide and I just grin and say "It works. Try it." I caution him that it doesn't work if there are spaces in the names of the filesystems. It's not really robust, but then, he didn't give me a lot of data. So Joe is satisfied. But I'm not, and I've got three hours before I can leave. So the next step is to clean out the parens and the unnecessary functions. Like scalar. Don't even remember why I used scalar. Insurance I guess. And then there is substituting "or" for || so I can get rid of some more parens. This is great! The marginal value to my company of these code improvements is actually negative, and falling rapidly. The final task is to take some hints from posts by japhy. I use odd delimiters for quoting. I get rid of whitespace. I use one-letter variable names. The final version, which is as far as I can take it, is as follows: which gave the desired output In summary, this was a throwaway script. I would never intentionally obfuscate any code that someone would have to come along and maintain later. This was the first occasion I've ever had to do a little obfuscation, but I would never have tried to do it if it weren't for one original design factor. Namely, the fact that using a block with redo was (in this case) better than using the usual for or foreach. Said block confused a fellow programmer for a bit. So now I've had my first taste of obfuscation. Honestly? It was a so-so experience. I taught myself some new constructs, with the help of zdog, jlp, h0mee, and also jcwren and japhy. But I think I like solving useful problems a lot better, and will stick with functional (non-JAPH) readable scripts for the time being. So go ahead -- what would you have done differently? How? Why? e-mail neshura
Back to
Obfuscated Code
|
|