Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Perl, how can I disable CGI::Carp that was loaded in a required script?

Tags:

cgi

perl

I've got a small Perl program that I want to run on the command line. I need to require another Perl script (not a module) that someone else has written. That, in turn, requires some other scripts. (I cannot do anything about the way this works).

Now, it is really annoying that one of these scripts has use CGI; and use CGI::Carp qw(fatalsToBrowser) in it. I do not want that. Having 15 lines of 500 Internal Server Error page on my console every time something breaks is really getting on my nerves. I've tried

require 'otherscript.pl';
no CGI;
no CGI::Carp;

and

no CGI;
no CGI::Carp;
require 'otherscript.pl';

to unload it, like the use doc describes, but it doesn't work.

Can I somehow manipulate the symbol table or do some other magic to get rid of it?

like image 957
simbabque Avatar asked Jun 28 '12 10:06

simbabque


3 Answers

There is no unimport routine in the CGI::Carp package, so no has no effect. Undo the relevant part of the import routine manually.

Lexical scope (see caveat):

local $main::SIG{__DIE__} = \&CGI::Carp::realdie;

Global scope:

CGI::Carp::set_die_handler(\&CGI::Carp::realdie);
like image 196
daxim Avatar answered Sep 20 '22 20:09

daxim


By setting $CGI::Carp::TO_BROWSER variable to 0 you can suppress printing the HTML version of die messages.

like image 39
Denis Ibaev Avatar answered Sep 19 '22 20:09

Denis Ibaev


Where does the code that uses fatalsToBrowser come from? Whilst fatalsToBrowser is a really useful development tool, it should only be used in development. In a production environment it can be argued that fatalsToBrowser is a security risk as it potentially gives too much information to someone trying to crack your server.

So, by all means use the tricks that other people have mentioned to turn it off. But you should also do whatever you can to ensure that the offending code is changed so that it doesn't use fatalsToBrowser in production.

like image 44
Dave Cross Avatar answered Sep 18 '22 20:09

Dave Cross