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

Not a good title but not sure how to describe it...or how to find it in CPAN...
I want to save some config options that are "on" or "off"
I want to save a logical expression of them and determine if it is true or false.
My ideas is to just use eval() and maybe safe::

For example I might save this in my config file.
(option1 && option2) || option3 And my program might define
option3, but not 1 or 2

I was thinking of s/// every option(1,2) with 0 and every option3 with 1 and then evaling it..

Would this be a good approach?

thanks

Replies are listed 'Best First'.
Re: and / or for config options
by DamnDirtyApe (Curate) on Aug 16, 2002 at 23:08 UTC

    If I understand you correctly, you want to do something like this:

    test_l.cfg
    %config = ( 'proceed_with_x' => '( ($option1 && $option2) || $option3 )' ) ;
    test_l.pl
    #! /usr/bin/perl use strict ; use warnings ; $|++ ; our %config ; do "test_l.cfg" ; my ( $r, $option1, $option2, $option3 ) ; # Case 1: Should pass. ( $option1, $option2, $option3 ) = ( 1, 1, 0 ) ; $r = eval qq[$config{'proceed_with_x'}] ; print "Case 1: " . ( $r ? "PASS" : "FAIL" ) . "\n" ; # Case 2: Should pass. ( $option1, $option2, $option3 ) = ( 0, 0, 1 ) ; $r = eval qq[$config{'proceed_with_x'}] ; print "Case 2: " . ( $r ? "PASS" : "FAIL" ) . "\n" ; # Case 3: Should fail. ( $option1, $option2, $option3 ) = ( 0, 1, 0 ) ; $r = eval qq[$config{'proceed_with_x'}] ; print "Case 3: " . ( $r ? "PASS" : "FAIL" ) . "\n" ; __END__

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      makes sense, thanks!