useruser has asked for the wisdom of the Perl Monks concerning the following question:

It seems as if Fatal.pm behaves oddly when the tag :void is specified. Fatal seems to affect $!. Consider the following script temp.pl:
use strict; use warnings; use Fatal qw(:void open); open my $f, '<no_such_file' or die $!;
This example behaves differently with and without the "use Fatal;" line. With this line, the output is
Died at temp.pl line 3.
Without the line, the output is
No such file or directory at temp.pl line 3.
So $! is cleared. Is that how it's supposed to work? I expected Fatal to have no effect in this case, since open is being used in scalar context, not void.

I'm running perl v5.6.1, ActiveState build 633. Thanks for your help.

Replies are listed 'Best First'.
Re: use Fatal ':void' vs. $!
by rblasch (Monk) on Jun 13, 2006 at 14:38 UTC

    Consider this example.

    use strict; use warnings; use Fatal qw(:void open); # set $! to some random number; we just want to see # if it is modified $! = 42; print "$!\n"; open my $f, '<no_such_file' or die "Can't open no_such_file: $!";

    The output is as follows.

    Illegal byte sequence Can't open no_such_file: Illegal byte sequence at t.pl line 10.

    $! doesn't seem to get modified. Taking a look at Fatal.pm, one sees the following at about line 121:

    local(\$", \$!) = (', ', 0);

    $" and $! are localized in the replacement subroutine, open in this example. Removing above line, and running the example again yields the following.

    Illegal byte sequence Can't open no_such_file: No such file or directory at t.pl line 10.

    This seems to be the desired outcome.

    I don't use Fatal, so I can't tell if the localized $! is a bug or a feature.

      I think you have it. It looks to me like,

      $code = <<EOS; sub$real_proto { local(\$", \$!) = (', ', defined(wantarray)? \$! : 0); EOS
      would fix it. Untested.

      After Compline,
      Zaxo

A reply falls below the community's threshold of quality. You may see it by logging in.