http://www.perlmonks.org?node_id=168555

tune has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks!

I have a task to parse some PHP codes and mine some system-wide constants out and apply it in my Perl code as own constants.
I think it is possible by parsing out the constant name, and value from the PHP file, and then eval a constant declaration.
But the Perl interpreter thinks otherwise:

eval "use constant CONST1=>5" or warn $@; # when I run this I get: +Warning: something's wrong at ./consteval.pl line 3.
So what's wrong?

--
tune

Replies are listed 'Best First'.
•Re: constant and eval (are they enemies?)
by merlyn (Sage) on May 22, 2002 at 20:52 UTC
    Let's say you wanted to make CONSTANT1 be 1, CONSTANT2 be 2, and so on. This loop does the same as constant:
    BEGIN { no strict 'refs'; for (1..5) { my $num = $_; *{__PACKAGE__ . "::CONSTANT$num"} = sub () { $num }; } }
    It's important that $num here be a closure variable, so that all five coderefs are distinct.

    You should be able to extrapolate this strategy to your application.

    -- Randal L. Schwartz, Perl hacker

Re: constant and eval (are they enemies?)
by Juerd (Abbot) on May 22, 2002 at 19:53 UTC

    eval returns whatever the block of code returns. A block of code returns the return value of the last evaluated statement. use is a compile-time keyword, and at runtime, nothing happens in your evaled code, so it returns undef. Undef is false, so $@ is warned. But because nothing went wrong, $@ is empty, and warn warns "something's wrong".

    Fix it like this:

    eval "..."; warn $@ if $@;

    Or like this (make it return true):

    eval "...; 1;" or warn $@;
    The first solution is better, imho.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: constant and eval (are they enemies?)
by Anonymous Monk on May 22, 2002 at 19:52 UTC
    You have to make it return a true value upon success. eval "use constant CONST1 => 5; 1" or warn $@; I'd suggest not using eval though. You can manually do what the use statement does for you.
    require constant; constant->import(CONST1 => 5);
    Cheers,
    -Anomo
      Thanks. I cannot use the import() thing, because I parse the constant name and the value from a file. That's why I have to use 'eval'...

      --
      tune

        Why can't you do
        require constant; constant::->import($name => $value);
        ?

        -Anomo