Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

constant and eval (are they enemies?)

by tune (Curate)
on May 22, 2002 at 19:30 UTC ( #168555=perlquestion: print w/ replies, xml ) Need Help??
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 ./ line 3.
So what's wrong?


Comment on constant and eval (are they enemies?)
Download Code
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);
      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'...


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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://168555]
Approved by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2016-05-05 11:59 GMT
Find Nodes?
    Voting Booth?