Skip to content

Instantly share code, notes, and snippets.

@EvanCarroll
Created December 9, 2022 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvanCarroll/4f5faaf1997af4f83da1c3eb7c121967 to your computer and use it in GitHub Desktop.
Save EvanCarroll/4f5faaf1997af4f83da1c3eb7c121967 to your computer and use it in GitHub Desktop.
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