UPDATED due bug found by tobyink

Greetings monks,

I'm trying to figure out a way to have automatic error checking when invoking methods from a Win32::OLE object, for example:

my $sa = Win32::OLE->new("SiebelDataServer.ApplicationObject") or die +"failed"; $rcref = Variant(VT_I2|VT_BYREF, 0); $sa->LoadObjects($cfg, $rcref); x("a", $rcref); sub x { my ($tag, $rc) = @_; warn("[$tag] " . $sa->GetLastErrText()) unless $rc == 0) }

This way I will need to call x function every time after calling a Win32::OLE method. This seems to be quite boring.

Instead, I would like to encapsulate the Win32::OLE object inside a Moose object and execute the error checking automatically.

First thought that I had is using the Triggers in a attribute that has a reference to the Win32::OLE::Variant object, but this does not look like it will work because a trigger will be activated only when a writer method is invoked for the attribute or during the object construction.

After that I considered using AROUND_modifiers in a to be defined role, doing it for every method that invokes a Win32::OLE method internally. The AROUND would look like this (I didn't check if this code would work, just an example):

package Automatic:Check; use Moose::Role; around 'load_objects' => sub { my $orig = shift; my $self = shift; $rcref = Variant(VT_I2|VT_BYREF, 0); return $self->$orig(@_, $rcref); }; package SA::Application; use Moose; use Automatic::Check; has ole => ( is => 'ro', isa => 'Win32::OLE', builder => '_build_ole') +; sub _build_ole { return Win32::OLE->new("SiebelDataServer.ApplicationObject" or die + "failed"; } sub load_objects { my $self = shift; my $cfg = shift; die "error" unless ( $rcref == 0 ); $self->ole()->LoadObjects($cfg); } package main; my $cfg = 'somefile.cfg'; my $sa = SA::Application->new(); eval { my $sa->load_objects($cfg); }; if ($@) { die $sa->GetLastErrText(); }

But this does not seems to be lazy enough since I would need to create a new role for each different kind of Win32::OLE classes that I'm using.

Do you monks have any other suggestion that could improve this laziness?

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

In reply to getting automatic error checking with Win32::OLE and Moose by glasswalk3r

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.