Skip to content

Instantly share code, notes, and snippets.

@hesco
Last active December 14, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hesco/5020130 to your computer and use it in GitHub Desktop.
Save hesco/5020130 to your computer and use it in GitHub Desktop.
Brand new to both Mojolicious and to HTML::FormHandler, only been toying with these tools for less than an hour a day over the past week or so, plus perhaps a day of reading perldoc and tutorials and watching videos. Here is what I've got and what I'm seeing. My question is this: What happened to my \@config_files on the way to instantiating my …
logs . . .
[Sun Feb 24 12:18:33 2013] [debug] GET /password_reset (Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.19 (KHTML, like Gecko) Chrome/25.0.1323.1 Safari/537.19).
[Sun Feb 24 12:18:33 2013] [debug] Routing to a callback.
The password_reset scripts says: $VAR1 = {
'config_files' => [
't/conf/myapp_api.ini'
]
};
::ResetPassword->my_app says config_files is: $VAR1 = [
't/conf/myapp_api.ini'
];
[Sun Feb 24 12:18:33 2013] [error] MyApp's constructor invoked with no Confirguration File.
$VAR1 = bless( {}, 'MyApp::Base' );
$VAR2 = [
'Moose::Meta::Attribute',
'/usr/local/lib/perl/5.10.1/Moose/Meta/Attribute.pm',
545
];
[Sun Feb 24 12:18:33 2013] [debug] Template "exception.development.html.ep" not found.
[Sun Feb 24 12:18:33 2013] [debug] Template "exception.html.ep" not found.
[Sun Feb 24 12:18:33 2013] [debug] Rendering inline template.
[Sun Feb 24 12:18:33 2013] [debug] Rendering inline template.
[Sun Feb 24 12:18:33 2013] [debug] 500 Internal Server Error (0.058962s, 16.960/s).
-------------------------------------------------------------
#!/usr/bin/env perl
use Mojolicious::Lite;
use Data::Dumper;
use lib qw( lib );
use MyApp::WWW::Admin::ControlPanel::ResetPassword;
my $ini = "t/conf/myapp_api.ini";
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
any [qw( get post )] => '/password_reset' => sub {
my $self = shift;
my $args = { config_files => [ $ini ] };
warn Dumper( $args );
my $form = MyApp::WWW::Admin::ControlPanel::ResetPassword->new( $args );
$form->process( params => $self->req->params->to_hash );
if( $form->validated ){
my %site = %{$form->my_app->get_site_from_url(
$form->field('site_url')->value(), 1 )};
my $password_reset_hash =
$form->my_app->reset_user_password(
$form->my_app->get_site( $site{'site_id'} ) );
$self->render( text => '<p>Obtaining Password Reset Hash for: <br />'
. $form->field('site_url')->value()
. ' is: <br />'
. $password_reset_hash . '</p>');
} else {
$self->render( text => $form->render({ id => 'reset_pw_form' }) );
}
};
app->secret('very_secret');
app->start;
__DATA__
----------------------------------------
package MyApp::WWW::Admin::ControlPanel::ResetPassword;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Data::Dumper;
use lib qw( lib );
use MyApp::ThisApplication;
has 'config_files' => (
is => 'rw',
isa => 'ArrayRef[Str]',
required => 1,
);
has 'my_app' => (
is => 'ro',
isa => 'MyApp::ThisApplication',
default => sub {
my $self = shift;
warn '::ResetPassword->my_app says config_files is:'
. Dumper( $self->config_files ) . "\n";
return MyApp::ThisApplication->new({
'config_files' => $self->config_files });
},
);
---------------------------------------
package MyApp::ThisApplication;
use Moose;
use lib qw( lib );
extends 'MyApp::Base';
has 'config_files' => (
is => 'ro',
isa => 'ArrayRef[Str]',
);
-----------------------------------
package MyApp::Base;
use Moose;
use Data::Dumper;
use Config::Simple::Extended;
use lib qw( lib );
use MyApp::DB;
has 'cfg' => (
is => 'rw',
isa => 'Config::Simple',
builder => '_build_cfg',
);
has 'config_files' => (
is => 'rw',
isa => 'ArrayRef[Str]',
);
# required => 1,
has 'configuration' => (
is => 'rw',
isa => 'Config::Simple',
);
sub _build_cfg {
my $self = shift;
if(defined($self->configuration)){
return $self->configuration;
} elsif(defined($self->config_files)){
print STDERR "We have a config_files key which includes: " .
Dumper( $self->config_files )
if( $self->debug );
my $cfg;
undef($cfg);
foreach my $file (@{$self->config_files}){
print "Now applying $file to configuration hash \n"
if( $self->debug );
$cfg = Config::Simple::Extended->inherit({
debug => $self->debug,
base_config => $cfg,
filename => $file });
}
print Dumper( $cfg->get_block("paths") )
if( $self->debug );
return $cfg;
} else {
my @caller = caller();
die "MyApp::Base's constructor invoked with no Confirguration File.\n"
. Dumper( $self->config_files, \@caller );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment