Difference
(author diff)
Added Changed Deleted

  • Prior minor revision
  • Current Revision



    The concise nature of [[Perl]] syntax permits miniscule yet powerful programs called "one liners" because they're easy to run from a command line. Line breaks have been added here.
     
    ----
     
    '''A simple web server that works with static and dynamic pages'''
     
    perl -MIO::All -e 'io(":8080")->fork->accept->(
    sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
     
     
    ----
     
    '''Portscanner'''
     
    perl -MIO::Socket -wle"map { IO::Socket::INET->new(qq($ARGV[0]:$_))
    and print } 1..$ARGV[1]||10000" localhost 1000
     
     
    ----
     
    '''Mandelbrot'''
     
    perl -MPDL -MPDL::IO::Pic -e '$a=zeroes 300,300;$r=$a->xlinvals(-1.5,0.5);
    $i=$a->ylinvals(-1,1);$t=$r;$u=$i;for(1..30){$q=$r**2-$i**2+$t;$h=2*$r*$i+$u;
    $d=$r**2+$i**2;$a=lclip($a,$_*($d>2.0)*($a==0));
    ($r,$i)=map{$_->clip(-5,5)}($q,$h);}$a->wpic("mandel.gif");'
     
     
    ----
     
    '''Rock, Scissors, Paper'''
     
    perl -e "$c=(($r,$s,$p)=qw|rock scissors paper|)[(int rand 3)];
    $|=0; until($y=~/^($r|$s|$p)$/){print \"$r, $s or $p? \";$y=<>;chop $y};
    %r=($s,{$p,1,$r,0},$p,{$r,1,$s,0},$r,{$s,1,$p,0});
    print \"You: $y\nCom: $c\nYou \".
    ((exists $r{$y}{$c}?($r{$y}{$c}?'Win': 'Lose'):'Draw').\"\n\")"
     
    '''Bone::Easy <-> Chatbot::Eliza'''
     
    perl -MChatbot::Eliza -MBone::Easy -e "$a=Chatbot::Eliza->new();
    for(0..$ARGV[0]){$b=pickup(); $c=$a->transform($b);
    print \"Bone: $b\nLiz: $c\n\"}print 'Liz: '.$a->final()->[0].\"\n\"" 10
     
     
    ----
     
    == CGI ==
     
    '''World's Shortest Wiki'''
    <pre>
    #!/usr/bin/perl
    use CGI':all';path_info=~/\w+/;$_=`grep -l $& *`.h1($&).escapeHTML$t=param(t)
    ||`dd<$&`;open F,">$&";print F$t;s/htt\S+|([A-Z]\w+){2,}/a{href,$&},$&/eg;
    print header,pre"$_<form>",submit,textarea t,$t,9,70
    </pre>
    From http://c2.com/cgi/wiki?SigWik
     
    ----
     
    == CLI ==
     
    '''RSA in 5 lines of perl'''
     
    #!/usr/bin/perl -s
    do 'bigint.pl';($_,$n)=@ARGV;s/^.(..)*$/0$&/;($k=unpack('B*',pack('H*',$_)))=~
    s/^0*//;$x=0;$z=$n=~s/./$x=&badd(&bmul($x,16),hex$&)/ge;while(read(STDIN,$_,$w
    =((2*$d-1+$z)&~1)/2)){$r=1;$_=substr($_."\0"x$w,$c=0,$w);s/.|\n/$c=&badd(&bmul
    ($c,256),ord$&)/ge;$_=$k;s/./$r=&bmod(&bmul($r,$r),$x),$&?$r=&bmod(&bmul($r,$c),$x)
    :0,""/ge;($r,$t)=&bdiv($r,256),$_=pack(C,$t).$_ while$w--+1-2*$d;print}
     
    From http://www.cypherspace.org/adam/rsa/pureperl.html
     
    ----
    * [http://www.perlmonks.org/index.pl?node_id=324749 Uncommon* but Useful Perl Command Line Options for One-liners]