in reply to It's friday, I can't think.
I did some extra stuff there to make it more clear. The $i gets incremented with each call so you can differentiate the print outs. My first version of this post had the select before I opened the filehandle. I don't recommend that as any print between the select and the open would bomb. So I fixed my example.{ my $i = 1; # Function static var... ooooh. sub Whatever { my $select = (defined $_[0]) ? $_[0] : 0; my $old_fh; local *FH; open FH, ">>$file" or die "Failed to open $file, $!"; if( $select ) { $old_fh = select FH }; print FH "$i This will go to $file\n"; print "$i This will go to $file if \$select($select) is true.\n"; if( $select ) { select $old_fh } close FH or die "Failed to close $file, $!"; $i++; } } Whatever(0); Whatever(1); # Now the second print goes to FH.
|
|---|