You cannot pass a Perl filehandle to Python. But you can try pass a file descriptor:
use feature qw(say);
use strict;
use warnings;
use Inline Python => <<END;
import os
def fun(fd):
with os.fdopen(int(fd), 'a') as file:
file.write("Hello from Python")
END
my $fn = 't.txt';
open (my $fh, ">", $fn) or die "Could not open file '$fn': $!";
say $fh "hello";
$fh->flush();
fun(fileno($fh));
close $fh
The content of t.txt
after running the script is:
$ cat t.txt
hello
Hello from Python
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…