in reply to Reactions to OO-Perl

OO-Perl is easy and fun. It is too bad for those who don't want to get into it. It certainly makes some of the "pure" OO scripting languages out there look like they're doing it the hard way. The problem is, we all start with Hello World, but nobody goes and does that for OO Perl (except maybe merlyn in perlboot-- his menagerie of animals make a few interesting noises). So here's a start on why OO Perl shouldn't be intimidating...
#!/usr/bin/perl -w use strict; use World; # object oriented hello world my $USA = World->new('English'); my $foo = new World; $USA->greet; $foo->greet;
and then in World.pm
#filename World.pm (easiest to put in same directory with calling scri +pt) package World; my %greetings = ( 'english' => 'Hello, World!', 'german' => 'Guten Tag, Welt!', 'latin' => 'Salve, Munde!', ); sub new { my $class = shift; #default language is Latin my $language = lc( shift ) || 'latin'; my $self = {}; #check to make sure the language is valid $self->{'language'} = (grep( /$language/, (keys %greetings)))[0] | +| 'latin'; bless $self, $class; return $self; } sub greet { my $self = shift; my $language = $self->{'language'}; print $greetings{$language}."\n"; } 1;
Trivial example, but imagine writing this (same functionality) in procedural code. You might be able to fake it with some closures or a handrolled lookup table, but when you have data that lends itself to the object model it pays to use OO. In the few lines above we have class data (%greetings), class methods (new), instance data ($self->{'language'}), and instance methods (greet). And once we've built our class, we have very few instructions to get a wide variety of greetings going. Imagine localizing more IO routines this way...

Anyhoo. Just having fun. The world can always use another hello world program right? *grin*