Solved

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

Posted on 2000-02-27
14
223 Views
Last Modified: 2010-03-05
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
Comment
Question by:simran
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 3

Expert Comment

by:jyu_88
Comment Utility
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
 

Author Comment

by:simran
Comment Utility
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
 
LVL 3

Expert Comment

by:jyu_88
Comment Utility
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
 

Author Comment

by:simran
Comment Utility
Adjusted points to 300
0
 

Author Comment

by:simran
Comment Utility
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
 
LVL 3

Expert Comment

by:alien_life_form
Comment Utility
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
 

Author Comment

by:simran
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:jyu_88
Comment Utility
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
 

Author Comment

by:simran
Comment Utility
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
 
LVL 3

Expert Comment

by:jyu_88
Comment Utility
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
 
LVL 3

Expert Comment

by:alien_life_form
Comment Utility
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
 
LVL 1

Expert Comment

by:omere
Comment Utility
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
 
LVL 3

Accepted Solution

by:
alien_life_form earned 300 total points
Comment Utility
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
 

Author Comment

by:simran
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now