Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[perl #24806] Stackable __WARN__ handlers

0 views
Skip to first unread message

perlbug-...@perl.org

unread,
Jan 4, 2004, 12:52:19 PM1/4/04
to bugs-bi...@netlabs.develooper.com
# New Ticket Created by e...@budvar.future-i.net
# Please include the string: [perl #24806]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=24806 >

This is a bug report for perl from e...@budvar.future-i.net,
generated with the help of perlbug 1.34 running under perl v5.8.2.


-----------------------------------------------------------------
[Please enter your report here]

When you call warn() from inside a __WARN__ handler, the default
warn() is called, your handler does not get called recursively. The
manual page documents it clearly, so this is correct behaviour. But
if $SIG{__WARN__} is localized, it would be useful for only the
innermost handler to get disabled and for the next to be called.

{
local $SIG{__WARN__} = sub { warn "A: $_[0]" };
{
local $SIG{__WARN__} = sub { warn "B: $_[0]" };
warn "hello\n";
}
}

This prints (correctly): 'B: hello'.

But I would like it to print: 'A: B: hello'.

If local() is judged the wrong way to implement these stacked handlers
then some other mechanism would be okay.


[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
category=core
severity=wishlist
---
Site configuration information for perl v5.8.2:

Configured by ed at Sat Nov 29 20:12:13 GMT 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 2) configuration:
Platform:
osname=linux, osvers=2.2.19-6.2.12.1rs, archname=i686-linux
uname='linux budvar.future-i.net 2.2.19-6.2.12.1rs #1 sat nov 3 02:42:38 cst 2001 i686 unknown '
config_args='-de -Dprefix=/home/ed'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
optimize='-O2',
cppflags='-fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm'
ccversion='', gccversion='egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)', 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='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lbind -lnsl -lgdbm -ldl -lm -lcrypt -lutil -lc
perllibs=-lbind -lnsl -ldl -lm -lcrypt -lutil -lc
libc=/lib/libc-2.2.4.so, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version='2.2.4'
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.2:
/home/ed/lib/perl5/5.8.2/i686-linux
/home/ed/lib/perl5/5.8.2
/home/ed/lib/perl5/site_perl/5.8.2/i686-linux
/home/ed/lib/perl5/site_perl/5.8.2
/home/ed/lib/perl5/site_perl/5.8.2/i686-linux
/home/ed/lib/perl5/site_perl/5.8.2/i386-linux
/home/ed/lib/perl5/site_perl/5.8.2/i686-linux
/home/ed/lib/perl5/site_perl/5.8.2
/home/ed/lib/perl5/site_perl
/home/ed/lib/perl5/5.8.2/i686-linux
/home/ed/lib/perl5/5.8.2
/home/ed/lib/perl5/site_perl/5.8.2/i686-linux
/home/ed/lib/perl5/site_perl/5.8.2
/home/ed/lib/perl5/site_perl
.

---
Environment for perl v5.8.2:
HOME=/home/ed
LANG=en_US
LANGUAGE (unset)
LD_LIBRARY_PATH=/home/ed/lib:/home/ed/wine/inst/lib:/usr/local/lib
LOGDIR (unset)
PATH=/home/ed/bin:/home/ed/bin/i686-pc-linux-gnu:/home/ed/wine/inst/bin:/home/ed/lib/ant/bin:/bin:/usr/bin:/usr/local/maple/bin:/usr/sbin:/sbin:/opt/kde/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/X11/bin:/usr/krb5/bin:/usr/athena/bin:/opt/IBMJava2-13/bin:/usr/local/jdk1.2.2/bin:/usr/share/smlnj/bin:/usr/games:/usr/kerberos/bin:/usr/kerberos/sbin:/usr/libexec/openssh:/usr/local/java/bin
PERL5LIB=/home/ed/lib/perl5/5.8.2:/home/ed/lib/perl5/site_perl/5.8.2:/home/ed/lib/perl5/site_perl/5.8.2/i686-linux:/home/ed/lib/perl5/site_perl/5.8.2/i386-linux:/home/ed/lib/perl5/site_perl
PERL_BADLANG (unset)
SHELL=/bin/bash

Rafael Garcia-Suarez

unread,
Jan 5, 2004, 3:37:58 AM1/5/04
to perl5-...@perl.org
e...@budvar.future-i.net (via RT) wrote:
> When you call warn() from inside a __WARN__ handler, the default
> warn() is called, your handler does not get called recursively. The
> manual page documents it clearly, so this is correct behaviour. But
> if $SIG{__WARN__} is localized, it would be useful for only the
> innermost handler to get disabled and for the next to be called.
>
> {
> local $SIG{__WARN__} = sub { warn "A: $_[0]" };
> {
> local $SIG{__WARN__} = sub { warn "B: $_[0]" };
> warn "hello\n";
> }
> }
>
> This prints (correctly): 'B: hello'.
>
> But I would like it to print: 'A: B: hello'.

There's no need for a new language feature for this; you can capture
the old signal handler in a closure :

{
local $SIG{__WARN__} = sub { warn "A: $_[0]" };
{
local $SIG{__WARN__} = sub {

my $oldsigwarn = $SIG{__WARN__};
sub {
my $warnmsg = "B: $_[0]";
$oldsigwarn ? $oldsigwarn->($warnmsg) : warn $warnmsg;
}
}->();
warn "hello\n";
}
}

Ed Avis

unread,
Jan 5, 2004, 8:57:32 AM1/5/04
to Rafael Garcia-Suarez via RT
On 5 Jan 2004, Rafael Garcia-Suarez via RT wrote:

>you can capture the old signal handler in a closure :
>
>{
> local $SIG{__WARN__} = sub { warn "A: $_[0]" };
> {
> local $SIG{__WARN__} = sub {
> my $oldsigwarn = $SIG{__WARN__};
> sub {
> my $warnmsg = "B: $_[0]";
> $oldsigwarn ? $oldsigwarn->($warnmsg) : warn $warnmsg;
> }
> }->();
> warn "hello\n";
> }
>}

This, then, should be the preferred idiom for setting a warning
handler if you have the feeling that someone else might want to trap
the warnings in turn (eg inside a library). Is it possible to make it
into a pragma somehow?

--
Ed Avis <e...@membled.com>

Yitzchak Scott-Thoennes

unread,
Jan 5, 2004, 9:12:39 AM1/5/04
to perl5-...@perl.org
On Sun, Jan 04, 2004 at 05:52:19PM -0000, "e...@budvar.future-i.net (via RT)" <perlbug-...@perl.org> wrote:
> # New Ticket Created by e...@budvar.future-i.net
> # Please include the string: [perl #24806]
> # in the subject line of all future correspondence about this issue.
> # <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=24806 >
>
>
>
> This is a bug report for perl from e...@budvar.future-i.net,
> generated with the help of perlbug 1.34 running under perl v5.8.2.
>
>
> -----------------------------------------------------------------
> [Please enter your report here]
>
> When you call warn() from inside a __WARN__ handler, the default
> warn() is called, your handler does not get called recursively. The
> manual page documents it clearly, so this is correct behaviour. But
> if $SIG{__WARN__} is localized, it would be useful for only the
> innermost handler to get disabled and for the next to be called.
>
> {
> local $SIG{__WARN__} = sub { warn "A: $_[0]" };
> {
> local $SIG{__WARN__} = sub { warn "B: $_[0]" };
> warn "hello\n";
> }
> }
>
> This prints (correctly): 'B: hello'.
>
> But I would like it to print: 'A: B: hello'.
>
> If local() is judged the wrong way to implement these stacked handlers
> then some other mechanism would be okay.

If progress ever gets made on better lexical pragma support, we could have
a warnings::handler that you could use like:

{
use warnings::handler sub { warn "A: $_[0]" };
{
use warnings::handler sub { warn "B: $_[0]" };
warn "hello\n";
}
}

But for now, one level is all you get.

Rafael Garcia-Suarez

unread,
Jan 5, 2004, 9:11:01 AM1/5/04
to perl5-...@perl.org
Yitzchak Scott-Thoennes wrote:
> If progress ever gets made on better lexical pragma support, we could have
> a warnings::handler that you could use like:
>
> {
> use warnings::handler sub { warn "A: $_[0]" };
> {
> use warnings::handler sub { warn "B: $_[0]" };
> warn "hello\n";
> }
> }

That's a bit different since pragmas are a compile-time thing and
the $SIG{__WARN__} handler is a run-time thing (not suitable to
catch compile-time warnings.)

0 new messages