Page 1 of 1

[NO BUG] Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 14:31
by Filehero
Hi Don,

Code: Select all

  $a = eval("A.b.c.d.");
  msg($a, "1");
Abcd.png
Imo the dots must survive because they are properly quoted and hence "stringified".


Cheers,
Filehero

/e: added "[NO BUG]" to title

Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 16:36
by PeterH
I'm afraid what you wrote is just an invalid expression. (And the problem, that XY doesn't give info about such errors :( )

So: the string to evaluate is A.b.c.d. - so far correct. But: this expression cannot be evaluated, as it is the same as
$a = A.b.c.d.;
This:
- either must be quoted $a = "A.b.c.d.";
- or is concatenation, then quotes are $a = "A"."b"."c"."d".;
(with an illegal trailing concatenation character :shock: - you might also say: concat with nothing :biggrin: )

XY just ignores the missing quotes and interprets the second version.
Some people love this smartness or robustness, others are sad that they don't see what's wrong. :?

Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 17:02
by Filehero
Hi Peter,

you have perfectly analyzed the bug. :appl:

Yes, the bug (I know you consider it a bug too), because I clearly followed the rules as I

- defined a regular string by double-quoting the literals
- and passed that regular string to another function which of course has to respect the very nature of the parameter passed

Actually, it's funny

Code: Select all

  $b = "baby";
  $d = "dance";
  $str = eval("A.$b.can.$d.");
  msg($str, "1")
To me it's currently not a biggie, but extremly strange because SC is breaking it's own specification given in the manual. And that is nothing I would call perfect or elegant (aka Zen). :whistle:


Cheers,
Filehero

Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 17:21
by PeterH
Split it up: from $str = eval("A.$b.can.$d.");
make:
$val = "A.$b.can.$d.";
$str = eval($val);

The result of $val is the string A.baby.can.dance.
eval calculates this as an expression. Here the dots are concatenation characters to concatenate the illegally unquoted strings: A, baby, can, dance, and an empty string. Formally wrong, but XY does it ignoring the error.

It simaulates
$str = eval("""A"".""$b"".""can"".""$d"".""""");
while you seem to want
$str = eval("""A.$b.can.$d.""");

So again: you expression only shows a result, as XY ignores the error in the expression for eval().

Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 17:39
by Filehero
Hi Peter,

thanks a lot for your support.

I should have made myself a tad clearer from the beginning: I already use the following "workaround" pattern. :oops:
PeterH wrote:Split it up: from $str = eval("A.$b.can.$d.");
make:
$val = "A.$b.can.$d.";
$str = eval($val);
The reason why I nevertheless posted this complaint is that in any other programming language I know both statement sets are perfectly and truly equivalent - and I think XY SC should follow this logic.


Cheers,
Filehero

Re: SC eval() To Dot or not to Dot

Posted: 27 Dec 2014 23:00
by PeterH
Now you mix me up :shock:

$str = eval("A.$b.can.$d.");
*is* identical to the 2 statements:
$val = "A.$b.can.$d.";
$str = eval($val); // string to evaluate is a.baby.can.dance. => Syntax Error

And for the example of your dancing baby both return it without dots.

As I've written: this includes a syntax error, and to correct it you must write:
$val = """A.$b.can.$d.""";
$str = eval($val); // string to evaluate is "a.baby.can.dance."
or identical:
$str = eval("""A.$b.can.$d.""");

Maybe, to understand a bit more what happens, step through the script, and see what happens with the variables and values. It shows that everything is rather correct here.

OK: we might try to guess why the eval() is here. In form of the example it just deletes the inner double-quotes, so it's *identical* to
$str = "A.$b.can.$d.";
I don't think you just want to look it a bit more complicated. :roll:
But if I would understand I would replace a pair of double quotes wiht a single single-quote:
$str = eval('"A.$b.can.$d."');
*or*
$str = eval("'A.$b.can.$d.'");
In *this* situation the results are identical. But on stepping you will see that the intermediate result is different:
- for 1st case the string to eval still contains the variable names, and eval() replaces them with their values,
- for 2nd case variables are resolved *before* eval() is called.
So there *can* be situations where the result is different. (Else you wouldn't have to use eval() at all.)

Re: SC eval() To Dot or not to Dot

Posted: 28 Dec 2014 15:15
by Filehero
Hi Peter,

first of all, your help is really appreciated So, sorry for making you slightly confused. :oops:
PeterH wrote: $str = eval("A.$b.can.$d.");
*is* identical to the 2 statements:
$val = "A.$b.can.$d.";
$str = eval($val); // string to evaluate is a.baby.can.dance. => Syntax Error
Yes, you're right, they are identical, my cold, erm fault.

So why I am/was using eval() anyway?
The point is, that $val is global and will be accessed in a subroutine which is called in the very next statement after that said assigment. And without making $a and $b also global, both vars hadn't been resolved upon "pass" to the subroutine yesterday. So I evaled it.
But today - actually right now when re-checking things before posting :whistle: - I can't reproduce this obviuous hick-up any longer. :?: :shock: :veryconfused:
Which IS GOOD since eval() isn't needed anymore. :D :beer:

Nevertheless, this quoting/operator mix-up described here should be remedied some day. With regard to the "specification" to me it's simply a quoting/escaping inconsistency.

Thanks again for your help.


Cheers,
Filehero

Re: SC eval() To Dot or not to Dot

Posted: 28 Dec 2014 17:51
by PeterH
Sorry: still 'no'.

You can imagine as if the 1st operand of eval is on the right of an equation, so:
$str = eval("A.$b.can.$d.");
is identical to:
$str = A.$b.can.$d.;
This is formally wrong, but is ("by internal rule") interpreted as if it would be
$str = "A".$b."can".$d."";
i.e. the virtual (as being wrong) string constants like A are interpreted as real string "A", while the dots are "real" concatenation characters.
(How else would you specify concatenation dots to be evaluated??)

Try it: all examples are the same. The first is how eval() works, and the second is how XY treats unquoted (= wrong!) string tokens by virtually adding quotes.

So: no bug here.

[NO BUG] Re: SC eval() To Dot or not to Dot

Posted: 28 Dec 2014 18:05
by Filehero
PeterH wrote:So: no bug here.
Well - also upon the strong advice of my headaches - I see a sufficient amount of truth there. Changed the tread title.


Cheers,
Filehero