I have an advantage over other people reading the original question, inasmuch as I can simply walk over to the OP and ask for clarification.

The basic situation is this:

in file FOO/Bar/Baz.pm package FOO::Bar::Baz; sub some_function { login(); } in file FOO/Bar.pl package FOO::Bar; sub login { # something interesting }

Bar.pl is the executable script (with its own defined package) that defines a function. She then wanted to call this function from the separate module file. The original version was slightly off from the functional one I posted.

Obviously, this is a not-so-hot approach. FOO::Bar is now dependent upon a function being defined in the symbol table in advance at some point in some separate file beyond its control. So if that function in that other package somehow changes or ceases to be, this module breaks and it's not immediately clear why. Side effects are bad.

I proposed two solutions. (1) is to rip out the function into a separate module and import it.

--- in the new module FOO/Bar.pm package FOO::Bar sub login { } --- in FOO/Bar/Baz.pm package FOO::Bar::Baz use FOO::Bar; sub some_function { FOO::Bar::login(); } --- in the script (remove the login function and proceed as normal)

This is better inasmuch as it's now quite clear that the function is declared in a new module that's explicitly used by the original module. Hopefully you'd be more careful modifiying it, and at a minimum it'd be easier to spot the error.

But, it's still tying FOO::Bar into the specific method that works in this one case. A (better?) more generic approach would be to use some sort of a function lookup table and define the callback method you want. Roughly,

--- in FOO/Bar/Baz.pm my %function_table; sub set_callback { my ($key, $function) = @_; $function_table{$key} = $function; } sub some_function { if (defined $function_table{'login'}) { $function_table{'login'}->(); } else { die "No callback declared for login"; } } --- in FOO/Bar.pl use FOO::Bar; sub login { } FOO::Bar->set_callback('login', \&login);

FOO::Bar's some_function is now completely decoupled from the original login function, and you can drop in any new one you'd like. I wouldn't be surprised if there's some CPAN module to give an enhanced version of this sketched out functionality.


In reply to Re: Paths to Packages by jimt
in thread Paths to Packages by mikasue

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.