Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

CGI::Carp.pm taking over the name space for the main Carp.pm?

Hi All,  

In a very simple example to follow, the CGI::Carp.pm module seems to be
taking over the name space of the main Carp.pm

-------------------------------------------------------------------------
MyTest.pm
---------

package MyTest;

use strict;
use CGI::Carp qw(fatalsToBrowser croak carp);

1;

------------------------------------------------------------------------
test.pl
-------
#!/usr/local/bin/perl

use strict;
use Carp;
use MyTest;

&Carp::croak("Hello world\n");

------------------------------------------------------------------------





When test.pl is run, the message i get back is:










-------------------------------------------------------------------
Content-type: text/html

<H1>Software error:</H1>
<CODE>Hello world
</CODE>
<P>
For help, please send mail to this site's webmaster, giving this error
message
and the time and date of the error.
 
[Mon Feb 28 11:15:57 2000] test.pl: Hello world

-------------------------------------------------------------------

Which is what i should get when i use CGI::Carp::croak... but i'm not
using CGI::Carp::croak... i'm using Carp::croak.

Anyone know if CGI::Carp intentionally takes over the main Carp
namespace/subroutines... and is there anything i can do to stop it.


regards,

simran.

0
simran
Asked:
simran
  • 6
  • 4
  • 3
  • +1
1 Solution
 
jyu_88Commented:
according to 'perldoc CGI:Carp", it is intended that warn,die,croak.confess, and carp calls replaced those from Carp so that messages can get time-stamped in the HTTP server error log.

If you really want to use Carp::croak, then you need to leave out 'croak' from your 'use CGI qw//' in your Package, myTest..
0
 
simranAuthor Commented:
That is fair enough that they are getting replaced in the package... but then how do i then revert back to the normal carp... in the package i really want to use CGI::Carp::croak but in my
script i don't - hence i can't "not import" croak from CGI::Carp from my module, as i really do want it there.
0
 
jyu_88Commented:
you should be able to use 'no', the opposite of 'use', if you so desire.
check section on use in 'perldoc perlfunc' for more detail.

use MyTest;
no CGI::Carp qw(croak);
use Carp;



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
simranAuthor Commented:
Adjusted points to 300
0
 
simranAuthor Commented:
Hi jyu,

Unfortunately CGI::Carp does not have a "unimport" method which is required by the "no" command....

Try the example... with the
"no CGI::Carp qw(croak)" ... you'll
get a fatal error saying that the "unimport" method was not defined in CGI::Carp.

0
 
alien_life_formCommented:
Greetings.

die and warn are made available through realdie() and realwarn().
You could use CGI::carp() in your script, which should get you the original behaviour.

Also, you can play tricks with the set_message thingy (from the CGI::Carp docs).

And BTW, how does on go about to write an unimport function? The library modules have very few non-relevant examples.

Cheers,
       alf
0
 
simranAuthor Commented:
Hi Alf,

even after writing our own set_message handler... no matter what i do i can't get it to stop printing out the "Content-type: text/html" header... but at least i can get rid of the other html junk... :-)

Not sure about the unimport functions... i just came across them after the "no" thingy was suggested... found that CGI::Carp did not have it defined so left it at that!
0
 
jyu_88Commented:
Since you did use CGI::Carp qw(fatalsToBrowser),
"Fatal errors will now be echoed to the browser as well as to the log.  CGI::Carp arranges to send a minimal HTTP header to the browser so that even errors that occur in the early compile phase will be seen. " --CGI::Carp perldoc.

The "Content-type: text/html" is part of the minimal HTTP header mentioned above. If you run it as as CGI, you should not see it from the browser, since the browser will eat up "Content-type: text/html". It should only show up when you are running from command line. The set_message will just change the default message, i.e., the HTML body,  but not the HTTP header.

Solution:
1) not use "fatalsToBrowser",
or
2) not use Croak. Since you don't  seem to care about sending a minimal HTTP page reporting errors, then, you can simply do a print/printf to print any message you want, instead of use croak to generate generically-formatted error page.
0
 
simranAuthor Commented:
Hi Jyu,

You will notice that in the module i _do want_ fatalsToBrowser (i do want the header) but in the script i don't!

The problem is that the module is overriging the name space of the script... (which it should not be as i am saying use strict in the module... but it is...) ...

I can use "print whatever; exit"
statements... but this has now become an issue of design in programming/knowledge_based  for me...

There are lots of ways to get around the issue by programming differently.. but that is going around the problem not solving it...

i'm curious if the CGI::Carp module deliberately takes over all its parent scripts/modules... and if it does... is there a way to tell it not to!
0
 
jyu_88Commented:
time to write unimport method for CGI::Carp :-;

I am using version 5.00503 and latest CGI from CPAN.org. Now it even won't allow me to put "use Carp", "use CGI::Carp" at same script, oompling will fail due to 'redeclaration of those methods which CGI::Carp intentionally took over from Carp.
0
 
alien_life_formCommented:
Greetings.

I can do it (CGI::Carp and Carp, I mean). This is strange, but maybe due to CGI version numbers.

But, there is really no need to do it explicitely. CGI::Carp uses Carp, so if you use the former, you get the latter for free. (Assuming the author has not changed it, I mean)

Another note: depending on the needs of
the poster, a solution could be 'use'ing Carp, and doing a 'require' on CGI::Carp only when needed. Better yet, one could profit from autouse.pm   (which does execution time loading, but preserves some of the semantics of use).

Cheers,
   alf

0
 
omereCommented:
Actually, CGI::Carp does not 'overriding' the main namespace, it actually overrode perl's built-in die and warn functions, that's why you'll always get those minimal timestamps.

You can remove the part in CGI::Carp where that little hack is, and change accordingly.
0
 
alien_life_formCommented:
Greetings.

omere is right -(UTSL :-) ). In fact, the entire overriding bit consists in replacing warn and die.

Which also means that behavior can be switched by doing:

# Get back the default behavior

$savewarn=$main::SIG{__WARN__};
$main::SIG{__WARN__}=\&CORE::warn;
$savedie=$main::SIG{__DIE__};
$main::SIG{__DIE__}=\&CORE::die;

# Reinstate CGI::Carp;
$main::SIG{__WARN__}=$savewarn;
$main::SIG{__DIE__}=$savedie;


Instructive thread.

Cheers,
    alf
0
 
simranAuthor Commented:
Thankyou alf for your answer...

i now undef $SIG{__WARN_} and $SIG{__DIE__} before i say use Carp and everything works perfectly... :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now