Created
December 9, 2022 03:44
-
-
Save EvanCarroll/4f5faaf1997af4f83da1c3eb7c121967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v5.36; | |
use Sub::Util (); | |
use Inline::Python (); | |
use File::Slurp (); | |
use File::Spec (); | |
use Exporter 'import'; | |
our @EXPORT = qw(convert); | |
use experimental "signatures"; | |
use constant VERBOSE => 0; | |
BEGIN { | |
$ENV{INTERPRETER} //= 'PERL'; | |
if ( $ENV{INTERPRETER} ne 'PERL' && $ENV{INTERPRETER} ne 'PYTHON' ) { | |
die "INTERPRETER not set properly, must be PERL or PYTHON\n"; | |
} | |
} | |
sub convert :prototype($$) ($fnname, $block) { | |
my $pkg = caller(); | |
if ( $ENV{INTERPRETER} eq 'PERL' ) { | |
{ | |
no strict 'refs'; | |
*{"${pkg}::${fnname}"} = Sub::Util::set_subname( $fnname, $block ); | |
} | |
} | |
else { | |
my $file = join '::', | |
map ucfirst, | |
split '::', $pkg; | |
$file .= ".py"; | |
my $abspath = File::Spec->catfile( 'py', $file ); | |
if (VERBOSE) { | |
print "Will eval the contents of $file calling $abspath"; | |
} | |
my $pyfile = File::Slurp::read_file($abspath); | |
{ | |
no strict 'refs'; | |
my $sub = Sub::Util::set_subname( $fnname, sub { | |
Inline::Python::py_eval($pyfile); | |
Inline::Python::py_call_function('__main__', $fnname); | |
} ); | |
*{"${pkg}::${fnname}"} = $sub; | |
} | |
return; | |
} | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment