[cgiapp] Graceful Error Handling in CGI::App (compile / init stages)

Peter Karman peter at peknet.com
Wed Jul 7 10:03:26 EDT 2010



Michael Peters wrote on 7/7/10 8:43 AM:

> 
> It's verbose, but the only thing I know of is:
> 
> BEGIN {
>    eval { require Data::Dumper; Data::Dumper::import() };
>    die "Can't find Data::Dumper module: $@" if $@;
> }
> 
> repeated for each one.

or

 my @modules = qw( Data::Dumper );
 for my $mod (@modules) {
     eval "use $mod";
     die "Can't find $mod module: $@" if $@;
 }

You can use 'use Foo' in a string eval and still get the defer-till-runtime help
of $@ and 'require':

# 'use' evaluated at compile-time
% perl -e 'print Data::Dump::dump(\{}),$/; use Data::Dump'
\{}

# 'require' evaluated at
% perl -e 'print Data::Dump::dump(\{}); require Data::Dump'
Undefined subroutine &Data::Dump::dump called at -e line 1.

# like 'require', deferred till run-time
% perl -e 'print Data::Dump::dump(\{}),$/; eval "use Data::Dump";'
Undefined subroutine &Data::Dump::dump called at -e line 1.

But Michael is correct about the security issues and the logs. Love your logs;
they will love you back.

-- 
Peter Karman  .  http://peknet.com/  .  peter at peknet.com


More information about the cgiapp mailing list