[comp.sources.d] Why can't I print the SIG array in perl?

hans@nlgvax.UUCP (Hans Zuidam) (11/20/88)

I think I've found a 'bug' in the perl SIG array. The following
script doesn't print anything. Substituting SIG by ENV nicely
shows my environment.

    eval "exec /usr4/hans/bin.${ARCH}/perl.${ARCH} -Sw $0 $*"
        if $run_shell;

    @Keys   = keys(SIG);
    @Values = values(SIG);

    while ($#Keys >= 0)
    {
        print pop(Keys), " = ", pop(Values), "\n";
    }

Is this indeed a bug? Is there a fix?

B.T.W: The eval at the top is because the #! construct does not use
your PATH. We have multiple architectures (Sun3, Sun4, Sun386i
and VAX), by setting ARCH to `arch`, you get the right perl :-).

Thanks in advance,

						Hans
-- 
Hans Zuidam                                    E-Mail: hans@pcg.philips.nl
Philips Telecommunications and Data Systems,   Tel: +31 40 892288
Project Centre Geldrop, Building XR
Willem Alexanderlaan 7B, 5664 AN Geldrop       The Netherlands

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/22/88)

In article <161@nlgvax.UUCP> hans@pcg.philips.nl (Hans Zuidam) writes:
: I think I've found a 'bug' in the perl SIG array. The following
: script doesn't print anything. Substituting SIG by ENV nicely
: shows my environment.
: 
:     eval "exec /usr4/hans/bin.${ARCH}/perl.${ARCH} -Sw $0 $*"
:         if $run_shell;
: 
:     @Keys   = keys(SIG);
:     @Values = values(SIG);
: 
:     while ($#Keys >= 0)
:     {
:         print pop(Keys), " = ", pop(Values), "\n";
:     }
: 
: Is this indeed a bug? Is there a fix?

Hmm.  I don't think it's quite a bug.  The SIG array works like regular
associative arrays in that there's nothing there unless you put it into
it yourself.  Any signal that doesn't have a value just does the default.

I could set it up to have values for all the signals, if
	1) I knew what all the signals were (they vary considerably) and
	2) I was willing to endure the overhead of loading them all into
		the SIG array.

Since I don't think either of those are going to be true soon, I've installed
the following sentence in the perl manual:

	The SIG array only contains values for the signals actually
	set within the perl script.

(If you inserted $SIG{'TERM'} = 'IGNORE'; into your script above, you would
note that it does print out TERM = IGNORE.)

The ENV array has to be different since there's no such thing as a
default value for environment variables.

If you want all the SIGs very very much, you can say something like this:

if (open(SIG,"/usr/include/sys/signal.h")) {
    while (<SIG>) {
	eval "\$SIG{'$1'} = 'DEFAULT';" if /^#define\s+SIG(\w+)/;
    }
    close SIG;
}

: B.T.W: The eval at the top is because the #! construct does not use
: your PATH. We have multiple architectures (Sun3, Sun4, Sun386i
: and VAX), by setting ARCH to `arch`, you get the right perl :-).

Cute.  I've always wished #! could use the PATH.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov