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. Advertising

  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. Advertising

  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. Advertising

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. Replies:
    80
    Views:
    2,489
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    473
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    511
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    534
    Michael T. Babcock
    Nov 3, 2003
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    513
    S.Tobias
    Jul 24, 2004
Loading...

Share This Page