According to PerlIO::scalar you can open a filehandle to a scalar,
open my $fh, ">", \$scalar or die;
How come $! gets set when you use this though?
use strict;
use warnings;
use autodie;
use Test::More tests => 3;
is ( $!, '', '$! is not set' );
my $output;
open ( my $fh, '>', \$output );
print $fh "Hello, World!";
is ( $output, "Hello, World!", "Got the right output" );
is ( $!, '', '$! is not set' );
This is really weird: fork returns the pid so you determine failure by looking at $!. I thought $! only got set on failure though. The docs make it clear that this variable doesn't get cleared on success,
Many system or library calls set
errnoif they fail, to indicate the cause of failure. They usually do not seterrnoto zero if they succeed. This meanserrno, hence$!, is meaningful only immediately after a failure -perldoc perlvar
So generally you have to clear $! after a failure. But should we always clear it after a success too? What things set $! on success?