Professional Web Applications Themes

Newbie OO question - PERL Miscellaneous

In article <EbkRa.331382$fC.2436421news.easynews.com>, "Ed W" <dodgynewsgroupsewildgoose.demon.co.uk> wrote: > Hi, I do hope this isn't a question asked 50 times a month, but I'm just > getting to grips with the OO side of perl and struggling top get my head > round the sheer volume of info. Please feel free to direct me to the man > pages, but specific sections would be useful > > I have an existing class that I would like to derive some methods from (it's > compress:PPMd for what it's worth). The ->new function actually returns > something which is not a hash (in ...

  1. #1

    Default Re: Newbie OO question

    In article <EbkRa.331382$fC.2436421news.easynews.com>,
    "Ed W" <dodgynewsgroupsewildgoose.demon.co.uk> wrote:
    > Hi, I do hope this isn't a question asked 50 times a month, but I'm just
    > getting to grips with the OO side of perl and struggling top get my head
    > round the sheer volume of info. Please feel free to direct me to the man
    > pages, but specific sections would be useful
    >
    > I have an existing class that I would like to derive some methods from (it's
    > compress:PPMd for what it's worth). The ->new function actually returns
    > something which is not a hash (in fact I think it may be a C++ reference),
    > but I haven't managed to get my debugger to tell me what it is. So what I
    > think I would like is for my new derived class to use a hash for data
    > storage, but somehow NOT to have to overload every existing method.
    >
    > To abstract this a bit. Imagine a base class which returns a blessed scaler
    > (via a class->new method). Later I want to derive a new class from this
    > which needs a lot of attribute storage, and hence a scaler is out, but I
    > don't want to have to overload all the methods to avoid breaking things.
    >
    > Can this be done? Or is this what happens anyway, and I just failed to read
    > the manual properly....?
    >
    > Thanks for basic tuition on this
    >
    > Ed
    >
    >
    perldoc perltoot

    it's all you need... that and maybe damian's book!:

    [url]http://www.manning.com/Conway/[/url]


    --
    Michael Budash
    Michael Budash Guest

  2. #2

    Default Re: Newbie OO question


    "Ed W" <dodgynewsgroupsewildgoose.demon.co.uk> wrote in message
    news:EbkRa.331382$fC.2436421news.easynews.com...
    > Hi, I do hope this isn't a question asked 50 times a month, but I'm just
    > getting to grips with the OO side of perl and struggling top get my head
    > round the sheer volume of info. Please feel free to direct me to the man
    > pages, but specific sections would be useful
    >
    > I have an existing class that I would like to derive some methods from
    (it's
    > compress:PPMd for what it's worth). The ->new function actually returns
    > something which is not a hash (in fact I think it may be a C++ reference),
    > but I haven't managed to get my debugger to tell me what it is. So what I
    > think I would like is for my new derived class to use a hash for data
    > storage, but somehow NOT to have to overload every existing method.
    >
    1. Have you considered using Data::Dumper to yze the object?
    2. A look at the Compress::PPMd source code
    ([url]http://search.cpan.org/author/SALVA/Compress-PPMd-0.08/MANIFEST[/url]) shows that
    this module -- considered by itself -- is not actually object-oriented.
    Rather, it's a "regular" Perl module which uses Exporter to export its
    functions. It uses XSLoader to load the extension written (I think) in C.
    The 'new' method is in the XS, not in the Perl. I don't know enough about
    this to comment further; perhaps another list-member can tell you more about
    XSLoader.



    James E Keenan Guest

  3. #3

    Default Re: Newbie OO question

    "Ed W" <dodgynewsgroupsewildgoose.demon.co.uk> wrote:

    : I have an existing class that I would like to derive some methods from (it's
    : compress:PPMd for what it's worth). The ->new function actually returns
    : something which is not a hash (in fact I think it may be a C++ reference),
    : but I haven't managed to get my debugger to tell me what it is. So what I
    : think I would like is for my new derived class to use a hash for data
    : storage,

    Subclassing (via ISA) is out, but an object from your class could
    easily have a field for holding a compress:PPMd object.

    : but somehow NOT to have to overload every existing method.

    Just because it would require a lot of typing? An AUTOLOAD{} method
    in your class could do the dirty work of inspecting the compress:PPMd
    object's capabilities and creating fresh methods for you.

    Jay Tilton Guest

  4. #4

    Default Re: Newbie OO question

    Hi Ed,

    "Ed W" <dodgynewsgroupsewildgoose.demon.co.uk> wrote in message news:<SItRa.362198$fC.2630720news.easynews.com>.. .
    > > perldoc perltoot
    > >
    > > it's all you need... that and maybe damian's book!:
    > >
    > > [url]http://www.manning.com/Conway/[/url]
    >
    > Thanks, I have read the perltoot page a few times, but it doesn't seem to
    > cover what happens if you want to inherit from an object that has, say, a
    > scalar as the underlying object storage, but I need somewhere to store some
    > extra object variables.
    >
    > I suspect that there is something clever possible involving closures, but
    > since I'm not quite ready for that yet, I was just wondering if there was a
    > standard way out of this...

    I know three different ways to do what you want:

    1 - use a "has-a" relation instead of a "is-a" one.
    Compress::PPMd::Compressor and Compress::PPMd::Decompressor have very
    few methods so you only need to write a few proxy methods to call
    them.


    2 - use a hash private to your package indexed by the Compress::PPMd
    objects (untested):

    package MyPkg;

    use Compress::PPMd;
    our ISA=qw(Compress::PPMd::Compressor);

    my %data; # here goes your data

    sub new {
    my $class=shift;
    my $this=$class->SUPER::new(_);
    $data{$this}={ foo => 'bar' }
    }

    # data(...) stores and retrieves your class object properties
    # $obj->data('foo') retrieves property 'foo'
    # $obj->data('bar', 'doz') sets property 'bar' as 'doz'
    sub data {
    my $this=shift;
    my $key=shift;
    if (_) {
    $data{$this}{$key}=$_[0];
    }
    else {
    return $data{$this}{$key}
    }
    }

    sub DESTROY {
    my $this=shift;
    delete $data{$this}
    eval { $this->SUPER::DESTROY(_) }
    }


    3 - tell me why you need to extend the package and maybe I will add
    that functionality to Compress::PPMd ;-)

    Bye,

    - Salva.
    Salvador Fandino Guest

Similar Threads

  1. Newbie Question: Biz Card Template Question
    By Thomas_Porter@adobeforums.com in forum Adobe Indesign Windows
    Replies: 4
    Last Post: May 30th, 08:08 AM
  2. A newbie with a newbie question
    By dusty_davis@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 5
    Last Post: October 14th, 07:54 AM
  3. A newbie question on SSO
    By Holysmoke in forum ASP.NET Security
    Replies: 2
    Last Post: October 8th, 01:53 PM
  4. Pen Tool Use Question. (Embarrassingly Newbie Question)
    By Bozo Schmozo in forum Macromedia Flash
    Replies: 0
    Last Post: November 12th, 10:00 PM
  5. [newbie] dbi question
    By tsheets in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 10th, 04:26 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139