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

  2. George Mpouras <> writes:
    > 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).
     
    Rainer Weikusat, Mar 26, 2014
    #2
    1. Advertising

  3. Στις 26/3/2014 15:37, ο/η Rainer Weikusat έγÏαψε:
    > George Mpouras <> writes:
    >> 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).
    >




    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.
     
    George Mpouras, Mar 26, 2014
    #3
  4. George Mpouras <> writes:
    > Στις 26/3/2014 15:37, ο/η Rainer Weikusat έγÏαψε:
    >> George Mpouras <> writes:
    >>> 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).
    >>

    > 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.

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

    could be used to work around that. IMHO, that's a bug in CGI::Carp.
     
    Rainer Weikusat, Mar 26, 2014
    #4
  5. >
    > 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;
    > }
    > ----------
    >
    > could be used to work around that. IMHO, that's a bug in CGI::Carp.
    >



    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
    #5
  6. George Mpouras <> writes:
    >> 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;
    >> }
    >> ----------
    >>
    >> could be used to work around that. IMHO, that's a bug in CGI::Carp.
    >>

    >
    >
    > 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.
     
    Rainer Weikusat, Mar 27, 2014
    #6
  7. 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" };
     
    George Mpouras, Mar 27, 2014
    #7
  8. George Mpouras <> writes:
    > 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" };


    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
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Louis Erickson
    Replies:
    2
    Views:
    230
    James Willmore
    Sep 3, 2003
  2. Jo Oberman
    Replies:
    5
    Views:
    235
    Ilya Zakharevich
    Sep 14, 2003
  3. A. Farber
    Replies:
    3
    Views:
    286
    Ben Morrow
    Mar 3, 2004
  4. Gunnar Hjalmarsson

    CGI::Carp and "useless quotes"

    Gunnar Hjalmarsson, Apr 4, 2004, in forum: Perl Misc
    Replies:
    12
    Views:
    235
    Gunnar Hjalmarsson
    Apr 16, 2004
  5. Bennett Haselton
    Replies:
    2
    Views:
    540
    Gunnar Hjalmarsson
    Mar 2, 2008
Loading...

Share This Page