nysus has asked for the wisdom of the Perl Monks concerning the following question:

Here are the first few lines of a class I've created:

package FFMech; $ENV{'DISPLAY'} = ':0.0'; use X11::GUITest qw/SendKeys FindWindowLike ClickWindow SetEventSendDe +lay/; use Moose; use Modern::Perl; use MooseX::NonMoose; extends 'WWW::Mechanize::Firefox';

I'd like to access the SendKeys subroutine from an object. To do so, I created a wrapper method to the SendKeys subroutine like so:

sub mySendKeys { my $self = shift; my $arg = shift; &X11::GUITest::SendKeys($arg); }

This worked fine. But now I'd like to use other subroutines from the the X11::GUITest module. Rather than create a new wrapper for each subroutine, I'm wondering if there might be a better approach. Thanks!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Moose: Accessing subroutines in packages used by Moose class
by Corion (Patriarch) on Apr 17, 2016 at 11:00 UTC

    You can use code to write your wrappers:

    require X11::GUITest; for my $imported (qw/SendKeys FindWindowLike ClickWindow SetEventSendD +elay/) { my $func = "X11::GUITest::$imported"; my $code = do { no strict 'refs'; \&{"$func"} }; my $delegate = sub { my( $self, $arg ) = @_; $code->($arg); }; # Install as mySendKeys etc: no strict 'refs'; *{"my$imported"} = $delegate; };

      Awesome, thanks! Amazing how you can whip that kind of code out. It literally would have taken me a day or more to figure out how something like this could be accomplished.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

      I gave it a whirl but X11::GUITest complained with:

      Use of uninitialized value $titlerx in regexp compilation at /usr/lib/ +x86_64-linux-gnu/perl5/5.20/X11/GUITest.pm line 347. Use of uninitialized value $titlerx in regexp compilation at /usr/lib/ +x86_64-linux-gnu/perl5/5.20/X11/GUITest.pm line 347. ... Repeated many more times ... Use of uninitialized value $titlerx in regexp compilation at /usr/lib/ +x86_64-linux-gnu/perl5/5.20/X11/GUITest.pm line 347. Use of uninitialized value in subroutine entry at /usr/lib/x86_64-linu +x-gnu/perl5/5.20/X11/GUITest.pm line 545.

      Do you happen to have an inkling of what the issue might be? If not, don't worry about it, I'll just write the wrapper manually. I learned something. Maybe this technique just doesn't work on this particular module.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        Without seeing your code and without knowing about what X11::GUITest expects as the parameters, I can't tell.

Re: Moose: Accessing subroutines in packages used by Moose class
by Arunbear (Prior) on Apr 18, 2016 at 10:55 UTC

    Updated: Please ignore as this won't be useful for the OP problem due to not handling the package parameter (as noted by Corion).

    Moose can create such wrappers for you e.g.
    package Bogus; use Data::Dump 'pp'; sub foo { printf "foo got %s\n", pp([@_]) } sub bar { printf "bar got %s\n", pp([@_]) } sub baz { printf "baz got %s\n", pp([@_]) } package Tester; use Moose; has 'bogus' => ( is => 'bare', default => 'Bogus', handles => [qw( foo bar baz )] ); package main; my $tester = Tester->new; $tester->bar('bar'); $tester->foo('ram'); $tester->baz('ewe');
    See Moose::Manual::Delegation

      I think that Moose won't write the code that handles delegating to a function (by shifting $_[0] away). But other than that, letting Moose handle the delegation is certainly a much better approach than handling the delegation yourself (once you've bought into Moose).

        D'oh! You're quite correct, though it can be fixed with additional hackery:
        package Bogus; use Data::Dump 'pp'; sub foo { printf "foo got %s\n", pp([@_]) } sub bar { printf "bar got %s\n", pp([@_]) } sub baz { printf "baz got %s\n", pp([@_]) } package MyBogus; use Moose; use MooseX::NonMoose; extends 'Bogus'; around [qw( foo bar baz )] => sub { my $orig = shift; my $self = shift; $orig->(@_); }; package Tester; use Moose; use MyBogus; has 'bogus' => ( is => 'bare', default => sub { MyBogus->new }, handles => [qw( foo bar baz )], ); package main; my $tester = Tester->new; $tester->bar('bar'); $tester->foo('ram'); $tester->baz('ewe'); __END__ prints: bar got ["bar"] foo got ["ram"] baz got ["ewe"]