Re: Conditional includes
- Posted by jeremy (admin) Nov 12, 2010
- 1318 views
How would I do a conditional include?
If x then include foo.e end if
What is "x" at this point?
Or, somewhat related question: how can I include a file specified as a string:
sequence s = "myfile.e" include s
Being able to do one (or both) of these would be a great help.
You cannot really do that at this point because Euphoria parses, Converts to IL, then runs. The above script would require Euphoria to be able to know what "s" is and that "s" has not changed before include s is executed. In the above situation, that is very possible but no so in the more complex situation that you would want to use this in. For example:
sequence incName = prompt_string("What file do you want to include?") include incName
This would require that the application be up and running in the interpreter before the parser ever hit line #2. See what I mean?
Now, depending on what you want to accomplish you can use the ifdef system. It controls things at parse time, but in doing so, it's not as flexible because you cannot run Euphoria scripts in order to make the decision. Let's say you have 1 of 3 database drivers you could possibly want to use:
ifdef MYSQL then include mysql.e as db elsifdef PGSQL then include pgsql.e as db elsifdef EDS then include eds.e as db elsedef crash("Valid database driver could not be determined") end ifdef
Then:
$ eui -dPGSQL myprog.ex
You can also add to your eu.cfg file the -d line.
See: man:lang_branch.html#ifdefstatement
Jeremy