FAQ
Hello,

I'm developing a webmin module for arp-scan. If you're not familiar with
webmin, it's a web interface to linux/unix administration. The first
release was some time around 1995, right after or around when Perl 5 was
released. Webmin is programmed in Perl 5 and is basically a web server
and collection of CGI scripts in Perl. Modules can be developed in Perl
that perform various linux/unix functions, and make use of the webmin API,
basically a collection of perl functions, to easily accomplish a user
interface.

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?

Here is an example from a typical module. It's the file 'log_parser.pl'
in the module 'shell' which basically acts like a command line. This is
the complete file:

# log_parser.pl
# Functions for parsing this module's logs

do 'shell-lib.pl';

# parse_webmin_log(user, script, action, type, object, &params)
# Converts logged information from this module into human-readable form
sub parse_webmin_log
{
local ($user, $script, $action, $type, $object, $p) = @_;
if ($action eq "run") {
return &text('log_run', "<tt>$p->{'cmd'}</tt>");
}
elsif ($action eq "clear") {
return $text{'log_clear'};
}
}

That's it. Here's another example, the beginning of index.cgi in the same
module, which is run when the user tries to access the module through the
webmin web interface.

#!/usr/bin/perl
# index.cgi
# Show the shell user interface

require './shell-lib.pl';
%access = &get_module_acl();
&ReadParseMime() if ($ENV{'REQUEST_METHOD'} ne 'GET');
&ui_print_header(undef, $text{'index_title'}, "", undef,
$module_info{'usermin'} ? 0 : 1, 1,
undef, undef, undef,
"onLoad='window.scroll(0, 10000); document.forms[0].cmd.focus()'");

$prevfile = "$module_config_directory/previous.$remote_user";
if ($in{'clearcmds'}) {
&lock_file($prevfile);
unlink($prevfile);
&unlock_file($prevfile);
&webmin_log("clear");
}
else {
open(PREVFILE, $prevfile);
chop(@previous = <PREVFILE>);
close(PREVFILE);
}
$cmd = $in{'doprev'} ? $in{'pcmd'} : $in{'cmd'};

The file continues, but I'm stopping it there for brevity. Any
suggestions? Thanks for your help and advice.

Regards,

John Refior

Search Discussions

  • Gunnar Hjalmarsson at Jan 24, 2009 at 2:46 am

    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
  • Dr.Ruud at Jan 24, 2009 at 2:50 pm

    Gunnar Hjalmarsson wrote:

    sub trim {
    for ( $_[0] ) {
    s/^\s+//;
    s/\s+$//;
    }
    }
    The following respects the environment better. :)

    sub trim {

    if ( defined wantarray() ) {

    my @_s = @_; # copy

    s/\s+$//, s/^\s+//
    for grep defined(), @_s;

    return wantarray() ? @_s : $_s[0];
    }

    s/\s+$//, s/^\s+//
    for grep defined(), @_; # in place

    return;
    }

    --
    Ruud
  • John Refior at Jan 24, 2009 at 5:56 pm

    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
    Gunnar,

    This may be a useful workaround for me, I'll give it a try. Thanks
    for your response.

    Sincerely,

    John

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 21, '09 at 9:12p
activeJan 24, '09 at 5:56p
posts4
users3
websiteperl.org

People

Translate

site design / logo © 2023 Grokbase