----- Original Message ----- From: "David Christensen" <dpchr...@holgerdanske.com>

$ cat trap-print-errors.pl
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;

sub myprint($@)
{
    my $s = shift;
    eval {
###  circumvent Can't use string ("*STDOUT") as a symbol ref...
no strict 'refs';
print $s @_;

Replace the above line with:
print $s @_ or die "Can't print: $!";

    };
    print "trapped error $@" if $@;
}

myprint '*STDOUT', "hello, world!\n";
myprint '*NOSUCH', "goodbye, cruel world!";

[snip]

print() on unopened filehandle NOSUCH at trap-print-errors.pl line 12.

That is not a fatal error - it does not cause the script to die. Hence it's not something that can be trapped in an eval{} block.

Cheers

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to