Getting constants from *.h header files

K

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?

Thanks!

-Karl
 
T

Tassilo v. Parseval

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
 
B

Ben Morrow

Quoth KKramsch said:
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
 
K

KKramsch

In said:
Also sprach KKramsch:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top