PERL and the Art of Looking It Up Yourself
Cancer Omega <comega@attrition.org>
Hardly a week goes by when we don't receive a request for the contents of
the code that does the following on the Attrition Main Page:
"rm -rf /bin/laden"
It's a little quote cranked out by a little program that took very little
time to write.
#!/usr/bin/perl -- Trivial Quote Generator - Cancer Omega <comega@attrition.org>
print"Content-type: text/html\n\n";open(Q,"<./QUOTE_FILE.TXT")||die"\"$!\"\n";
$/="\n";rand($.)<1&&($q=$_)while<Q>;close(Q);chomp($q);@qs=split(/\^/,$q);
if($qs[0] ne ""){print"<a href=\"$qs[0]\">$qs[1]</a>\n";}else{print"$qs[1]\n";}
This document explaining the code took longer to write than the code itself.
The reason: this code and its functionality are beyond simple; they're trivial.
To wit:
-
print"Content-type: text/html\n\n";
Deliver output as part of an HTML page.
-
open(Q,"<./QUOTE_FILE.TXT")||die"\"$!\"\n";
$/="\n";rand($.)<1&&($q=$_)while<Q>;
close(Q);
Open and assign a file handle to a text file. The text file is comprised of
individual records separated by newlines and fields within the records separated by carats
(^). If the quote file cannot be opened, give the file open error. Split the input by
newlines. Select one line by a pseudorandom value. Close the filehandle.
-
chomp($q);@qs=split(/\^/,$q);
Strip newline; split the chosen line into elements for the array, @qs.
-
if($qs[0] ne ""){print"<a href=\"$qs[0]\">$qs[1]</a>\n";}
else{print"$qs[1]\n";}
Check that the first element of the array isn't empty. If true, use
the URL of the array's first element with the quote in the second
element. If false, just print the quote in the second element of
the array.
That's it.
That's right. That's IT. Like I said, this code is
TRIVIAL. Minimal research
(via a resource such as CPAN or even Google)
by anyone with even the slightest
understanding of PERL would have yielded more than ample information to
compose a similar if not identical script!
Look, I don't mind helping people. I don't even mind giving people quick
answers to quick questions. But when people write to me asking for this data
without so much as even perfunctory research on their own...well, that's
where I get a bit annoyed.
From this time forward, any "gimme" requests for script codes aren't going
to get an answer; they're going to get forked over to the Attrition
Going Postal section.
.c
|