Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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):
Code from Foobar.pm:#!/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; }
Sample fbconfig.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;
package fbconfig; sub get { return { 'key' => 'value' } ; } 1;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Running a sub before a script exits?
by rattusillegitimus (Friar) on Aug 25, 2002 at 04:13 UTC | |
by Anonymous Monk on Aug 25, 2002 at 04:23 UTC | |
by rattusillegitimus (Friar) on Aug 25, 2002 at 04:29 UTC | |
Re: Running a sub before a script exits?
by dws (Chancellor) on Aug 25, 2002 at 04:09 UTC | |
by Anonymous Monk on Aug 25, 2002 at 04:49 UTC |
Back to
Seekers of Perl Wisdom