Post your favourite Perl scripts here! Let's kick it off with this one, that prints the ever-popular "just another perl hacker" text:
not exp log srand xor s qq qx xor
s x x length uc ord and print chr
ord for qw q join use sub tied qx
xor eval xor print qq q q xor int
eval lc q m cos and print chr ord
for qw y abs ne open tied hex exp
ref y m xor scalar srand print qq
q q xor int eval lc qq y sqrt cos
and print chr ord for qw x printf
each return local x y or print qq
s s and eval q s undef or oct xor
time xor ref print chr int ord lc
foreach qw y hex alarm chdir kill
exec return y s gt sin sort split
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
$h="GABLHAAADABCDIAABIBCBIEAAMBCPMPPMOPDAGEAALBCADEAAIBCBAECAI".
"BCPAEDMIPDBAECAIBCBAECAIBCBAECIIPDBAECEIBCPAEDAIBCBAECAIBB".
"AAGAIIBAAADA";
while($h=~s/.{8}//){map{$b=($b<<4)+(ord($_)-65)}split(//,$&);
$_=unpack("b32",pack("N",$b))."\n";tr/01/ #/;print;}
That one reminds me of this, which was written by me and a friend:
$a="4 '|'\n3 (|'\n( (|/.(|W-,,'\n.(/|[-'>'\n( (|/'(|W-,''\n3 (|'\n4 '|'\n";
$a=~s/(.)(.)/print $2x((ord$1)-38)/sge;
The output probably only makes sense to old Usenetters, though.
Also, here's a classic, which is extra fun because it's fairly easy to pick apart and understand:
perl -wle '(1 x $_) !~ /^(11+)\1+$/ && print while ++ $_'
It lists prime numbers.
I'm not getting how >>8 works. Does it have to do with the -l option... what does -l do anyway?
According to perl --help
:
-l[octal] enable line ending processing, specifies line terminator
So, uh, I think it tells Perl that a program line ends when it comes to a line break, as opposed to a ;
. I could be wrong...
-l
just adds an automatic newline after each print
.
Here's a cleaner version:
while(1)
{
$n++;
if( ("1" x $n) !~ /^(11+)\1+$/ )
{
print "$n\n";
}
}
Further hints: the "x" is a string repetition operator. "a"x3 is "aaa".
Oh. OH!
God that's retarded.
It gets the regex lib to try to see if any integer multiple (>2) of runs of '1' greater than 2 exactly fit a string of '1's as long as the number.
ARRRGH. ARRRRGH!
Hahaha.
See I didn't understand the regex, cuz I was seeing "$/" and thinking that there was some sort of early binding going on that makes a weird regex (I only thought about that because -l in perl mentions changing the $/ and $\ variables for line endings).
Of course // binds tightly, and $/ at the end always means "end of string. I'm an idiot.
I'm glad somebody gets it. That code makes me giggle every time I see it.
Nerdiest thread evar.
My favorite perl scripts are the ones that jump out of the monitor and club you in the face with obviousness. Yay for self-documenting code.
All right, maybe I'm just a bit too staid. >.>;
The reason I like Perl is that you can write strict, well-structured and understandable code in it, while at the same time, you can also write what pretty much amounts to humour in it. I think poetry is still out of its grasp, but overall Perl is much more like a natural language than any other programming language, since most programming languages limit their expressivity in favour of clarity. Thus, Perl does not limit creativity nearly as much as more traditional programming languages. This is both a blessing and a curse, of course, but personally, I enjoy it a lot.
> I think poetry is still out of its grasp
But those are written in English, not Perl. If you want to (as I am trying here) consider Perl as a natural language, you'd have to express your concepts in Perl code itself, not in English words.
Still amusing, of course, but not what I was getting at.