Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

object oriented module question

by convenientstore (Pilgrim)
on Sep 07, 2007 at 01:37 UTC ( [id://637562]=perlquestion: print w/replies, xml ) Need Help??

convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

Have a question regarding writing a module. Is there a way to include another module inside of a module so that while blessing them, you can use it to assign. I need to use Text::CSV since fields that I will be breaking apart is usually separated by comma, but will need to use Text::CSV to make one field with multiple comma inside of double quote as one field How is this done while blessing the class?

Replies are listed 'Best First'.
Re: object oriented module question
by rhesa (Vicar) on Sep 07, 2007 at 02:20 UTC
    Inheritance is one option, but you could also use delegation. Create a Text::CSV object, and store it inside your own object. You can then access that object to perform the actions you need.
    package My::Module; use Text::CSV; sub new { my $class = shift; my $self = { _text_csv => Text::CSV->new( ... ), # we store the object her +e }; bless $self, $class; } sub some_method { my $self = shift; # we use it here my $result = $self->{_text_csv}->some_text_csv_action( @input ); }
      Note: Not advisable you create the object w/i the only, paramaterless constructor.

      Should you wish to use something-else as the CSV parser or want to do some other stuff (such as testing) w/o requiring Text::CSV about, now you're stuck.

      If anything, create a function/method and feed it in. p.s. I know it is only an example.

        I'd say I do this (object blessed into my object) pretty routinely and it hasn't bitten me yet. I use this primarily when I want to use the functionality of an object within my object without really extending the existing object.

        It seems to me that whether you make your object inherit from (@ISA) or bless it into an object you'll still need the parent (Text::CSV) anyway. I'm not trying to nitpick, just want to know I understand things correctly. Am I missing something?

      For a good example, check out the source code for Parse::CSV It does pretty much exactly what the previous commentor is describing. (full disclosure, I wrote it)
        full disclosure, I wrote it
        None needed, you're anonymous ;)

        -David

Re: object oriented module question
by atemon (Chaplain) on Sep 07, 2007 at 02:02 UTC

    Inheritance in Perl is by using @ISA variable. Check this.

    package MyPackage; use vars qw(@ISA); @ISA = qw(Text::CSV); ... ... rest of your code ... ...
    here MyPackage is a Text::CSV and you can access the functions in Text::CSV like any other function of MyPackage. please refer to perlboot for details.

    Cheers !

    --VC



    There are three sides to any argument.....
    your side, my side and the right side.

Re: object oriented module question
by erroneousBollock (Curate) on Sep 07, 2007 at 02:06 UTC
    Is there a way to include another module inside of a module so that while blessing them, you can use it to assign.
    Could you explain this a bit more clearly? (perhaps with an example, and labels like A,B,C)

    I need to use Text::CSV since fields that I will be breaking apart is usually separated by comma, but will need to use Text::CSV to make one field with multiple comma inside of double quote as one field
    Luckily, my human parser capabilities managed to work in an environment of changing sentence delimiters ;)

    Text::CSV already does have the possibility of parsing CSV fields that contain commas, provided those fields are double-quoted.

    Perhaps I've mis-parsed the meaning of your request?

    Update: Well apparently vcTheGuru's internal parser works *much* better than mine.

    -David

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://637562]
Front-paged by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-19 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found