From f1efe9521aa5aead099b73a7f3d87a5bd3f68457 Mon Sep 17 00:00:00 2001 From: Peter Marquardt Date: Tue, 22 Nov 2022 10:42:42 +0100 Subject: [PATCH] chicken: add numberofwords option Waaak... Bwak gobble-gobble cluckity cluck-a-buh-gawk cluck Bok bwak cluckity cluck-cluck-cluck cluck Gobble-gobble cluck-cluck-cluck... Bwak cock-a-doodle-doo bok Cluck-cluck-cluck! Cluckity Bwok cluck-a-buh-gawk puk bok Gobble-gobble Cluck-a-buh-gawk cluck-a-buh-gawk bwwwaaaaaaaaaak bwak waaak bwak puk puk bwak! --- chicken.pl | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/chicken.pl b/chicken.pl index b949ad6..ce6036d 100755 --- a/chicken.pl +++ b/chicken.pl @@ -1,4 +1,18 @@ #!/usr/bin/perl -w +use Getopt::Long; +use constant USAGE => <<'eof'; + +usage: $0 [options] + + --numberofwords --nw : number of words + +eof + +our %OPT; + +my $numberOfWords = 0; + +GetOptions( 'numberofwords|nw=i' => \$numberOfWords, ) or die USAGE; my @WORDS = ( "puk", "pukaaak", "cluck", "cluck-cluck-cluck", "cluckity", "bwak", @@ -10,7 +24,9 @@ my @PARAGRAPH_SIZES = ( 15, 30, 75 ); -my $numberOfWords = $PARAGRAPH_SIZES[ int( rand($#PARAGRAPH_SIZES) ) ]; +unless ($numberOfWords) { + $numberOfWords = $PARAGRAPH_SIZES[ int( rand($#PARAGRAPH_SIZES) ) ]; +} # generate paragraph, with random words, capitalization, and punctuation. my $paragraph = ""; @@ -34,7 +50,7 @@ # note: always capitalize if it's the first words, or if preceded by punctuation my $shouldCapitalize = $i == 0 || $previousWordHadPunctuation || rand() > 0.7; - $word = $shouldCapitalize ? uc( substr($word,0,1) ) . substr( $word, 1 ) : $word; + $word = $shouldCapitalize ? uc( substr( $word, 0, 1 ) ) . substr( $word, 1 ) : $word; # preserve info for next iteration $previousWordHadPunctuation = $shouldAddPunctuation;