#!/usr/bin/perl -l

#                                                          ppg256-2.pl
# Perl Poetry Generator in 256 Characters, Number 2
# Nick Montfort nickm@nickm.com
#====================================================================#
#
# This is a reformatted and commented version of a Perl program that 
# will run on Mac and Linux on the command line and which is 256 
# characters long:
#
# perl -le 'sub p{split/,/,pop;$_[rand@_]}{$_=p("sw,-aw,&w,saw".", "x$l);s//p("aw,w")/e;s// /g;$_="\n\nthe s\n"if!$l;s/s/ws/;s/a/p("a,the,to,of")/e;s/w/p("b,ch,f,gr,k,p,sh,s,sk,sp,tw")."i".p("ll,n,t")/eg;s/(b|p|f)i/$1.p("a,i")/e;print;$l=0if$l++>6+rand 9;sleep 1;redo}'
#
# The only changes that have been made are the addition of spaces,
# newlines, and a few comments.
#
# On Windows, install ActivePerl to be able to run this program.
# To run, change to the directory where this file is located and type
# at the command prompt:
#
# perl -l ppg256-2.pl

sub p # Return a random element from a comma-delimited string
  {
    split /,/,pop;
    $_[rand@_]
  }
{ # Main loop
  $_ = p("sw,-aw,&w,saw".", "x$l);
  s //p("aw,w")/e;
  s // /g;
  $_ = "\n\nthe s\n" if !$l;
  s/s/ws/;
  s/a/p("a,the,to,of")/e;
  s/w/p("b,ch,f,gr,k,p,sh,s,sk,sp,tw")."i".p("ll,n,t")/eg;
  s/(b|p|f)i/$1.p("a,i")/e;
  print;
  $l=0 if $l++ > 6 + rand 9;
  sleep 1;
  redo
}
