On Sun, Jan 23, 2000 at 12:27:20PM -0500, [EMAIL PROTECTED] wrote:
>   JNP> On Sun, Jan 23, 2000 at 12:05:44PM -0500, [EMAIL PROTECTED] wrote:
>   JNP> What is wrong with this:
>   >> 
>   JNP> $watcher->{data} = ...; #?
>   >> 
>   >> that breaks the OO rules. one shoudl never assume knowledge about the
>   >> internal stucture of an object. there should be a method/attribute for that.
> 
>   JNP> But you can't access the internal structure of a watcher except via
>   JNP> methods. The hash part is entirely empty.
> 
> then i am curious, what and how do you store the info for a watcher? if
> not in the hash of $watcher, then what? a pseudohash? or closures?

Magic:

  static SV *wrap_watcher(void *ptr, HV *stash, SV *temple) {
    SV *ref;
    MAGIC **mgp;
    MAGIC *mg;

    assert(ptr);
    assert(stash);

    if (!temple)
        temple = (SV*)newHV();
    else
        SvREFCNT_inc(temple);
    if (SvOBJECT(temple))
        croak("Can't attach to blessed reference");
    assert(!SvROK(temple));
    assert(mg_find(temple, '~') == 0); /* multiplicity disallowed! */

    ref = newRV_noinc(temple);
    sv_bless(ref, stash);

    mgp = &SvMAGIC(temple);
    while ((mg = *mgp))
        mgp = &mg->mg_moremagic;

    New(0, mg, 1, MAGIC);
    Zero(mg, 1, MAGIC);
    mg->mg_type = '~';
    mg->mg_obj = (SV*) ptr;  /* NOT refcnt'd */
    *mgp = mg;

    return ref;
  }

The real data is not accessible from perl, except by methods.

>   JNP> You can also add methods with a prefix.  For example,
> 
>   JNP>   package Event::Watcher;
>   JNP>   sub uri_data { ... }
> 
> is that documented?

Not yet.

> what prefix is allowed (any string)? i assume the
> prefix is to avoid method name collisions. this is not cool OO coding
> either IMO. i will write damian conway to see what he thinks about that
> idea.

I think it's cool OO once it's documented.  Perl's OO is pretty strange.

-- 
"Never ascribe to malice that which can be explained by stupidity."
                            via, but not speaking for Deutsche Bank

Reply via email to