tags:

views:

578

answers:

4

How do I use macros in Perl, like I do in C?

Example:

#define value 100 
print value; 

I want to get the output as 100.

A: 

Try the following:

macro.pl

    use strict;
    use warnings;
    #define a 10;
    print a;

And run this code using -P switch.

Example: perl -P macro.pl

And use the module Filter::cpp also.

kiruthika
From the perlrun man page: "NOTE: Use of -P is strongly discouraged because of its inherent problems, including poor portability."
Gavin Brock
Has anyone ever found a good use for -P in production code?
Gavin Brock
Although this technically answered the question, it would be better if it mentioned how messy things get when running a whole perl script through the C pre-processor with the -P flag. Indeed the perlrun manpage says in boldface "NOTE: Use of -P is strongly discouraged because of its inherent problems, including poor portability. It is deprecated and will be removed in a future version of Perl."http://perldoc.perl.org/perlrun.html
msw
-1, -P is a horrible flag.
rjh
@Gavin - doesn't "P" stand for "Production"? :)
DVK
Downvoted. This is a *horrible* "accepted" answer.
Randal Schwartz
+25  A: 

If you just want to define constants (like your example) rather than full macros, there are a couple of perl ways to do this.

Some people like:

use constant value => 100;
print value;

Note that 'value' is a subroutine, not a 'variable'. This means you cannot interpolate it in strings so you have to do. print "The value is ".value."\n";.

The "Best Practices" crowd like:

use Readonly;
Readonly my $value => 100;
print $value;

However, unlike constant, Readonly is not part of the core perl distribution and so needs to be installed from CPAN.

Gavin Brock
Constants are difficult to interpolate into a string, amongst other problems. Use **Readonly**.
Duncan
Duncan: constants get optimized away if(someconst){...} will go away entirely during compilation if someconst is false. That is NOT the case for Readonly variables. Also, if you insist on using Readonly, ALWAYS install Readonly::XS!
tsee
@tsee, sounds like micro-optimisation. I'm sure `if(0)` is fast enough; aside from that, the OP isn't asking for `#ifdef`. See http://search.cpan.org/perldoc?Readonly#COMPARISON_WITH_"use_constant" - use constant has tons of problems.
rjh
@rjh: I disagree on most of the criticism in the Readonly documentation. Furthermore, the compile-time behaviour means that you also save the memory of the OP-structure of the code. Regarding if(0): You're wrong: With Readonly (at least the pure-perl version), you actually pay at least one subroutine call per access. That's *a lot* since subroutine calls are very slow. If you consider something like ''Readonly my $DEBUG'', and use it for debugging output liberally, you pay dearly at run-time.
tsee
Constants aren't _that_ difficult to interpolate into strings. Thy might be slightly annoying, but not difficult.
brian d foy
@tsee: I agree with your point about Readonly - XS is a given. Still, as a Moose user, I'm quite used to trading performance for more maintainable and less error-prone code.
rjh
@brian - could you please elaborate? Did you mean http://dev.perl.org/perl6/rfc/252.html? perldoc constant explicitly states "Constants defined using this module cannot be interpolated into strings like variables" - and I don't recall any way interpolation would work on a subroutine. Thanks!
DVK
Don't put too much stock in Perl 6 RFCs. Every crank came out to say all sorts of dumb things. It's not that difficult to write @{[CONSTANT]}, among the 10 other ways you could do the same thing.
brian d foy
+8  A: 

For constants, the common way is to use a constant subroutine that will get inlined by the compiler:

use constant value => 100;

or

sub value () {100}
Eric Strom
+11  A: 

Perl is not C. You would be much better served by learning the corresponding Perl idioms (such as the already mentioned use constant value => 100) than by trying to drag C idioms into Perl.

Dave Sherohman