CGI::Carp and File::Temp

G

George Mpouras

When I use CGI::Carp and File::Temp my code is crashing without obvious
reason (Centos 6.5 , Perl 5.10) For a proof of bug you can run

perl -e "use CGI::Carp; use File::Temp"

The only workaround is to "zero" the die using the

local *CORE::GLOBAL::die = sub {};

at several places at /usr/share/perl5/File/Temp.pm
Any better idea ?
 
R

Rainer Weikusat

George Mpouras said:
When I use CGI::Carp and File::Temp my code is crashing without
obvious reason (Centos 6.5 , Perl 5.10) For a proof of bug you can run

perl -e "use CGI::Carp; use File::Temp"

Works for me (the example above) on both 5.10.0 and 5.10.1 (rather
ancient Ubuntu and moderately ancient Debian).
 
G

George Mpouras

Στις 26/3/2014 15:37, ο/η Rainer Weikusat έγÏαψε:
Works for me (the example above) on both 5.10.0 and 5.10.1 (rather
ancient Ubuntu and moderately ancient Debian).



I cpanm the latest modules and now the command works also for me, BUT
still not working at real script.
I have drill down the problem to the following piece of code that
reproduce the bad behavior


#!/usr/bin/perl
use strict;
use CGI::Carp;
BEGIN { CGI::Carp::set_die_handler( sub {print $_[0]} ) };
use File::Temp;



I get

our vendor has not defined Fcntl macro O_NOINHERIT, used at
/usr/share/perl5/File/Temp.pm line 114.
Your vendor has not defined Fcntl macro O_EXLOCK, used at
/usr/share/perl5/File/Temp.pm line 122.
Your vendor has not defined Fcntl macro O_TEMPORARY, used at
/usr/share/perl5/File/Temp.pm line 144.
 
R

Rainer Weikusat

George Mpouras said:
Στις 26/3/2014 15:37, ο/η Rainer Weikusat έγÏαψε:
Works for me (the example above) on both 5.10.0 and 5.10.1 (rather
ancient Ubuntu and moderately ancient Debian).
I cpanm the latest modules and now the command works also for me, BUT
still not working at real script.
I have drill down the problem to the following piece of code that
reproduce the bad behavior


#!/usr/bin/perl
use strict;
use CGI::Carp;
BEGIN { CGI::Carp::set_die_handler( sub {print $_[0]} ) };
use File::Temp;



I get

our vendor has not defined Fcntl macro O_NOINHERIT, used at
/usr/share/perl5/File/Temp.pm line 114.
Your vendor has not defined Fcntl macro O_EXLOCK, used at
/usr/share/perl5/File/Temp.pm line 122.
Your vendor has not defined Fcntl macro O_TEMPORARY, used at
/usr/share/perl5/File/Temp.pm line 144.

I've spent some time looking into this: The File::Temp code tries to
determine if the macros exist on the current platform by accessing them
from within eval-blocks with $SIG{__DIE__} locally bound to sub {} to

"Make sure that redefined die handlers do not cause problems
e.g. CGI::Carp"

The problem appears to be that CGI::Carp overrides CORE::GLOBAL::die
with a subroutine which calls the registered die handler via
$CGI::Carp::DIE_HANDLER if that is set instead of invoking 'the real
die' which would use $SIG{__DIE__} instead.
 
G

George Mpouras

The problem appears to be that CGI::Carp overrides CORE::GLOBAL::die
with a subroutine which calls the registered die handler via
$CGI::Carp::DIE_HANDLER if that is set instead of invoking 'the real
die' which would use $SIG{__DIE__} instead.

----------
BEGIN {
local $CGI::Carp::DIE_HANDLER;
require File::Temp;
}


if the BEGIN is the last line e.g..
...
use File::Temp;
BEGIN { CGI::Carp::set_die_handler( sub {print $_[0]} ) };

it works without error,even if the BEGIN is executed before anything
else. So may it have to with principle of indeterminacy also
 
R

Rainer Weikusat

George Mpouras said:
The problem appears to be that CGI::Carp overrides CORE::GLOBAL::die
with a subroutine which calls the registered die handler via
$CGI::Carp::DIE_HANDLER if that is set instead of invoking 'the real
die' which would use $SIG{__DIE__} instead.

----------
BEGIN {
local $CGI::Carp::DIE_HANDLER;
require File::Temp;
}


if the BEGIN is the last line e.g..
...
use File::Temp;
BEGIN { CGI::Carp::set_die_handler( sub {print $_[0]} ) };

it works without error,even if the BEGIN is executed before anything
else.

As can be determined by creating a file named b.pm with the following
content:

----
print "1\n";
----

and then executing

perl -e 'use b; BEGIN { print "2\n"; }'

perl executes the file b.pm prior to the BEGIN-block encountered after
the use statement. For File::Temp, this would mean it executes the
init-code testing the flags before $CGI::Carp::DIE_HANDLER gets set in
you example and hence, everything works as desired.
 
G

George Mpouras

b; BEGIN { print "2\n"; }'
perl executes the file b.pm prior to the BEGIN-block encountered after
the use statement. For File::Temp, this would mean it executes the
init-code testing the flags before $CGI::Carp::DIE_HANDLER gets set in
you example and hence, everything works as desired.

true, but weird

print "start\n";
BEGIN { print "begin\n" };
 
R

Rainer Weikusat

George Mpouras said:
b; BEGIN { print "2\n"; }'

true, but weird

print "start\n";
BEGIN { print "begin\n" };

It's actually documented behaviour: 'do EXPR' can be used to execute a
file, require ... uses do .... to read a library file which implies that
it is getting executed and use ... is by defintion identical to
BEGIN { require ...; ...->import(...); } which implies that the file
gets executed from within a BEGIN-block in this case. This BEGIN-block
will be executed as soon as it is encountered which means prior to all
BEGIN-blocks encountered after it (and after all BEGIN-blocks
encountered earlier).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top