Generating functions with Macros in Perl

Discussion in 'Perl Misc' started by Andreas Lundgren, Aug 26, 2011.

  1. Hi!

    I'm building an API for a HW, and this API has a lot of wrapper
    functions that wraps Win32OLE-calls. All these wrapper functions are
    quite alike. In stead of writing a lot of duplicated Win32OLE-code, I
    would like to generate these functions using Marcos.

    Basically I want to define one macro that expands to several
    functions.

    _____Simple Example_____
    Template: define ADDX(Xname, Xval) <- Here i need help, how to write
    this kind of macro?!?

    The intension is that when I put this in my script
    ADDX("Two",2)
    ADDX("Four",4)

    It should be expanded and make these two functions available:

    sub AddTwo
    {
    $ret = _@[0] + 2
    }

    sub AddFour
    {
    $ret = _@[0] + 4
    }



    _____In the real case_____

    Macro definitions should be something like this:
    SET_VALUE(f_name, method, no_of_params)

    Later I will include the macro with for example this parameters:

    SET_VALUE("InitDriver", "ext_driver_init", 4)

    And it should expand to something like this:

    sub InitDriver # Here the "InitDriver" is from Marco Expansion
    {
    ( 4 == scalar( @_ ) ) or die( "Incorrect Number Of Arguments.\n" );
    # Here the "4" is from Marco Expansion
    ole_arg_list = join(';',@_);

    my $ewaApp = Win32::OLE->new('ewa.Application');
    # Some code for the Win32OLE

    $ewaApp.call(['MethodNameIn','Params'],
    ['ext_driver_init',ole_arg_list]);
    # Here the "ext_driver_init" is from Marco Expansion
    }

    Is it possible to write macros that create functions like this?

    BR,
    Andreas - Sweden
     
    Andreas Lundgren, Aug 26, 2011
    #1
    1. Advertisements

  2. Am 26.08.2011 10:06, schrieb Andreas Lundgren:
    > Hi!
    >
    > I'm building an API for a HW, and this API has a lot of wrapper
    > functions that wraps Win32OLE-calls. All these wrapper functions are
    > quite alike. In stead of writing a lot of duplicated Win32OLE-code, I
    > would like to generate these functions using Marcos.
    >
    > Basically I want to define one macro that expands to several
    > functions.
    >
    > _____Simple Example_____
    > Template: define ADDX(Xname, Xval) <- Here i need help, how to write
    > this kind of macro?!?
    >
    > The intension is that when I put this in my script
    > ADDX("Two",2)
    > ADDX("Four",4)
    >
    > It should be expanded and make these two functions available:
    >
    > sub AddTwo
    > {
    > $ret = _@[0] + 2
    > }
    >
    > sub AddFour
    > {
    > $ret = _@[0] + 4
    > }


    I think you could probably use currying.

    For example:

    sub makeAdder {
    my $value = shift;
    return sub {
    return $value + $_[0]
    }
    }

    my $addTwo = makeAdder(2);
    my $addFour = makeAdder(4);

    print $addTwo->(5), "\n"; #prints 7
    print $addFour->(5), "\n"; #prints 9


    - Wolf
     
    Wolf Behrenhoff, Aug 26, 2011
    #2
    1. Advertisements

  3. > print $addTwo->(5), "\n"; #prints 7
    > print $addFour->(5), "\n"; #prints 9


    Hi!

    This is a way to get close, but I would need generate a real function
    in runtime. Something called exactly like a function. I guess that a
    pre processor is needed in order to first generate code before
    executing the code... Is there no such thing in Perl?

    BR,
    Andreas
     
    Andreas Lundgren, Sep 1, 2011
    #3
  4. Andreas Lundgren

    Dr.Ruud Guest

    On 2011-09-01 13:09, Andreas Lundgren wrote:
    > ATTRIBUTION DAMMIT - Wolf wrote:


    >> print $addTwo->(5), "\n"; #prints 7
    >> print $addFour->(5), "\n"; #prints 9

    >


    > This is a way to get close, but I would need generate a real function
    > in runtime. Something called exactly like a function. I guess that a
    > pre processor is needed in order to first generate code before
    > executing the code... Is there no such thing in Perl?


    Explore Perl's compile phase. Plenty support for lambda there.

    --
    Ruud
     
    Dr.Ruud, Sep 1, 2011
    #4
  5. Am 01.09.2011 13:09, schrieb Andreas Lundgren:
    >> print $addTwo->(5), "\n"; #prints 7
    >> print $addFour->(5), "\n"; #prints 9

    >
    > Hi!
    >
    > This is a way to get close, but I would need generate a real function
    > in runtime. Something called exactly like a function.


    Why? What is the problem with calling it the way I have shown? It is a
    "real" function. Just that you have a ref to it in a scalar variable.

    > I guess that a
    > pre processor is needed in order to first generate code before
    > executing the code... Is there no such thing in Perl?


    Again, why do you want that?

    You could do:
    *addTwo = \&$addTwo;
    and then call addTwo(2). But still, what is wrong with calling a coderef
    with $coderef->();

    - Wolf
     
    Wolf Behrenhoff, Sep 1, 2011
    #5
  6. Andreas Lundgren <> wrote:
    >> print $addTwo->(5), "\n"; #prints 7
    >> print $addFour->(5), "\n"; #prints 9

    >
    >This is a way to get close, but I would need generate a real function
    >in runtime. Something called exactly like a function. I guess that a
    >pre processor is needed in order to first generate code before
    >executing the code... Is there no such thing in Perl?


    Maybe you are looking for something as simple as eval()?

    jue
     
    Jürgen Exner, Sep 1, 2011
    #6
  7. On 1 Sep, 13:47, Wolf Behrenhoff <>
    wrote:
    > Am 01.09.2011 13:09, schrieb Andreas Lundgren:
    >
    > >> print $addTwo->(5), "\n"; #prints 7
    > >> print $addFour->(5), "\n"; #prints 9

    >
    > > Hi!

    >
    > > This is a way to get close, but I would need generate a real function
    > > in runtime. Something called exactly like a function.

    >
    > Why? What is the problem with calling it the way I have shown? It is a
    > "real" function. Just that you have a ref to it in a scalar variable.
    >
    > > I guess that a
    > > pre processor is needed in order to first generate code before
    > > executing the code... Is there no such thing in Perl?

    >
    > Again, why do you want that?
    >
    > You could do:
    > *addTwo = \&$addTwo;
    > and then call addTwo(2). But still, what is wrong with calling a coderef
    > with $coderef->();
    >
    > - Wolf


    Hi!

    The software that utalize the API is already written, and I cannot
    update that sw. I need to keep the API backward compatible. But maybe
    in combination with global symbol table like bugbear suggest would
    solve that problem!

    Thanks for the help!

    BR,
    Andreas
     
    Andreas Lundgren, Sep 2, 2011
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,923
    Victor Bazarov
    Jun 21, 2004
  2. Replies:
    80
    Views:
    3,143
    Stephen J. Bevan
    Nov 7, 2003
  3. Replies:
    1
    Views:
    669
    Marco Antoniotti
    Oct 7, 2003
  4. Replies:
    5
    Views:
    700
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    633
    S.Tobias
    Jul 24, 2004
  6. Anthony de Almeida Lopes

    Macros within function-like macros?

    Anthony de Almeida Lopes, Dec 26, 2005, in forum: C Programming
    Replies:
    13
    Views:
    974
  7. rhXX

    generating macros with another macro

    rhXX, Jun 18, 2007, in forum: C Programming
    Replies:
    7
    Views:
    401
    Thad Smith
    Jun 19, 2007
  8. Thant Tessman

    macros that build macros

    Thant Tessman, Sep 11, 2007, in forum: C Programming
    Replies:
    6
    Views:
    506
    Dave Hansen
    Sep 11, 2007
Loading...