tomazos has asked for the wisdom of the Perl Monks concerning the following question:
I want to take the literal source code of the parameter of a function call, and use it as a string at runtime. (...in a similiar fashion to the # operator in the context of a macro definition in the C/C++ preprocessor.)
sub literalize { ???? } sub assert($) { my ($condition) = @_; my $literal_condition_code = literalize(@_); if ($condition) { print "assertion failed: $literal_condition_code\n"; } } assert(42 == 24);
Output:
assertion failed: 42 == 24
The C/C++ code would be:
#define assert(condition) { if (!condition) { printf("assertion failed +: %s\n", #condition); } }
Anyone know how to do something like this in Perl?
Thanks,
Andrew.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Literal Source Parameters At Runtime
by BrowserUk (Patriarch) on Oct 29, 2007 at 05:10 UTC | |
Re: Literal Source Parameters At Runtime
by tuxz0r (Pilgrim) on Oct 29, 2007 at 15:37 UTC |
Back to
Seekers of Perl Wisdom