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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.