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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |