PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm just re-factoring a project that uses Net::Server::PreFork and one of the ugly things in there is the use of hardcoded paths splattered all over the source. Yuck! I thought it would be a good idea to move them to the .conf file of Net::Server with some key, like so:

...
root_html               /srv/www/htdocs
root_cgi                /srv/www/cgi-bin
...

and then read them out by either $server->{server}->{root_html} or the respective $server->get_property('root_html') method call. Unfortunately, it seems as if Net::Server would not populate the config hash with key/value pairs he doesn't know, so everything I add to its config file is undefined/unknown.

I did some code browsing and it seems like Net::Server sets up some kind of template, which is cross-checked against the parameters given, and all unknown params are simply not touched. Although I do believe this might be a good idea for some purpose, it is quite frustrating for my purpose (centralize configuration).

The documentation wasn't very fruitful mentioning any of these "templates", so maybe some of you know how to do it. Perhaps I just oversaw something and contacting the author might be a blamage... ;-).

Thank you

Update: Got it working (after going round the houses). Basically I did some slightly more intelligent cargo cult programming after looking more closely at the inheritances of Net::Server. E.g. Net::Server::PreForkSimple, defines its own options method to add options for the package. I did exactly the same thing and came up with:
sub options { my $self = shift; my $ref = shift; my $prop = $self->{server}; my @newo = qw(root_html root_cgi); # YOUR NEW OPTIONS HERE! $self->SUPER::options($ref); for (@newo){ $prop->{$_} = undef if(!exists $prop->{$_}); $ref->{$_} = \$prop->{$_}; } }
Ok. Problem solved. I have to ask the author though why it has to be that complicated.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: Net::Server config-file extension
by ghenry (Vicar) on May 26, 2005 at 10:20 UTC
      The question - formulated so it can be correctly understood - is:

      How do I sneak in my configuration parameters to a Net::Server configuration file?

      I definitely know how to get own config files easily and I definitely do not want an ADDITIONAL config file for this project.

      Bye
       PetaMem
          All Perl:   MT, NLP, NLU

        Ah, Okay, apologies. I never noticed you were a Saint ;-)

        Anyway, according to Server.pm, these are the only arguments that are available:

        Key/value pairs used by the server are removed by the configuration process so that server layers on top of C<Net::Server> can pass and read their own parameters. Currently, Getopt::Long is not used. The following arguments are available in the default C<Net::Server> or C<Net::Server::Single> modules. (Other personalities may use additional parameters and may optionally not use parameters from the base class.) Key Value Default conf_file "filename" undef log_level 0-4 2 log_file (filename|Sys::Syslog) undef ## syslog parameters syslog_logsock (unix|inet) unix syslog_ident "identity" "net_server" syslog_logopt (cons|ndelay|nowait|pid) pid syslog_facility \w+ daemon port \d+ 20203 host "host" "*" proto (tcp|udp|unix) "tcp" listen \d+ SOMAXCONN reverse_lookups 1 undef allow /regex/ none deny /regex/ none ## daemonization parameters pid_file "filename" undef chroot "directory" undef user (uid|username) "nobody" group (gid|group) "nobody" background 1 undef setsid 1 undef no_close_by_child (1|undef) undef ## See Net::Server::Proto::(TCP|UDP|UNIX|etc) ## for more sample parameters.

        I would suggest either hacking Server.pm, or creating a new config file for the whole lot.

        Just my poor advise.

        Walking the road to enlightenment... I found a penguin and a camel on the way.....
        Fancy a yourname@perl.me.uk? Just ask!!!
Re: Net::Server config-file extension
by dragonchild (Archbishop) on May 26, 2005 at 13:56 UTC
    Because Net::Server::options() is a method call only, we're going to fake it out a bit. The author really needs to provide a clearer custom template method.
    my $template = Net::Server::options( {}, {} ); $template->{$_} = undef for @my_extra_param_names; my $server = Net::Server->new( $template );

    I think that will work, based on a 10-minute reading of the code.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"