[prev in list] [next in list] [prev in thread] [next in thread] 

List:       webmin-l
Subject:    Re: [webmin-l] Webmin code
From:       "Jamie Cameron" <jcameron () webmin ! com>
Date:       2008-10-31 21:00:20
Message-ID: 1225486819.29069 () webmin ! com
[Download RAW message or body]

On 31/Oct/2008 12:27 Tracy Reed wrote ..
> I'm working with an ancient version of webmin (1.310) that a client
> has hacked up and wants me to write a custom module for. I notice
> things like:
> 
> # foreign_call(module, function, [arg]*)
> # Call a function in another module
> sub foreign_call
> {
> local $pkg = $_[0] ? $_[0] : "global";
> $pkg =~ s/[^A-Za-z0-9]/_/g;
> local @args = @_[2 .. @_-1];
> $main::foreign_args = \@args;
> local @rv = eval <<EOF;
> package $pkg;
> &$_[1](\@{\$main::foreign_args});
> EOF
> if ($@) { &error("$_[0]::$_[1] failed : $@"); }
> return wantarray ? @rv : $rv[0];
> }
> 				
> What is the purpose of this foreign_call and foreign_module stuff?
> Someone really likes the ternary operator. My perl is a bit
> rusty...let's see if I can figure out what is going on here:
> 
> # If $_[0] is defined let it be $pkg otherwise make $pkg = "global"
> local $pkg = $_[0] ? $_[0] : "global";
> # Remove anything not a number or letter from $pkg and replace with _
> $pkg =~ s/[^A-Za-z0-9]/_/g;
> # assign the args that we were passed in on $_ to local array @args
> local @args = @_[2 .. @_-1];
> # What is $main? Not sure. But I think foreign_args is an attribute in
> # it to which we are assigning a reference to @args
> $main::foreign_args = \@args;
> # We pass the next few lines up to EOF into eval and put the result
> # into local array @rv
> local @rv = eval <<EOF;
> # Not sure what this does. Declare the following code to be a package?
> package $pkg;
> # Call a function named by $_[1] passing in a reference to an array
> # of...ok, this is the args originally given to @foreign_call
> &$_[1](\@{\$main::foreign_args});
> # End of stuff being eval'd
> EOF
> # If $@ is defined then the last eval emitted some sort of error.
> # Call the error handling function it failed and the args
> if ($@) { &error("$_[0]::$_[1] failed : $@"); }
> # Where did this wantarray thing come from? If it is true return array
> # @rv and if not return scalar $rv[0]. I guess if the eval produces an
> # array or scalar we return the correct type.
> return wantarray ? @rv : $rv[0];
> 
> Ok, so why did we do all that? We wrapped the function in a heinously
> complicated way and I don't get it. But it is everywhere. Same with
> foreign_require. It says it "brings in functions from another module".
> Doesn't perl have some sort of import/include/use syntax that will
> bring those into the current namespace without such machinations?

Yeah, those functions are admittedly odd - I created them years ago
to allow cross-module function calls, before I know about Perl modules
and such.

Basically, foreign_require sets up the context for another Webmin module
to be called from the current one. The idea is to use it like so :

foreign_require("useradmin", "user-lib.pl");
@users = useradmin::list_users();
etc..

> And while I'm here asking questions: Is there a webmin IRC channel? I
> googled and found a reference to #webmin on undernet but that seems to
> be for something completely unrelated and presumably non-english
> speaking.

Not as far as I know .. this list is the best place for discussions.
There are web forums, but the list gets more attention .. at least from
me :-)

 - Jamie

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
-
Forwarded by the Webmin mailing list at webadmin-list@lists.sourceforge.net
To remove yourself from this list, go to
http://lists.sourceforge.net/lists/listinfo/webadmin-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic