[tpm] 'use CGI::Carp' being calling in an 'if' block when condition not met

Indy Singh indy at indigostar.com
Thu Jan 8 14:29:14 PST 2009


Use statements are efectively evaluated before other run-time 
statements.  From 'perldoc -f use'
use Module LIST;
It is exactly equivalent to
BEGIN { require Module; Module->import( LIST ); }

Wrap the 'use' with an 'eval' to avoid this, or use the quivalent 
require statements.

Indy Singh
IndigoSTAR Software -- www.indigostar.com


----- Original Message ----- 
From: "Madison Kelly" <linux at alteeve.com>
To: "Toronto Perl Mongers" <tpm at to.pm.org>
Sent: Thursday, January 08, 2009 5:21 PM
Subject: [tpm] 'use CGI::Carp' being calling in an 'if' block when 
condition not met


> Hi all,
>
>   I've got a program that can either be printed to a browser or shell. 
> To this end, I have a simple function that determines how the program 
> is running. When it detects a browser, it loads 'CGI::Carp 
> qw(fatalsToBrowser);'.
>
>   The problem is that this module is being loaded regardless of 
> whether The containing 'if' block's condition is met.
>
>   Here's the function
>
> ------------------------------------------------------------------------
> sub detect_calling_method
> {
> my ($conf)=@_;
>
> # Now determine if we are being called via the command line or a
> # browser and update the 'conf' value.
> if ($ENV{HTTP_USER_AGENT})
> {
> # HTML, load  CGI.
> $conf->{'system'}{display}{mode}="html";
> use CGI::Carp qw(fatalsToBrowser);
> print "Content-type: text/html; charset=utf-8\n\n";
> }
> elsif ($ENV{SHELL})
> {
> $conf->{'system'}{display}{mode}="text";
> STDOUT->autoflush(1);
> }
> print "$THIS_FILE ".__LINE__.": system::display::mode: 
> [$conf->{'system'}{display}{mode}]\n";
> # Not detecting a mode is no longer fatal, it simply remains the
> # default value set in the config file.
>
> return(1);
> }
> ------------------------------------------------------------------------
>
>   So even if '$ENV{HTTP_USER_AGENT}' is empty and 
> '$conf->{'system'}{display}{mode}' is set to 'text' (showing that it 
> was the condition matched), calling 'die' shows an HTML'ized error 
> like:
>
> ------------------------------------------------------------------------
> common.lib 238: system::display::mode: [text]
> <h1>Software error:</h1>
> <pre>Looping in common.lib at line 665 on string: [Opening log file 
> on: [#!var!0!#] at: [#!var!1!#] from file: [#!var!2!#].]. Exiting.
> </pre>
> <p>
> For help, please send mail to this site's webmaster, giving this error 
> message
> and the time and date of the error.
>
> </p>
> ------------------------------------------------------------------------
>
>   Any tips?
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm 



More information about the toronto-pm mailing list