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*

In reply to (ichimunki) Re: Reactions to OO-Perl by ichimunki
in thread Reactions to OO-Perl by pjf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.