Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The advice is getting a little thick here, so I thought I'd offer simplicity. First, I'd go with Morgon's advice and track down a copy of Damian Conway's Object-Oriented Perl. Also try Intermediate Perl. You might get lucky and find either or both at your local library.

If you're asking what OO Perl is, either you don't know what OO programming is, or you don't know what it would mean in Perl. So I'll give you a couple of basics. After that, I'd suggest you pick a super-trivial task and roll your own objects. Then re-read whatever you've got on hand. It should make more sense then.

OO programming simply inverts functions and data, after a fashion. So instead of maintaining a large data structure and writing code to access it, you refactor down what one record should look like, and then you create a class that represents that record. Objects are references. Classes are packages. Methods are functions. When you refer to $this_obj->name(), it runs the function name() in the class you created $this_obj from, but $this_obj has its own copy of the function and the data.

Off the cuff, let's say you want to write a little database to store your DVD's. I'm ignoring persistence issues.

The procedural way in Perl would probably be to create a hash, something like this (you'd probably use ISBN numbers, but c'mon, it's just an example!):

%movies = ( 'Gone with the Wind' => { year => 1939, stars => ['Clark Gable', 'Vivien Leigh'], director => 'George Cukor' }, 'Wizard of Oz, The' => { year => 1939, stars => ['Judy Garland', 'Billie Burke'], director => 'Victor Fleming' } # etc. );

Then you'd write your code to address the data structure. Say you want to print a list, with the basic info:

for $key (sort keys %movies) { print "$key:\n"; print " Year: $movies{$key}->{year}\n"; print " Stars: ", join(', ', @{ $movies{$key}->{stars} }), "\n"; print " Director: $movies{$key}->{director}\n\n"; } ------------------Prints-------------- Gone with the Wind: Year: 1939 Stars: Clark Gable, Vivien Leigh Director: George Cukor Wizard of Oz, The: Year: 1939 Stars: Judy Garland, Billie Burke Director: Victor Fleming

Mmmmmm! Yummy mush code! And with dereferencing sorbet for dessert! An important reason for object-oriented programming is to hide all the messy details in the object code (often in another file). There's also encapsulation and inheritance, but learn how to make it work first.

Below is the code for a class of pitifully inadequate DVD objects.

package MyDVDs; # Creates a reference as a hash ref, "blesses it" into the # current package, and returns the reference. sub new { my $self = {}; bless $self; return $self; } # Simple get/set accessor. If you pass it something, it will add or # replace (set) whatever is there. Always returns whatever it has (get +). sub title { my $self = shift; $self->{title} = shift if @_; return $self->{title}; } sub year { my $self = shift; $self->{year} = shift if @_; return $self->{year}; } # A variation, storing data in an array. A simple improvement might be # to remove duplicates. sub stars { my $self = shift; push @{$self->{stars}}, @_ if @_; return join(', ', @{$self->{stars}}); } sub director { my $self = shift; $self->{director} = shift if @_; return $self->{director}; }

Now you go to your main package and create and populate objects.

use MyDVDs; # You'd probably load this directly from some source, creating anonymo +us # objects and sticking them in an array, but I have to go to a meeting + soon... # Notice how easy it is simple it is to add data. my $movie1 = MyDVDs->new; $movie1->title('Gone with the Wind'); $movie1->year(1939); $movie1->stars('Clark Gable', 'Vivien Leigh'); $movie1->director('George Cukor'); my $movie2 = MyDVDs->new; $movie2->title('Wizard of Oz'); $movie2->year(1939); $movie2->stars('Judy Garland', 'Billie Burke'); $movie2->director('Victor Fleming'); push @list, $movie1, $movie2; # And how is this for simplicity of accessing the data? for $movie (@list) { print $movie->title, ":\n", " ", $movie->year, "\n", " ", $movie->stars, "\n", " ", $movie->director, "\n\n"; } ------------------Prints-------------- Gone with the Wind: Year: 1939 Stars: Clark Gable, Vivien Leigh Director: George Cukor Wizard of Oz, The: Year: 1939 Stars: Judy Garland, Billie Burke Director: Victor Fleming

Inserting code into the objects is super easy, as is accessing the data, without having to remember how to dereference even the most complicated data structures. When you think you have your mind wrapped around it, check out Moose. There's a LOT you can do with that, but it will make much more sense once you've seen how it all fits together. Designing OO systems can be hard. But using them is usually pretty easy.

--marmot

In reply to Re: kind of effort required for learning OO perl ?! by furry_marmot
in thread kind of effort required for learning OO perl ?! by Anonymous Monk

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 scrutinizing the Monastery: (6)
As of 2024-04-19 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found