Hello all!

I loaded one module into my package like this:

In MyClass.pm: { package MyClass; use mymodule; # call some function from mymodule }
and afterwards I load the same module into main package:
In myapp.pl: 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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.