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?
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |