John Refior wrote:
The problem I am facing right now is that webmin doesn't use packages or
classes (it is not object-oriented). Instead, API functions and variables
are customarily accessed with 'do' or 'require', and variables are global
accross files. Local variables in the modules I've looked at are
generally declared with 'local' insteady of 'my' (meaning they have
dynamic scope rather than lexical scope), and many files have 'use vars
qw()' instead of 'our'. I am guessing that it was coded before 'my' was
part of Perl, when packages were just starting to be used, and when
object-oriented programming was all but unheard of in Perl, and it has not
been overhauled and updated (perhaps because that's an overwhelming task
or because it would break against deployments or third-party modules).
My question is, how do I access these library functions and global
variables in my module? Do I have any choice but to follow the example of
the other modules? Can I somehow act like these library functions and
global variables exist in packages somewhere?
Functions and global variables in a Perl 4 style library will belong to
the package from where the library is loaded. Consequently it's possible
to do something like:
# MyLib.pl
# --------
sub trim {
for ( $_[0] ) {
s/^\s+//;
s/\s+$//;
}
}
# MyFunctions.pm
# --------------
package MyFunctions;
do 'MyLib.pl';
1;
# main script
# -----------
use MyFunctions;
my $string = ' hello ';
MyFunctions::trim($string);
HTH
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/