CGI::Carp and File::Temp

Discussion in 'Perl Misc' started by George Mpouras, Mar 26, 2014.

  1. 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/
    Any better idea ?
    George Mpouras, Mar 26, 2014
    1. Advertisements

  2. Works for me (the example above) on both 5.10.0 and 5.10.1 (rather
    ancient Ubuntu and moderately ancient Debian).
    Rainer Weikusat, Mar 26, 2014
    1. Advertisements

  3. Στις 26/3/2014 15:37, ο/η Rainer Weikusat έγÏαψε:

    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

    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/ line 114.
    Your vendor has not defined Fcntl macro O_EXLOCK, used at
    /usr/share/perl5/File/ line 122.
    Your vendor has not defined Fcntl macro O_TEMPORARY, used at
    /usr/share/perl5/File/ line 144.
    George Mpouras, Mar 26, 2014
  4. 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.
    Rainer Weikusat, Mar 26, 2014

  5. 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
    George Mpouras, Mar 26, 2014
  6. As can be determined by creating a file named with the following

    print "1\n";

    and then executing

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

    perl executes the file 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.
    Rainer Weikusat, Mar 27, 2014
  7. b; BEGIN { print "2\n"; }'
    true, but weird

    print "start\n";
    BEGIN { print "begin\n" };
    George Mpouras, Mar 27, 2014
  8. 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).
    Rainer Weikusat, Mar 27, 2014
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.