Giter Club home page Giter Club logo

perlfunc's Introduction

Perlfunc

Python module for calling perl functions from python. Just declare and decorate an empty python function and it will invoke your perl function and return the results.

Read more at: https://www.boriel.com/calling-perl-from-python.html

Sample usage

First create a small Perl script named mymodule.pl:

sub myfunc
{
    my ($a, $b) = @_;
    return $a + $b;
}

1;

Now let's create a Python program that will invoke the function myfunc() in the module mymodule.pl:

from perlfunc import perl5lib, perlfunc, perlreq

@perlfunc
@perlreq('mymodule.pl')
def myfunc(a, b):
    pass  # Empty body

print(myfunc(1, 3))  # Should print 4

Explanation:

  • First we import the decorators.
  • Then we create an empty (notice the pass keyword) function myfunc with the same parameters as the called function.
  • Finally we wrap the function using two decorators, @perlreq and @perlfunc

Now we can call our python function myfunc(a, b) and the Perl one will be called. :)

Decorators

  • perlfunc: mandatory. Use it to invoke a perl function with the same name as the python function.
  • perlreq(<perl script>): imports the required module where the perl function is defined.
  • perl5lib: optional, defines a list of path to be added to the PERL5LIB path env var.

perlfunc's People

Contributors

boriel avatar displaylink-kstrzemp avatar

Stargazers

Michael Maldonado avatar  avatar chenkx avatar Maria Karanasou avatar Daniel Wei avatar Corné Oosthuizen avatar Amadeu Moya avatar Dan Lawson avatar japlee avatar Matt avatar  avatar Stefan Zwijsen avatar  avatar  avatar Joel Longever avatar Steve Moss avatar

Watchers

Corné Oosthuizen avatar James Cloos avatar  avatar Joel Longever avatar Stefan Zwijsen avatar  avatar

perlfunc's Issues

I want to use this with a class but seems to not be working

for example, how do I use the constructor? suppose that you have a class called Person defined in Person.pm
in Perl:
$person = new Person;

in Python

@perlfunc
@perlreq('Person.pm')
def new("person"):    #person should be bless class but here is just a string
    pass  # Empty body

Passing dictionary does not succeed

Hi,

I'm trying to use this to call a perl function with a python dictionary as arguments.
But I can't get it to work: looks like it assigns the element key name to the value as well, instead of the actual value.
I played around with args/kwargs in calling the function, but that didn't solve it.

Can you give an example of how to call a function with a dictionary? Probably I'm calling it wrong.

In my case, I have a GenCallMonLog.pl with perl function named pythonLog.
I can call that from a test perl script like this (simplified to essential since not all code is public):

#!/usr/bin/perl
require "./GenCallMonLog.pl";
my %testhash;
%testhash = ('Message' => 'Starting TEST Monitor', 'EventID' => 'TST-2000');
pythonLog(%testhash);

This works fine (it writes entries to a log file).
So I try to do the same from a python script using perlfunc:

#!/usr/bin/env python
import os
import sys
from perlfunc import perl5lib, perlfunc, perlreq
logparams = {'Message':'Starting TEST Monitor', 'EventID':'TST-2000'}
@perlfunc
@perl5lib('.')
@perlreq('GenCallMonLog.pl')
def pythonLog(logdict):
  pass # empty body needed
pythonLog(logparams)

This is the result:

Traceback (most recent call last):
  File "./NetMon.py", line 21, in <module>
    pythonLog(logparams)
  File "perlfunc.py", line 173, in new
    result_stdout = sys_exec([PERL] + [tmpname], False, env=env)
  File "perlfunc.py", line 51, in sys_exec
    raise BaseException(str(a.communicate()[1]))
BaseException: EventID (EventID): Invalid value.

In Debug mode of perlfunc I see that the .pl in /tmp has this:
@rst = (pythonLog((EventID => 'EventID', Message => 'Message')));
So, the values are the same as the keys.

As said, probably I'm passing the args wrong? So an example from your end could be sufficient ;-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.