in reply to C pre-processor in Perl
Could someone tell me why constant is preferable to #define in the following code?
- constant will work on platforms without cpp (that's the big one for me :-)
- constant is package scoped. #define is not.
- With constant you have access to perl at compile time. With #define you do not.
- Different versions of cpp on different platforms do different things.
- It's unlikely to be much faster, if at all. The constant's are expanded at compile time so there is no run-time cost using either system. Starting up a cpp process may actually slow down your compile time.
- require() will include your code only once, compile in a separate lexical file scope, and in package main by default. #include will not.
- C macros are very much not first class macros IMHO. See Macros, LFSPs and LFMs for more on this.