in reply to Re: perl -COE and, for example, CGI::Application
in thread perl -COE and, for example, CGI::Application

So, you would advocate for not using any switches on the perl shebang, right?

Putting binmode STDOUT, ':utf8'; in cgiapp_prerun and doing binmode STDOUT,':raw'; just on runmodes where I output binary data is a very elegant solution, I think, as it resets STDOUT to :utf8 on every new cycle. -Right?

In regards to "automatic decode_utf8($v) on form input with detection of uploads" I am conservative. Had a bit of trouble with it, thus falling back to doing it on a per runmode basis. Are your experiences more consistent (would this be production code)?

I can't share your last comment: Might be that I do things less efficient but I have my fastcgi loop far enough around everything, so I can use the normal runmode switching facility and all mentioned plugins work under CGI::Fast.

My (updated without -COE switches) current app.fcgi:
#!/usr/bin/perl use CGI::Fast(); use App; use utf8; # fastcgi usage as told in: http://cgiapp.erlbaum.net/index.cgi?FastCG +I while( my $q = new CGI::Fast ){ $q->header(-charset => 'utf-8'); # as I now set :utf8 in cgiap +p_prerun, might be more beautiful to also move this there.. my $app = new App( QUERY => $q ); $app->run(); }
and in App.pm:
use base 'CGI::Application'; ... sub cgiapp_init { ## under FCGI CGI::Application permanently re-inits itself. No +t what you would expect from cgiapp_init and setup, anyway... ## so we do a hack to get this here executed only once. setup( +) can be executet over and over if(!$self->{remember_app_init_done}){ # do one time setup stuff here } ## declare init done $self->{remember_app_init_done}=1; } sub setup { ... ## prepare runmodes $self->start_mode('default'); # $self->error_mode('error'); $self->run_modes(); ... } ...
Actually, the above code isn't exactly my code, as I think I found a bug in the logic while writing this (I've already corrected the above based on this).

What I haven't changed in the above is, now that I think about it, the setup() stuff should also run just once, shouldn't it...?

Replies are listed 'Best First'.
Re^3: perl -COE and, for example, CGI::Application
by karavelov (Monk) on Sep 23, 2008 at 15:18 UTC

    I think that it is better to do this with "binmode" but not with switches. One reason for this is that using "binmode" you could run the same cgi-app module under mod-perl.

    I use the decode_utf8() in prerun stage approach in production code. Initially I had some troubles caused by interaction of utf8 and MD5, but I escape this case. Pay attention that "auth_login/auth_passwword" is what I use for authentication, the default in CGI::Application::Plugin::Authentication is different.

    Now I see why you does not have problems with CGI::Fast - because you create new object for every request. My initial experience was with CGI::Application::FastCGI where the run handler is :

    sub run { my $self = shift; my $request = FCGI::Request(); $self->fastcgi($request); while ($request->Accept >= 0) { $self->reset_query; $self->SUPER::run; } }

    In your approach you have "new,run,new,run,new,run...". In my case I have "new,run,run,run...". Actually the difference in performance is not quite big but usually I have some heavy initialization in init stage that is run only once (on new object creation), so I see some benefit in using this approach.