Hello fellow monks! I am using the below code in a script and cannot figure out how to automatically run the sub save_config before the script exits (to save any changes in the config hash to the config file). Any help is appreciated.
Code from main cgi file (foo.cgi): #!/usr/bin/perl
use strict;
eval {
require Foobar;
my $self = Foobar->new;
$SIG{__WARN__} = sub { die @_ };
$self->run;
};
if ($@) {
print "content-type: text/html\n\nError: $@";
exit;
}
Code from Foobar.pm:
package Foobar;
use strict;
sub new {
my $self = bless {}, shift;
$self->init;
}
sub init {
my $self = shift;
$self->{build} = 1;
$self->{version} = '1.0 alpha';
require CGI;
$CGI::HEADERS_ONCE = 1;
$self->{cgi} = CGI->new;
require fbconfig;
$self->{cfg} = fbconfig->get; # this is where I load the config fr
+om fbconfig.pm (saved by the sub save_config)
return $self;
}
sub run {
# A page handler would go here
}
sub save_config {
my $self = shift;
use Data::Dumper;
$Data::Dumper::Terse = 1;
open XCFG, '>fbconfig.pm';
print XCFG "package fbconfig;\n\n";
print XCFG "sub get {\n return ", Dumper($self->{cfg}), ";\n\n}
+\n\n1;";
close XCFG;
}
1;
Sample fbconfig.pm: package fbconfig;
sub get {
return {
'key' => 'value'
}
;
}
1;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|