Page 1 of 1

About eval()

Posted: 22 Oct 2012 15:40
by Marco
Hi everybody,
can someone please give me some examples of usage of the eval() function? According to Wikipedia

Code: Select all

In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval.
XY help says

Code: Select all

Evaluates an expression.
Should I assume then that XY eval() is a "subset" of other languages eval(), and works only with numeric entities? If so it would explain why this doesn't return 1 but $a

Code: Select all

 eval("$a = 1;");
 echo $a;

Re: About eval()

Posted: 23 Oct 2012 08:34
by FluxTorpedoe
Hi'

I don't have much perspective on XY scripting, so you'll probably get more practical examples from others.
But at least here's the start of an answer. :)

Regarding your example, AFAIK:
1. eval() is a command that processes data - like round() for example. It doesn't 'define' anything, and is not an alternative to 'set', 'global', or 'perm'. So in your example, your second line doesn't get any information from the first. At least it should be: $a = eval("$a = 1;"); echo $a;
2. eval() is not meant to be fed literally with a 'declaration' (e.g. x=y), but (to simplify), with some form of operation (e.g. x+y, $var1+$var2), or more importantly combination (e.g. xy, $var1$var2).

I scanned my scripts to find where I'd used it with some practical example.
In the following case, I needed to know the path of the active pane, but based on its pane number (i.e. <xypane1> or <xypane2>), because for technical reasons it wasn't possible to ask for the 'active pane':

Code: Select all

  $x = (get("pane") == 1)? 1 : 2;
// (...)
  echo eval("<xypane".$x.">");
Edit: added quotes to the last line to make it more proper, but you can do without them.

In this next case, I wanted the HTML formatting of a report to be easily modifiable by variables in the header of the script. Some of them needed to be combined, e.g to obtain the left padding style from the variables (overall margins + left spacing):

Code: Select all

  $Margin = 12;
  $LeftColSpacing = 20;
// (...)
  $myReport = $myReport."<style type='text/css'> #TD2{padding-left:".eval($Margin+$LeftColSpacing)."px;}</style>";
Last but not least, one of the interesting uses of eval() is to combine variables. I had no real example, so here's a working but useless one:

Code: Select all

  $Pass1 = "foo";
  $Pass2 = "bar";
  $Pass3 = "done!";
  while ($i < 3) {
    $i++;
    echo eval($Pass$i);
  }
Well, I hope there's not too much BS in my examples :P , and that it sheds some light on your question... :D

Have a nice day! 8)
Flux

Re: About eval()

Posted: 23 Oct 2012 11:40
by Marco
Oh thank you! Yeah you definitely shed light on the subject, thank you again. I'll bookmark your post for reference and let's see if eval might come in handy in one of my future scripts.

PS: silent forum in these days uh?

Re: About eval()

Posted: 23 Oct 2012 12:13
by PeterH
Maybe what you were thinking about was something like a feature of the "Load" command?
With
Load $var, , "s";
you can execute the contents (i.e. the statements) contained in a variable $var.

The bad thing: it executes as a sub-script, and as such has no access to the variables of the current script. But it can contain "Global" statements and then share global variables.

The good thing: you can dynamically create scripting code in a variable, and then execute it.

If it could share the variable pool of the calling script I would love to use it as a kind of "macro".

Re: About eval()

Posted: 23 Oct 2012 12:26
by Marco
Yes!! That's exactly what I had in mind! Thank you, I'll experiment with that.

Re: About eval()

Posted: 23 Oct 2012 12:34
by PeterH
Marco wrote:Yes!! That's exactly what I had in mind! Thank you, I'll experiment with that.
I did some experiments. But without direct access to the calling routines variables the resulting code was less efficient than direct coding. Too much overhead to read and write global variables...