Solved

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

Posted on 2000-02-27
14
240 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
ID: 2563533
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
ID: 2563541
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
ID: 2563584
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:simran
ID: 2563603
Adjusted points to 300
0
 

Author Comment

by:simran
ID: 2563604
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
ID: 2564130
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
ID: 2566925
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
 
LVL 3

Expert Comment

by:jyu_88
ID: 2567149
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
ID: 2567193
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
ID: 2567216
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
ID: 2567866
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
ID: 2570990
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
ID: 2572028
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
ID: 2574744
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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