Why are the PerlMonks working?
They're hacking and golfing so fine.
And thinking and singing and smiling,
Cause seeking perl wisdom takes time!


Dear fellow monks!
Above is an exemplary output of my perl poetry generator, a small piece of code I wrote to learn about multi-dimensional arrays and Win32::OLE::Excel.
It is a port of a Pascal-program I created ~20 years ago ... any hint on how may I make it more "perlish"?
best regards
Rata

#!/usr/bin/perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); my $sheet = $Excel->Workbooks->Open($ARGV[0])->Sheets->item("words"); + # note to myself: add error handling here! my @words; foreach my $col('A'..'F') { my $row=1; my @list = (); my @value = (); do { @value= $sheet->Range("$col".$row++)->{'Value'}; if (defined ($value[0])) { push (@list, $value[0]); } } while(defined ($value[0])); push (@words, \@list); } my $rhyme = int(rand( scalar(@{$words[4]}) )); my $poem = $words[0][int(rand( scalar(@{$words[0]})))]." are ".$words[ +1][int(rand( scalar(@{$words[1]})))]." ".$words[2][int(rand( scalar(@ +{$words[2]})))]."?\n". "They're ".$words[2][int(rand( scalar(@{$words[2]})))]." an +d ".$words[2][int(rand( scalar(@{$words[2]})))]." ".$words[4][$rhyme] +.".\n". "And ".$words[2][int(rand( scalar(@{$words[2]})))]." and ". +$words[2][int(rand( scalar(@{$words[2]})))]." and ".$words[2][int(ran +d( scalar(@{$words[2]})))].",\n". "Cause ".$words[2][int(rand( scalar(@{$words[2]})))]." ".$w +ords[3][int(rand( scalar(@{$words[3]})))]." ".$words[5][$rhyme]."!\n" +; print ("$poem\n"); $Excel->Speech->speak($poem); # let us hear it! exit 0; #Provide the name of an Excel-file as parameter! #Excel content (in a sheet called "words"): # # A B C D E F #Why the PerlMonks coding perl wisdom in vain is pain #Where the people hacking c-sharp like none is fun #How the fanboys smiling unix so fine takes time # great coders thinking posix # small thinkerslearning shell scripts # code monkeys asking java # writing # pasting # ...

Replies are listed 'Best First'.
Re: poet generator (playing with Win32::OLE::Excel )
by toolic (Bishop) on Sep 03, 2009 at 19:18 UTC
    any hint on how may I make it more "perlish"?
    Maybe not more Perlish, but certainly a little easier to read:
    my $poem = # Line #1 $words[0][rand @{$words[0]}] . " are " . $words[1][rand @{$words[1]}] . " " . $words[2][rand @{$words[2]}] . "?\n" # Line #2, etc... ;

    Update: This reminds me of the silly-senetence-generator I hacked together for my young son a few years ago:

    # Random silly sentence generator. use warnings; use strict; my @nouns = qw(elephant dog man carrot ocean); my @verbs = qw(is seems appears wants_to_be); my @adjs = qw(hairy smelly purple grimy wobbly); while (1) { my $noun = $nouns [ rand @nouns ]; my $verb = $verbs [ rand @verbs ]; $verb =~ s/_/ /g; my $adj = $adjs [ rand @adjs ]; print "The $noun $verb $adj.\n..."; <STDIN>; # Hit "Enter" on your keyboard for a new sentence. } # Ctrl-C to exit.