develooper Front page | perl.perl5.porters | Postings from March 2000

[ID 20000310.019] Problems with nesting eval {}

Thread Next
From:
Jonathan Field
Date:
March 10, 2000 11:33
Subject:
[ID 20000310.019] Problems with nesting eval {}
Message ID:
Pine.BSF.4.10.10003101123230.6167-100000@binadopta.com

I've been experiencing some funny behavior when nesting eval {}.  Here's
some sample code to demonstrate:

  my $status = eval {

      eval { die };
      print "PERL";
      return; # removing this changes behavior

  };

  if ($@) {
      print " ERROR\n";
  } else {
      print " RULES\n";
  }

I would think this code would print out "PERL RULES\n".  However, it
prints out "PERL ERROR\n".  It makes it through the inner eval {} fine,
and yet the $@ variable is still set after exiting the outer eval {}.  
Now, the most interesting part, to me, is that it _will_ work as I
expected when the "return" is removed.

I guess what is happening here is that $@ is cleared when an eval hits its
closing brace, but not when the eval is exited via "return".  I can't
think of a reason why this would be desired behavior, so I'm wondering if
anyone here can explain it to me.  I've done my rounds in the newsgroups
and the perl-porters archives to no avail.

In my case, I wanted to be able to return different values from the eval
{} but certain libraries I use call eval {} internally and trip me up.  
There are other ways I can solve my problem, of course, but I am wondering
why this doesn't work.

Thanks for your time,
Jonathan Field

Same behavior on both of my machines:




Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=freebsd, osvers=4.0-current, archname=i386-freebsd
    uname='freebsd freefall.freebsd.org 4.0-current freebsd 4.0-current #0: $Date: 1999/05/05 19:42:40 $'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='undef', gccversion=egcs-2.91.66 19990314 (egcs-1.1.2 release)
    cppflags=''
    ccflags =''
    stdchar='char', d_stdstdio=undef, usevfork=true
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-Wl,-E'
    libpth=/usr/lib
    libs=-lm -lc -lcrypt
    libc=/usr/lib/libc.so, so=so, useshrplib=true, libperl=libperl.so.3
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fpic', lddlflags='-shared'


Characteristics of this binary (from libperl): 
  Built under freebsd
  Compiled at Sep 16 1999 21:51:34
  %ENV:
    PERL5LIB="/home/httpd/lib"
  @INC:
    /home/httpd/lib
    /usr/libdata/perl/5.00503/mach
    /usr/libdata/perl/5.00503
    /usr/local/lib/perl5/site_perl/5.005/i386-freebsd
    /usr/local/lib/perl5/site_perl/5.005
    .




Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=linux, osvers=2.2.12-20smp, archname=i686-linux
    uname='linux buffalo.idealab.com 2.2.12-20smp #1 smp mon sep 27 10:34:45 edt 1999 i686 unknown '
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    cppflags='-Dbool=char -DHAS_BOOL'
    ccflags ='-Dbool=char -DHAS_BOOL'
    stdchar='char', d_stdstdio=undef, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt
    libc=, 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'


Characteristics of this binary (from libperl): 
  Built under linux
  Compiled at Jan  4 2000 17:23:38
  %ENV:
    PERL5LIB="/isv/eb/lib"
  @INC:
    /isv/eb/lib
    /isv/eb/perl/lib/5.00503/i686-linux
    /isv/eb/perl/lib/5.00503
    /isv/eb/perl/lib/site_perl/5.005/i686-linux
    /isv/eb/perl/lib/site_perl/5.005
    .


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About