moose - Perl export to child modules -
i have parent module myapp.pm:
package myapp; use moose; use base 'exporter'; our @export = qw(msg); sub msg { print "hello msg\n"; } 1;
which inherited child module myapp2.pm:
package myapp2; use moose; extends qw(myapp); 1;
and when used in app.cgi script this:
#!/usr/bin/perl use myapp2; msg();
i error message:
undefined subroutine &main::msg called @ app.cgi line 3.
so exported function not work in child class myapp2 works if use "use myapp" instead of "use myapp2". assume exported function should accessible child modules extending parent class. doing wrong.
inheritance changes how method calls handled; function calls or variable accesses (like our @export
) not affected.
instead of exporting function, use method:
use myapp2; myapp2->msg;
but in case, cleaner explicitly load myapp
in order import msg
function, , additionally load myapp2
in order load class.
use myapp; use myapp2; msg;
it advisable module either object oriented or offer interface via exported functions, not both.
Comments
Post a Comment