I’ll Tell You What…

March 17, 2008

BeerSong.pl

Filed under: Programming, Perl — Larry @ 2:27 am
This has a much better output. It actually types out the word for the number instead of just the number itself.

Two bottles of beer on the wall.
Two bottles of beer.
Take one down.
Pass it around.
One bottle of beer on the wall.

One bottle of beer on the wall.
One bottle of beer.
Take it down.
Pass it around.
No more bottles of beer on the wall.

#!/usr/bin/perl -w

use strict;
use Lingua::EN::Inflect qw( NUMWORDS );

my ($beerNumber, $beerNum, @num_word);
my $word = "bottles";

$beerNumber = 99;

while ( $beerNumber > 0 ) {
	$beerNum = wordize($beerNumber);
	
	print "$beerNum " . $word . " of beer on the wall.\n";
	print "$beerNum " . $word . " of beer.\n";
	
	if ($beerNumber == 1) {
		print "Take it down.\n";
		$word = "bottle";
	} else {
		print "Take one down.\n";
	}
	
	print "Pass it around.\n";
	
	$beerNumber--;

	if ($beerNumber == 1) {
		$word = "bottle";
	}
	
	$beerNum = wordize($beerNumber);
	
	if ($beerNumber > 0) {
		print "$beerNum " . $word . " of beer on the wall.\n\n";
	} else {		
		print "No more bottles of beer on the wall.\n";
	}
}

sub wordize {
	my (@beerNumWord, $beerNumReturned);
	
	@beerNumWord = NUMWORDS ( shift @_ );
	$beerNumReturned = ucfirst(shift @beerNumWord); 
	return $beerNumReturned;
}

August 22, 2005

Verse of the Day - ver 0.5

Filed under: Bible, Software, Programming, Perl — Larry @ 7:08 pm
Here's a very basic verse of the day-type application that I wrote while playing with Perl and the Tk module. This version is platform independent, ie, it works with Windows, Linux, Solaris, Mac OS X, etc. In order to run this under Windows, you must have ActiveState Perl installed and have the Tk module installed. It may already be installed. For Mac OS X, X11 needs to be running with Perl and the Tk module installed. For all other *NIX platforms, Perl needs to be there as well as the Tk module.
#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use XML::Simple;
use Tk;
use Text::Wrap;

my ($content,$ref,$verse,$title);
my ($passage,$mw,$button,$column);

$Text::Wrap::columns = 40;

# Uncomment and modify the next line if you use a proxy
# $ua->proxy('http', '');
$content = get("http://www.gnpcb.org/esv/share/rss2.0/daily/")
	or die "Couldn't get URL: $!\n";

$ref = XMLin($content);

$verse   = $ref->{channel}->{item}->{description};
$title   = $ref->{channel}->{description};
$passage = $ref->{channel}->{item}->{title};

$verse = wrap('', '', $verse);

$mw = new MainWindow(-background => "#FFFFFF");
$mw -> title($title);

$mw -> Label(-background => "#FFFFFF",
             -text       => "$verse\n\t\t$passage\n")
     -> pack(-ipady      => "10",
             -ipadx      => "30");

$button = $mw -> Button(-text    => "Quit",
                        -command => sub { exit; } ) 
                -> pack(-expand  => "1",
                        -fill    => "x",);

MainLoop;

26 queries. 9.246 seconds. Powered by WordPress