#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    56
    Rep Power
    4

    Trying to Learn OO Perl


    First off I'd like to say I have no experience in oo programming, this is my first go at it. After reading many different tutorials and pieces from the camal book I think I'm starting to understand it (I'm a little slow). I'm having a hard time figuring out how to work the concepts into my projects. So lets say I have a project where I'm working with people and each person has a name, DOB, age, hair color and eye color. Seems like this would be a good candidate for an object. My questions are here:

    1. Every tutorial I've seen is passing predetermined values into the object. Is it best to do any complex calculations or pattern matching in you main program and call your methods to just manage the values? Or do you develop complex methods that will do everything for you?

    2. Does the hash you create with your object store all values? How do you pass it around for processing and printing?

    3. Lets say I have a program where I call a bunch of different subroutines that gather a bunch of different data and I need to do this multiple times. I gather the data that I need and put it into arrays manually to be used later. Is this type of program a good candidate for 00 programming?

    4. Whats best way to make changes to and access your objects?

    Any help will be appreciated and if some of my questions don't make sense, sorry I'm still learning.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    I know what you mean. Most programming books are good at explaining concepts, but not providing any practical application of the concepts. It's a struggle to begin to incorporte the ideas into your own programs.

    OO and perl can be a strange mix, because it wasn't baked in to the language from the beginning. What's there works pretty well, but because we can program procedurally, and because most scripts are pretty short, it's not something I use often myself. My primary exposure to OO in perl is in using other people's modules from CPAN.

    I do use OO primarily in Objective-C, but the concepts are exactly the same in any language.

    So what I mean, is that if you are developing an application in Cocoa on the Mac, they have built an application framework built around the MVC concept: model, view, controller. Their view is the user interface which you basically draw, the model is an object, but basically a sort of data structure with getter and setter methods for retreiving data out of the object, and the controller does the work of asking for data, and putting it in the view.

    And the same sort of thing exists in perl. I have often used CGI::Application to create web applications. A CGI::Application is an object. When you create this object, you get an application life-cycle with pre-built routines you can override to change the behavior, or use the default.

    The $app object is the controller in an MVC pattern. The data is often a database used directly, but it could be some other data object you provide. The view is the HTML/CSS template which is filled out by the controller.

    ---

    I don't see OO as something essential in every perl program. Making complex inheritence trees is usually overkill.

    But if you find a script that is becoming overly long or complex, it's a good time to think about breaking data types out into separate objects so the main logic flow of the script will be easily readable.

    As an example, I created a perl script which read NOTAMs (Notice to Airmen) off the web and stored them in a database. Later retreived them and compared them to other events.

    Conceptually, it was easy to see the NOTAM as an object. By moving that into a separate NOTAM class, I could validate the NOTAM at initialization (made sure the parsed HTML made sense, and could be categorized properly). If valid, the NOTAM could be saved to the database with one method. I could retreive NOTAMs from the database in a simliar method, and ask for any information, such as category or text.

    With these methods in a separate class, I was easily able to include the behavior in any script I wrote which included NOTAM objects.
  4. #3
  5. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    1. Every tutorial I've seen is passing predetermined values into the object. Is it best to do any complex calculations or pattern matching in you main program and call your methods to just manage the values? Or do you develop complex methods that will do everything for you?
    The method can have complex processing methods of it's own. Objects can parse XML if you want, so no, they don't need to be spoon-fed.

    2. Does the hash you create with your object store all values? How do you pass it around for processing and printing?
    Yes, the hash contains the data. It should be treated as a private data type, accessible from other scripts only through the object itself. Internally, the data is available to methods inside the object through a reference which is the first argument pull off the @_ array inside the subroutine. It's usually referred to as $self.

    3. Lets say I have a program where I call a bunch of different subroutines that gather a bunch of different data and I need to do this multiple times. I gather the data that I need and put it into arrays manually to be used later. Is this type of program a good candidate for 00 programming?
    Yes. Custom data types with initialization, setter and getter methods are very good candidate for objects.

    4. Whats best way to make changes to and access your objects?
    Only through the object's methods.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    56
    Rep Power
    4
    Thanks a lot for the replies. I appreciate the help. Lets say I have a script that works with multiple sub routines or modules that all boil down to getting information from an array via regular expressions. I can pass the arrays right into my method, parse it out and return the values one at a time to my object? I say one at a time because there are certain REGEX's I've built where I can get 2 or 3 pieces of information from the same line. Would I need to break this up into 2 or 3 methods and return the values one at a time? It doesn't look like you can have multiple returns from a method is this true? Then would I be able to pass the entire object (hash) into an external subroutine for use or even to just print? Then change the object, through the interface and due it again? Any arrays I'm passing into the object would need to be built in the main program or a different module, correct? Well not really but if I want to remove pieces fromk it as I capture them (splice) I would need to keep the updated list somewhere. Otherwise I could issue a system and get new information every time my method is invoked.

    So at the end of the day I could create an object with methods, create an interface to set and get the object (a subroutine?) Then setup a script to request an object, which will come nicely packaged with any related piece of data in a hash, and pass the object into an external sub routine to get a result. Rinse and repeat??

    Am I getting warm?
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    I can't give a detailed reply at the moment, but a subroutine can return a list, a scalar, or a reference to either. It can even return an object, so you aren't limited in any way.

    The more you describe what you are trying to do, the better suggestions you'll receive from the group here. There are lots of options.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    56
    Rep Power
    4
    Thanks again, I'm close to having this ironed out. I was able to create an object constructor, build a method that parses my array and returns the correct value to the main script. I am now trying to pass the object into a new sub and print it but cannot figure out the syntax to print it. Any ideas?
  12. #7
  13. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    I haven't seen your object definition, but you can define a print method to return whatever data you like.

    Code:
    sub print {
    	my ($self) = @_;
    	printf( "Name:%s %s\n", $self->firstName, $self->lastName );
    }
    Then from outside the object you could just call:
    Code:
    $object->print;
    to get the result you want.

IMN logo majestic logo threadwatch logo seochat tools logo