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 );
}
| [reply] [d/l] |
|
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.
| [reply] |
|
| [reply] |
|
|
|
|
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)
| [reply] |
|
full disclosure, I wrote it
None needed, you're anonymous ;)
-David
| [reply] |
Re: object oriented module question
by atemon (Chaplain) on Sep 07, 2007 at 02:02 UTC
|
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.
| [reply] [d/l] |
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
| [reply] |