Getting constants from *.h header files

Discussion in 'Perl Misc' started by KKramsch, Jul 19, 2004.

  1. KKramsch

    KKramsch Guest

    I have a Perl extension written in C that uses several constants,
    which are defined in the usual way as macros in a header (*.h)
    file:

    /* foobar.h */
    #define FOO 1
    #define BAR 2

    ....etc.

    Is there a standard way for Perl code to access these constants?

    Thanks!

    -Karl

    --
    Sent from a spam-bucket account; I check it once in a blue moon. If
    you still want to e-mail me, cut out the extension from my address,
    and make the obvious substitutions on what's left.
     
    KKramsch, Jul 19, 2004
    #1
    1. Advertising

  2. Also sprach KKramsch:

    > I have a Perl extension written in C that uses several constants,
    > which are defined in the usual way as macros in a header (*.h)
    > file:
    >
    > /* foobar.h */
    > #define FOO 1
    > #define BAR 2
    >
    > ...etc.
    >
    > Is there a standard way for Perl code to access these constants?


    There are several possible ways. One would be running the header through
    h2ph:

    ethan@ethan:~$ h2ph -d . foobar.h
    foobar.h -> foobar.ph
    ethan@ethan:~$ perl -e 'require "foobar.ph"; print &FOO'
    1

    Or you could use h2xs to turn the header into a full-fledged module with
    proper namespacing etc. In the latter case, the module requires
    compilation before it can be used. But it is more flexible as you can
    specify which of the constants are to be exported into the caller's
    namespace per default, which on demand and so forth.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jul 19, 2004
    #2
    1. Advertising

  3. KKramsch

    Ben Morrow Guest

    Quoth KKramsch <>:
    >
    > I have a Perl extension written in C that uses several constants,
    > which are defined in the usual way as macros in a header (*.h)
    > file:
    >
    > /* foobar.h */
    > #define FOO 1
    > #define BAR 2
    >
    > ...etc.
    >
    > Is there a standard way for Perl code to access these constants?


    h2xs will generate constant Perl subs for you, if you ask it nicely :).

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, Jul 19, 2004
    #3
  4. KKramsch

    KKramsch Guest

    In <> "Tassilo v. Parseval" <> writes:

    >Also sprach KKramsch:


    >> I have a Perl extension written in C that uses several constants,
    >> which are defined in the usual way as macros in a header (*.h)
    >> file:
    >>
    >> /* foobar.h */
    >> #define FOO 1
    >> #define BAR 2
    >>
    >> ...etc.
    >>
    >> Is there a standard way for Perl code to access these constants?


    >There are several possible ways. One would be running the header through
    >h2ph:


    > ethan@ethan:~$ h2ph -d . foobar.h
    > foobar.h -> foobar.ph
    > ethan@ethan:~$ perl -e 'require "foobar.ph"; print &FOO'
    > 1


    >Or you could use h2xs to turn the header into a full-fledged module with
    >proper namespacing etc. In the latter case, the module requires
    >compilation before it can be used. But it is more flexible as you can
    >specify which of the constants are to be exported into the caller's
    >namespace per default, which on demand and so forth.



    Thanks!

    -Karl

    --
    Sent from a spam-bucket account; I check it once in a blue moon. If
    you still want to e-mail me, cut out the extension from my address,
    and make the obvious substitutions on what's left.
     
    KKramsch, Jul 20, 2004
    #4
    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. rossum
    Replies:
    2
    Views:
    427
    rossum
    Oct 19, 2004
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    616
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,317
    Walter Roberson
    May 1, 2006
  4. John Goche

    constants in header files

    John Goche, Sep 15, 2006, in forum: C++
    Replies:
    3
    Views:
    448
    Frederick Gotham
    Sep 15, 2006
  5. mlt
    Replies:
    2
    Views:
    863
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page