Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: How do I go from procedural to object oriented programming?

by SimonPratt (Friar)
on Apr 22, 2015 at 08:52 UTC ( [id://1124237]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How do I go from procedural to object oriented programming?
in thread How do I go from procedural to object oriented programming?

Kia ora, Lady_Aleena, sure

  1. No it isn't, just happens to be the version I use (faster than writing use strict; use features;. Also gives hints about the version of Perl I am using, in case people do run into issues with any scripts I have created)
  2. In the sample I provided, this is main:
    my %movies_data = ( 'Firefly' => { 'title' => 'Firefly', 'startyear' => '2002', 'endyear' => '2003', 'media' => 'tv', }, 'Criminal Minds' => { 'title' => 'Criminal Minds', 'startyear' => '2005', 'endyear' => 'tbd', 'media' => 'tv', }, 'The 10th Kingdom' => { 'title' => 'The 10th Kingdom', 'startyear' => '2000', 'endyear' => '', 'media' => 'miniseries', }, 'Iron Man' => { 'title' => 'Iron Man', 'startyear' => '2008', 'endyear' => '', 'media' => 'film', 'basedon' => 'comics', 'company' => 'Marvel Comics', }, 'Tin Man' => { 'startyear' => '2007', 'title' => 'Tin Man', 'media' => 'miniseries', 'basedon' => 'novel', 'company' => 'L. Frank Baum' }, 'The Avengers (1998)' => { 'title' => 'The Avengers (1998)', 'startyear' => '1998', 'media' => 'film', 'basedon' => 'television series', 'company' => 'Thames Television', }, ); my $library = library->new(name => "Lucky's"); foreach my $movie (values %movies_data) { $library->add_movie(movie->new(%$movie)); } $library->print();
    From this, you can see that $movie is a reference to each of the hashes contaning the movie info (startyear, endyear, etc). When I call movie->new(%$movie) it is a call to the new function in the package movie, passing in the de-referenced hash containing the movie information. new is expecting a hash of stuff and deals with it appropriately when it creates the movie object. As for copying it, you don't have to do this - You can store this information in a myriad of ways (I would suggest a read-only XML file that you can easily backup, which your scripts can load easily)
  3. It won't get a value if I don't give it one. I didn't implement run_time as I was just trying to show a quick sample of how to create an OO object. Implementing this should be pretty straight-forward (and I would expect no end_year would be handled appropriately by run_time

Heh, certainly looks pretty meaty. In a more OO fashion, I would expect lookup to be a single function within a library class, that returns an object from the library (movie, tv show, whatever). Then getting any of the underlying state information of the object that the caller wants is placed back onto the caller, who simply asks the object for that piece of information.

UPDATE: You can copy the original sample I provided as-is, stick it all into a single script, change the use 5.16.2; line to use 5.8.8;, save it and run it - It should work just fine (though might complain about say (not sure when this feature was implemented), which you can just change to print.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1124237]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-25 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found