beginners:

I'm trying to catch errors when I call print() using a variable containing a string with a bad IO handle (and/or glob?) name. eval() doesn't seem to work:

2011-01-16 14:19:56 dpchrist@p43400e ~/sandbox
$ 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 @_;
    };
    print "trapped error $@" if $@;
}

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

2011-01-16 14:19:59 dpchrist@p43400e ~/sandbox
$ perl -c trap-print-errors.pl
trap-print-errors.pl syntax OK

2011-01-16 14:20:05 dpchrist@p43400e ~/sandbox
$ perl trap-print-errors.pl
hello, world!
print() on unopened filehandle NOSUCH at trap-print-errors.pl line 12.


Any suggestions?


TIA,

David

--
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