I loaded one module into my package like this:
and afterwards I load the same module into main package:In MyClass.pm: { package MyClass; use mymodule; # call some function from mymodule }
The problem is that I can normally call functions from mymodule within MyClass but calling the same functions within main throws an error about functions not being defined. I don't have any package declarations in mymodule.pm; it's just a file with a lot of functions.In myapp.pl: use mymodule; # call some function from mymodule
I resolved this by putting use mymodule in MyClass outside of package definition and brackets and now everything works fine, but I still don't know why the previous code failed to work.
Can it be because of Class::Contract? I use it to construct a class in MyClass. Does it somehow obscure loaded functions in MyClass so that no other packages can use it? (I know it sounds funny and probably is wrong, but that's the only reason I can think of.)
Any suggestions/descriptions/pointers to docs/slaps are appreciated.
Update: sample test code similar to the original and it doesn't work:
test_loading.pl:
#!/usr/bin/perl -w use strict; use warnings; { package MyClass; use Class::Contract; use test_func; contract { attr 'test'; ctor 'new'; impl { ${self->test} = test_me('within class'); }; }; }; package main; use test_func; test_me('in main');
test_func.pm
sub test_me { my $txt = shift; print "Exec of test_me with: $txt\n"; return $txt; } 1;
It throws an error:
Undefined subroutine &main::test_me called at test_loading.pl line 25
It works fine when I remove use test_func from MyClass and call test_me() like main::test_me() in class ctor.
In reply to Loading a module into many packages by arkturuz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |