I know about OO for a long time, but I am quite new to Moose. I read the cookbook and much more, but I am still not sure, when to use what construct and would appreciate some advice. Thank You for pushing me forward.

I am working with ActivePerl 5.14 on Windows and a have a library which I can access via Win32::OLE. I would like to wrap the OLE objects into Moose Objects and I am still not sure which way is most promising. In short, I am a bit lost. So I start with an example, solving that will show me a way to the more difficult things.

How would You wrap a Dictionary-Object

A Dictionary object is a kind of Visual-Basic answer to a Perl hash. In Perl I could use it as follows:

use Win32::OLE; my $dict = Win32::OLE->CreateObject("Scripting.Dictionary"); $dict->Count(); # how many Items $dict->Add( $key, $value); my $val = $dict->Item($key);

My first approch was something like this (shortened)

use Win32::OLE; use MooseX::Declare; class Dictionary { has '_DictObj' => ( is => 'ro', isa => 'Object', required => 1, handles => [ qw/Count Item Add/ ], ); }; # somewhere else my $dict = Dictionary->new(Win32::OLE->CreateObject("Scripting.Diction +ary")); $dict->Add($key, $value);

Although this works, I do not really like it, as it is not really much help. My first extension to this was creating a subtype:

package MyOleTypeLib; use Moose::Util::TypeConstraints; sub isDictionary { my $val = shift; return 0 unless $val->isa('Win32::OLE'); my @lst = Win32::OLE->QueryObjectType($val); return ( $lst[0] eq 'TSATLLib' # different for real ... and $lst[1] eq 'ITSEnt'); # ... VBS dictionary } subtype 'Dictionary', as 'Object', where { isDictionary($_) }, message { "$_ is not a OLE-Dictionary!"}; # in my Class: class Dictionary { has '_DictObj' => ( isa => 'Dictionary', ...
This is better, but now my real problems start. But before I continue, I would like to ask You. Would code the same approch? Are there better ways?

My real life dictionary has besides other properties some descriptive text which I can extract by calling $OleDict->GetFile(1);. I could also use delegation, but I would prefer to have a kind of attribute. I could write methods for it:

class Dictionary { ... method get_text { return _DictObj->GetFile(1); } method set_text (Str $newtext) { return _DictObj->SetFile(1, $newtext); }

This is not really elegant, there should be better ways in Moose. It would be great to have something working like:

my $text = $moose_dict->text; $moose_dict->text($newtext);

But here starts unknown (not understood) territory. Should I set up an attribute 'text' and overload/rewrite the accessors?

Do I follow a completely wrong approch?

My head is full of coercion, roles, currying delegation and more, might have something to do with wood and trees that I do not see. So thank You for all hints.

And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
(Terry Pratchett, Small Gods)


In reply to How to wrap Win32::Ole into Moose by Brutha

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.