develooper Front page | perl.perl5.porters | Postings from April 2015

[perl #124415] localizing $? causes exit() to ignore the argument passed to it

Ævar Arnfjörð Bjarmason
April 28, 2015 16:25
[perl #124415] localizing $? causes exit() to ignore the argument passed to it
Message ID:
# New Ticket Created by  Ævar Arnfjörð Bjarmason 
# Please include the string:  [perl #124415]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.40 running under perl 5.21.12.

[Please describe your issue here]

This is a bug report is against a documented feature of perl, I know
it's mostly supposed to work like this, but but regardless of that I
think there's specific edge case that's buggy.

The "feature" is that you can write code that does this:

    $ ~/perl5/installed/bin/perl5.21.12 -wE 'exit 1; END { $? = 2 }'; echo $?

I.e. to quote "perldoc -f exit":

    [...] END routines and destructors can change the exit status by
    modifying $?.> for details.

Okey, so be that is it may, but I think this is a bug:

    $ ~/perl5/installed/bin/perl5.21.12 -wE '{ local $?; exit 1 }'; echo $?

The documentation says that if you change the value of $? in an END
routine we'll use the new value, but here we're not in an END routine
(just tearing down a scope), and nothing explicitly modified $?.

Admittedly we're splitting hairs here from an implementation level
because end of scope teardown here is a bit like END routines, and
"local" modifies $?, but this is very surprising from a language
user's perspective.

The reason I ran into this was because I was writing code that could
be simplified as:

    $ ~/perl5/installed/bin/perl5.21.12 -wE '
            local $?;
            system "false";
            my $ret = $? >> 8;
            say "Exiting with status <$ret>";
            exit $ret;
        END {
            say "Our \$? in END is <$?>";
    '; echo $?
    Exiting with status <1>
    Our $? in END is <0>

I.e. I'm calling an external program and localizing $? first, if that
external program fails I'd like to exit, but because I localized $?
the exit status is clobbered.

The "exit" documentation suggests using POSIX::_exit() if this is an
issue, but that's going to cause more issues for most programs,
i.e. DESTROY won't run anywhere etc.

[Please do not change anything below this line]
Site configuration information for perl 5.21.12:

Configured by avar at Tue Apr 28 15:40:55 UTC 2015.

Summary of my perl5 (revision 5 version 21 subversion 12) configuration:
  Commit id: b77ebf74c6d61f12ef717fd4bd6f765479481ca1
    osname=linux, osvers=3.14-2-amd64, archname=x86_64-linux-thread-multi
    uname='linux 3.14-2-amd64 #1 smp debian 3.14.13-2 (2014-07-24) x86_64 gnulinux '
    config_args='-DDEBUGGING=both -Doptimize=-ggdb3 -Dusethreads -Dprefix=/home/avar/perl5/installed -Dusedevel -des'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=define, usemultiplicity=define
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DDEBUGGING -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion='', gccversion='4.2.1 Compatible Debian Clang 3.5.0 (tags/RELEASE_350/final)', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
    libs=-lpthread -lnsl -ldb -ldl -lm -lcrypt -lutil -lc
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -ggdb3 -L/usr/local/lib -fstack-protector-strong'

@INC for perl 5.21.12:

Environment for perl 5.21.12:
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)
    SHELL=/bin/bash Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About