About eval()

Discuss and share scripts and script files...
Post Reply
Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

About eval()

Post 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;
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

FluxTorpedoe
Posts: 904
Joined: 05 Oct 2011 13:15

Re: About eval()

Post 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

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: About eval()

Post 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?
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: About eval()

Post 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".

Marco
Posts: 2354
Joined: 27 Jun 2011 15:20

Re: About eval()

Post by Marco »

Yes!! That's exactly what I had in mind! Thank you, I'll experiment with that.
Tag Backup - SimpleUpdater - XYplorer Messenger - The Unofficial XYplorer Archive - Everything in XYplorer
Don sees all [cit. from viewtopic.php?p=124094#p124094]

PeterH
Posts: 2826
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: About eval()

Post 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...

Post Reply