Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Different behavior for Moo and Moose with Wx?

by stefbv (Deacon)
on Jun 06, 2015 at 10:20 UTC ( #1129292=perlquestion: print w/replies, xml ) Need Help??
stefbv has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on porting a wxPerl GUI application from Moose to Moo. All works just fine, excepting the Menu, here is the code to show the problem:

### MyMenuBar package MyMenuBar; use Moo; #use Moose; #use MooseX::NonMoose::InsideOut; use Wx qw(:everything); extends 'Wx::MenuBar'; sub FOREIGNBUILDARGS { return (); } sub BUILD { my $self = shift; $self->Append( MenuBar::Item->new, 'Item'); # comment this line to # get the object info return $self; } ### MenuBar::Item package MenuBar::Item; use Moo; #use Moose; #use MooseX::NonMoose::InsideOut; use Wx qw(:everything); extends 'Wx::MenuItem'; has 'item_quit' => ( is => 'ro', lazy => 1, builder => '_build_item_quit', ); sub _build_item_quit { my $self = shift; return Wx::MenuItem->new( $self, wxID_EXIT, '&Quit', 'Quit', wxITEM_NORMAL, undef # if defined, this is a sub-menu ); } sub FOREIGNBUILDARGS { return (); # Wx::Menu->new() takes no arguments } sub BUILD { my $self = shift; $self->Append( $self->item_quit ); return $self; } ### MyStatusBar package MyStatusBar; use Moo; #use Moose; #use MooseX::NonMoose::InsideOut; use Wx qw(:everything); extends 'Wx::StatusBar'; sub FOREIGNBUILDARGS { my $self = shift; my %args = @_; return ( $args{parent}, -1, $args{style}, $args{name}, ); } ### MyFrame package MyFrame; use Moo; #use Moose; #use MooseX::NonMoose::InsideOut; use Data::Printer; use Wx qw(:everything); extends 'Wx::Frame'; use MyStatusBar; use MyMenuBar; sub FOREIGNBUILDARGS { my $self = shift; my %args = @_; return ( undef, -1, 'Wx with Moo', [ -1, -1 ], [ -1, -1 ], wxDEFAULT_FRAME_STYLE, ); } sub BUILD { my $self = shift; my $panel = Wx::Panel->new( $self, -1, [ -1, -1 ], [ -1, -1 ] ); my $sizer = Wx::BoxSizer->new(wxVERTICAL); my $status_bar = MyStatusBar->new( parent => $self, name => 'sb', style => 0, ); p $status_bar; $status_bar->SetStatusText('Status', 0); my $menu_bar = MyMenuBar->new; p $menu_bar; $self->SetMenuBar($menu_bar); $panel->SetSizerAndFit($sizer); return $self; } ### main package main; use 5.010; use strict; use warnings; use Wx; my $app = Wx::SimpleApp->new; my $frame = MyFrame->new; $frame->Show(1); $app->MainLoop;

and the output is:

MyStatusBar { Parents Wx::StatusBar public methods (2) : FOREIGNBUILDARGS, new private methods (0) internals: {} } Can't locate object method "item_quit" via package "Wx::Menu" at wx-mo +o-menu-problem.pl line 58.

If I comment the line 18, it gets to the point to also dump the object info for the menu:

Wx::MenuBar { Parents Wx::Window public methods (28) : Append, Check, Enable, EnableTop, FindItem, +FindMenu, FindMenuItem, ... private methods (0) internals: 49230256 }

The problem is obvious, the class should be MyMenuBar not Wx::MenuBar, but with Wx::StatusBar is all OK, what I'm doing wrong?

It also works fine if instead of Moo, I use Moose and MooseX::NonMoose::InsideOut.

Thank you,
Stefan

Update: Environment: Citrus Perl 5.016003, Wx 0,9927, wxWidgets 2.9.4

Update2: Fix extended class, thanks Anonymous Monk.

Replies are listed 'Best First'.
Re: Different behavior for Moo and Moose with Wx?
by Anonymous Monk on Jun 07, 2015 at 01:16 UTC

    The problem is obvious, the class should be MyMenuBar not Wx::MenuBar, but with Wx::StatusBar is all OK, what I'm doing wrong?

    I get the same problem with Moo 1.006001 and Moo-2.000001

    I think its a bug in Moo but I'm not confident in that

    In addition, you're confused, your class is called MenuBar::Item but it extends 'Wx::Menu' not a Wx::MenuItem

      Thanks for the feedback, unfortunately fixing the extended class name does not solve the problem :(

        Thanks for the feedback, unfortunately fixing the extended class name does not solve the problem :(

        Well, not surprising, fixing thinkos rarely fixes "bugs" :)

        Well, it might be considered a bug that Moo isn't as helpful as Moose, Moose actually checks the class is blessed into the right, Moo doesn't

        Quick workaround is

        sub MenuBar::Item::BUILD { my $self = shift; bless $self, __PACKAGE__ ; $self->Append( $self->item_quit ); return $self; }

        Now why doesn't Wx::Menu obey the common rules of subclassing/inheritance, well not all things in wxPerl are mean to be subclasses, and indeed menu/menubar are indeed odd candidates for that ... some of this is explained in Re: wxperl-users scalar reference objects?

        As you can see a Wx::Menu isn't a hashref, its a scalar ref

        FWIW, not everything needs to be subclasses, ex wxperl_usage

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1129292]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2017-07-25 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (363 votes). Check out past polls.