develooper Front page | perl.perl5.porters | Postings from September 2003

[perl #23810] Tied methods break when combined with eval() of failing compile-time code

Thread Next
Jason Rhinelander
September 15, 2003 08:16
[perl #23810] Tied methods break when combined with eval() of failing compile-time code
Message ID:
# New Ticket Created by  Jason Rhinelander 
# Please include the string:  [perl #23810]
# in the subject line of all future correspondence about this issue. 
# <URL: >

The following example demonstrates the problem quite well:

perl -e '
package Breaks;
use Tie::Hash;
@ISA = qw(Tie::StdHash);
sub FETCH { eval "BEGIN { some bad code }"; return 1 }

package main;
my %foo;
my $f = tie %foo, "Breaks";

print "Calling FETCH explicitly ...\n";

print "\tdone.\nCalling FETCH implicitly ...\n";

print "\tdone.\n"

The output from this is:

Calling FETCH explicitly ...
Calling FETCH implicitly ...
Can't return outside a subroutine at -e line 5.

In more complex situations, this causes Perl to segfault (at once point
I also got a "panic: pp_iter").  I've also tested it with other tied
methods and types - it seems that _any_ tied method (i.e. FETCH, STORE,
KEYS, etc.) for _any_ tied object type (I tested with tied hashes,
arrays, and scalars) that does this sort of eval causes the error.  It
doesn't have to be an explicit BEGIN block either - 'use Foo' in the
eval (where doesn't exist) causes this to occur as well - which
is how I originally discovered the bug.

Just for fun, I threw the implicit FETCH inside a subroutine (sub main)
and called it - the result was that the return value of the FETCH is
returned to the point where I called main, ignoring any remaining code
in sub main.

I figured out a rather nasty workaround, by wrapping the eval around
another eval, something like:

eval q{my $ret = eval "BEGIN { some bad code }"; die "$@\n" if $@; $ret};

I'm curious why the extra eval makes this work - admittedly it was
something of a shot in the dark to fixing my specific problem.  I'll
have to live with this solution, as the code I'm working on needs to
work in older Perl's, but it obviously isn't a very nice solution.

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

Configured by jagerman at Mon Aug 25 13:49:17 PDT 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 1) configuration:
    osname=linux, osvers=2.4.21-pfeifer-r1_pre4, archname=i686-linux-thread-multi
    uname='linux 2.4.21-pfeifer-r1_pre4 #1 wed jul 23 21:38:48 pdt 2003 i686 amd athlon(tm) authenticamd gnulinux '
    config_args='-des -Darchname=i686-linux-thread -Dcc=gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth=  -Doptimize=-march=athlon-xp -O3 -mmmx -msse -m3dnow -mfpmath=sse -pipe -fomit-frame-pointer -fforce-addr -funroll-loops -frerun-loop-opt -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman3ext=3pm -Dcf_by=Gentoo -Ud_csh -Dusethreads -Di_gdbm -Di_db -Di_ndbm'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-march=athlon-xp -O3 -mmmx -msse -m3dnow -mfpmath=sse -pipe -fomit-frame-pointer -fforce-addr -funroll-loops -frerun-loop-opt',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing'
    ccversion='', gccversion='3.3.1  (Gentoo Linux 3.3.1, propolice)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
    libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:

@INC for perl v5.8.1:

Environment for perl v5.8.1:
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

-- Jason Rhinelander
-- Gossamer Threads, Inc.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About