I have the following initialisation code in a system tray applet:
use Gtk3 -init;
use Glib::Object::Introspection;
eval {
Glib::Object::Introspection->setup(
basename => 'Notify',
version => '0.7',
package => "MyProgram::Notify",
);
};
if ($@) {
say "no notify because setup failed: $@";
$use_notify = 0;
} else {
MyProgram::Notify->init();
}
That code is based upon fdpowermon, but seems to come more or less from the Exception handling examples in the POD of Glib::Object::Introspection.
But perlcritic (at level 3) argues about it:
Return value of eval not tested at line …
So I tried to rewrite that with Try::Tiny:
use Gtk3 -init;
use Glib::Object::Introspection;
use Try::Tiny;
try {
Glib::Object::Introspection->setup(
basename => 'Notify',
version => '0.7',
package => "MyProgram::Notify",
);
} catch {
say "no notify because setup failed: $@";
$use_notify = 0;
} finally {
if (!$@) {
MyProgram::Notify->init();
}
}
But then perl argues:
Can't locate object method "new" via package MyProgram::Notify::Notification
While I do see that especially the finally
block is not a real improvement, I do not understand why using Try::Tiny makes such a difference with regards to the package created by Glib::Object::Introspection.
Or is there a better way than Try::Tiny to make this code more elegant and more readable while keeping perlcritic
happy?