Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found