Pretty much every perl program written uses this idiom:
{
open(my $fh, '>>', $filename) || die "you lose";
print $fh $blah;
}
However, I don't want to die, I want to just skip the print. So I write:
{
print "you lose\n" unless (open(my $fh, '>>', $filename) and print $fh $blah);
}
and get "Can't use an undefined value as a symbol reference at ./o.pl line 5" for my trouble.
Removing the my (bad form) eliminates this error, as does:
{
my $fh;
print "you lose\n" unless (open($fh, '>>', $filename) and print $fh $blah);
}
but why?
Why, in the broken code, doesn't $fh exist from open(my $fh... to the close of the block (the })?