Brutha has asked for the wisdom of the Perl Monks concerning the following question:
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.
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:
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?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', ...
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to wrap Win32::Ole into Moose
by Anonymous Monk on Feb 13, 2012 at 20:57 UTC | |
by Brutha (Friar) on Feb 14, 2012 at 06:55 UTC |